Need some help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Radrily
    New Member
    • Sep 2008
    • 1

    Need some help

    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\nChoo se 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?
  • scruggsy
    New Member
    • Mar 2007
    • 147

    #2
    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.

    Comment

    • Studlyami
      Recognized Expert Contributor
      • Sep 2007
      • 464

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

      and finally you need to check your brackets check my comments for where
      Code:
      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();
      //NEEDS A END BRACKET from the bottom.
      else {
      cout<<"The quotient is "<<ans2/ans3<<"With a remainder of "<<remainder".";
      cin.get();
      } //one bracket needs to be moved up
      }

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        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,mu lt or divide requirement
        cin>>ans;
        3.if (ans=a)cout<<"R esult= "<<x+y<<end l;
        if(ans=m)cout<< "Result= "<<x*y<<end l;
        etc.
        4.cout<<"Remain der = "<<x%y<<end l;
        Code:
        ps. You should wrap your code in code tags

        Comment

        Working...