Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Percentage Calculating program
Author Message
Mr. Gruntsworthy

Posted: Thu Nov 30, 2006 11:18 am   Post subject: Percentage Calculating program

This is a little program i wrote to test my knowledge in C++ so far. It's a little nifty program, but it was made with what i've learned and is by no means the way it should be written.

But here it is, in a .zip format, complete with all source code files and the Debug folder with the executable program. Take a look at the source code and let me know what you think.

Percentage Calculator.zip
Description:
 Here's the complete set of files for this program.

Filename:  Percentage Calculator.zip
Filesize:  1.05 MB

wtd

Posted: Thu Nov 30, 2006 12:37 pm   Post subject: (No subject)

This should be in C++ Submissions, so if a mod could move it, that would be great.

That said, here's all we need to see:

 code: //Name: Mike Seguin //Date: Nov. 29, 2006 //Purp: A complex program that gives you the percent equivalent //      of a fraction. #include #include int main() {   using namespace std;     //Before all else, i want to declare this variable, because it   //gathers a keystroke for the program to continue, so that the user   //can see the results of the program.   char anyKey;   //This is the prompt for if you want to continue/get percent   cout << "Do you want to figure out the percent of a fraction?" <> YesNo;     //if it was a y, executes main loop   if (YesNo == 'y')   {   // these are the two variables   double numerator;   double denominator;     cout << "This program will tell you the percentage " << endl;   cout << "equivalent of a fraction. " << endl;   cout << "Please enter in the numerator (top number)." << endl;   cout << "Please make sure it's a real number." << endl;   //gets the numerator   cin >> numerator;     cout << endl;   cout << "Good, now enter the denominator." << endl;   //gets the denominator   cin >> denominator;   //checks to make sure the denominator is larger than the   //numerator.   while (denominator <= numerator)   {           cout << "The denominator you have given is either too small" <> denominator;   }   //the variable that is the final percent   double final;   //figures out the final percent by dividing the numerator into the   //denominator and multiplying the result by 100.   final = ((numerator / denominator) * 100);   cout << "The percent is " << final << "." << endl;   cout << "enter in a key and hit Enter to continue." << endl;   //This is just a simple trick so that the program doesn't   //end before the user sees the result.     cin >> anyKey;   }   else   {           cout << "Oh, it appears you dont want to. Bye Bye!" << endl;           //once again, it gathers any keystroke and exits the program.           cin >> anyKey;   }   return 0; }

Now, this kind of comment is useless:

 code: // these are the two variables   double numerator;   double denominator;

You have declared "anyKey" near the top of your program, yet you don't use it until the bottom. This is puzzling since most of your variable declarations have been pretty much done just when they need to be.

 code: //the variable that is the final percent   double final;

Perhaps this should be:

 code: double finalPercent;

That would negate the need for the comment.

For the following:

 code: cout << "Do you want to figure out the percent of a fraction?" <

 code: cout << "Do you want to figure out the percent of a fraction?" << endl        << "Type y for yes, any other key for no." << endl;

At the end of each branch of your conditional you read into the anyKey variable to affect a pause. Since this will happen either way, it does not belong in the conditional.

 code: final = ((numerator / denominator) * 100);

The above can be written more clearly with a knowledge of operator precendence levels.

 code: final = numerator / denominator * 100;

Also with regards to that. You do not need to separately declare and initialize a variable.

 code: double final = numerator / denominator * 100;

According to the C++ standard, any main function which does not explicitly return an integer returns zero.

Your "return 0;" is okay, but unnecessary.

Oh, and the contents of your conditional should be indented.
Mr. Gruntsworthy

Posted: Thu Nov 30, 2006 4:02 pm   Post subject: (No subject)

It seems to me that you are an experienced programmer, and I thank you for your very informative critisizm (not being sarcastic).

This is my first feature length written program, using the small portion of C++ ive learned.

I knew it wouldn't be perfect, but I had less critisizm about it than I was expecting.

Next time i write a program, I'll take your info to heart and try to get it to be a little more organized.

But other than that, what do you think of this program? Useful, or useless
wassonator

Posted: Fri Jun 26, 2009 6:48 pm   Post subject: Re: Percentage Calculating program

man their are such things as fractions with denominators that are larger than their corresponding numerators

"//Purp: A complex program that gives you the percent equivalent "...............................hardly
// of a fraction.
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 4 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: