Connecting Tech Pros Worldwide Forums | Help | Site Map

Need some help

Newbie
 
Join Date: Sep 2008
Posts: 1
#1: Sep 4 '08
I'm trying to create a small program for a beginner's C++ class. Basically, we have to take user input of 2 numbers and then output the sum, difference, product, and quotient of each. I'm trying to make mine a bit more user friendly and slightly more difficult that what is probably expected. This is what I have:

#include <iostream>

using namespace std;

int main(void)
{
char ans1; //I go ahead and declare my variables...
double ans2;
double ans3;
float add;
float subtract;
float mult;
float divide;
float remainder;

/*--Starting here, I prompt the user to specify what they want to do with
their two number choices--*/
cout<<" ------------------------------------------- ";
cout<<" Simple math ";
cout<<" ------------------------------------------- ";
cout<<"Choose what you want to do with two numbers:\n\na. Addition\nb. Subtraction\nc. Multiplication\nd. Division";
cout<<"\n\nChoose a, b, c, or d: ";
cin>>ans1;
if(ans1=='a'){ //This line starts my if loop that branches the programs actions according to the user's input.
cout<<"You've chosen Addition. Please enter two numbers separated by spaces:\n\n";
cin>>ans2>>ans3;
cout<<"The sum is "<<ans2+ans3<<".";
cin.get();
}
else if(ans1=='b'){
cout<<"You've chosen Subtraction. Please enter two numbers separated by spaces:\n\n";
cin>>ans2>>ans3;
cout<<"The difference is "<<ans2-ans3<<".";
cin.get();
}
else if(ans1=='c'){
cout<<"You've chosen Multiplication. Please enter two numbers separated by spaces:\n\n";
cin>>ans2>>ans3;
cout<<"The product is "<<ans2*ans3<<".";
cin.get();
}
else if(ans1=='d'){
cout<<"You've chosen Division. Please enter two numbers separated by spaces:\n\n";
cin>>ans2>>ans3;
remainder=ans2%ans3;
if (remainder==0){
cout<<"The quotient is "<<ans2/ans3<<".";
cin.get();
else {
cout<<"The quotient is "<<ans2/ans3<<"With a remainder of "<<remainder".";
cin.get();
}
}
cin.get();
}
cin.get();
return 0;
}

Dev-C++ keeps giving me errors. Anyone care to hlep?

Familiar Sight
 
Join Date: Mar 2007
Posts: 148
#2: Sep 4 '08

re: Need some help


Quote:

Originally Posted by Radrily

Dev-C++ keeps giving me errors. Anyone care to hlep?

It will be easier for people to help you if you put your code inside CODE tags, and post the text of the errors you are receiving.
Expert
 
Join Date: Sep 2007
Location: VA
Posts: 416
#3: Sep 4 '08

re: Need some help


There are a couple of problems first when posting code please use the code tags provided it makes it much easier to read.
Now
Quote:
cout<<"The quotient is "<<ans2/ans3<<"With a remainder of "<<remainder".";
needs to be
Expand|Select|Wrap|Line Numbers
  1. cout<<"The quotient is "<<ans2/ans3<<"With a remainder of "<<remainder<<".";
  2.  
The modules operator works with integer values only so
Quote:
remainder=ans2%ans3;
needs to be
remainder=(int)ans2%(int)ans3;

and finally you need to check your brackets check my comments for where
Quote:
Expand|Select|Wrap|Line Numbers
  1. else if(ans1=='d'){
  2. cout<<"You've chosen Division. Please enter two numbers separated by spaces:\n\n";
  3. cin>>ans2>>ans3;
  4. remainder=ans2%ans3;
  5. if (remainder==0){
  6. cout<<"The quotient is "<<ans2/ans3<<".";
  7. cin.get();
  8. //NEEDS A END BRACKET from the bottom.
  9. else {
  10. cout<<"The quotient is "<<ans2/ans3<<"With a remainder of "<<remainder".";
  11. cin.get();
  12. } //one bracket needs to be moved up
  13. }
  14.  
Familiar Sight
 
Join Date: Jan 2007
Posts: 190
#4: Sep 4 '08

re: Need some help


You're certainly on the right track
void is not required as a main() argument.
Don't declare + - * / % opreators just........ double x,y; and..... char ans;
It seems a bit long winded
1. get the two numbers x,y
cin>>x>>y;
2.get add,subtract,mult or divide requirement
cin>>ans;
3.if (ans=a)cout<<"Result= "<<x+y<<endl;
if(ans=m)cout<<"Result= "<<x*y<<endl;
etc.
4.cout<<"Remainder = "<<x%y<<endl;
Expand|Select|Wrap|Line Numbers
  1. ps. You should wrap your code in code tags
Reply