Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Percentage Calculating program
Index -> Programming, C++ -> C++ Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Mr. Gruntsworthy




PostPosted: 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.

Download
 Filename:  Percentage Calculator.zip
 Filesize:  1.05 MB
 Downloaded:  1701 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: 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 <iostream>
#include <cmath>

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?" <<endl;
  cout << "Type y for yes, any other key for no." <<endl;
  char YesNo;
  cin >> 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" <<endl;
          cout << "or is an illegal character. Please enter a number" <<endl;
          cout << "that is larger than the numerator." <<endl;

          //gets the denominator again
          cin >> 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?" <<endl;
  cout << "Type y for yes, any other key for no." <<endl;


How about?

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




PostPosted: 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




PostPosted: 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:   
   Index -> Programming, C++ -> C++ Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: