Thread: need help

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    24

    need help

    program needs to be able to take 2 different numbers that part i know. then it has to output What do you want to do? i know i just use "cout <<" for that. the part of the project where i am confused is that i have to make the program do what the user tells it to do with the two numbers. multiply, add, subtract, divide, and remainder. there cant be any logical errors. im not asking anyone to write it for me, i just want to know where to start.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The user will type in something (+,-,*,/) which you will read into a char variable for example. From there, you can use a series of if/else-if tests or maybe a switch/case statement to break down what to do based on what the user inputs. After that, you just output the results of that operation the user choose using the previous numbers entered.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'd suggest doing the part you know, getting the numbers and asking "What do you want to do?" Get that compiling and running before continuing on to the next task.

    Once that is done...

    How do you want the user to tell you whether to multiply, add, subtract, divide, or remainder? Do you want them to type out the exact word? Do you want them to type the operator for them (e.g. *, +, -, / or &#37? Do you want to give them a menu with numbered choices and have them pick one of the choices?

    Decide that and you can then move to figuring out how to do it.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    and don't name a thread "need help", if you're posting here that's pretty much assumed (unless you want to gloat about a particularly good piece of code or something)

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Yeah... I'll bet you've just learned about if-statements in your class. This is for a class, right?

    In programing, this is called branching The program flow branches this-way or that-way, depending on some condition.

    In C++, branching is done with if-staments and switch/case statements.

    Branching is how programs "make decisions", and it's one of the most important concepts in computer programming.

    Once you understand branching and loops (doing stuff over & over until some condition is met) you can start to understand what programming is all about, and you can write useful programs.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    Code:
    #include <iostream>
    using namespace std;
    void main () {
    	double x,y;
    	cout << "Give me a number: ";
    	cin >> x;
    	cout << "Give me another number: ";
    	cin >> y;
    	if (y==0)
    		cout << "Start over and enter a number not equal to zero for the second value";
    	else cout << "What do you want to do? ";
    	
    	
    	cin.ignore; cin.ignore;
    }
    thats what i have so far for code. im slow and i dont really have the best grasp on how to use if and else but yah this is for a class and yah we just learned if statements. from here out im confused. we also just learned how to use char. the prof wants us to use char to get the input after "what do you want to do?" so the user would type divide subtract add remainder or multiply. its only necessary to read the first letter since they are all different. so say if the user entered "dance" you would get divide

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Ok, so if you're going to read a char from the user, then you'll want a variable to read into, right? So add that variable. Then add code to read into the variable. You've already shown you can do this with a double, so do it with the char.

    >> cin.ignore; cin.ignore;
    That should be cin.ignore(); cin.ignore();

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    stuck in mounds of if statements please help. i cant seem to figure out how to make the program get past my first if statement
    Code:
    #include <iostream>
    using namespace std;
    void main () {
    	double a,b;
    	char add,subtract,divide,remainder,multiply;
    	cout << "Give me a number: ";
    	cin >> a;
    	cout << "Give me another number: ";
    	cin >> b;
    	cout << "What do you want to do? ";
    	cin >> add,subtract,divide,remainder,multiply;	
    	if (b==0 , divide)
    		cout << "Cannot divide by zero.";
    	cin.ignore(); cin.ignore();
    }
    i had some more if statements after that but they just caused errors. i removed them

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    void main()
    The main function should always return an int:
    Code:
    int main()
    {
        ...
        return 0;
    }


    Code:
    cout << "What do you want to do? ";
    cin >> add,subtract,divide,remainder,multiply;
    That's the wrong way to get values into your variables. If you wanted to get multiple values you'd do this:
    Code:
    cin >> add >> subtract >> divide >> remainder >> multiply;
    However, this is not what you want to do. You only want to get a single character from the user and then based on that character you can test to see what the user wanted to do and go on from there:
    Code:
    char choice;
    
    ...
    
    cout << "What do you want to do? ";
    cin >> choice;



    Now to test this value that you get, it is as simple as:
    Code:
    if( choice == '*' )
    {
        // Do the multiplication
    }
    else if( choice == '/' )
    {
        // Do the division
    }
    etc...
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    12
    i would probably use switch/case. its pretty easy to understand

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
     double a,b,value;
     int choice;
     
     cout<<"Please enter two numbers."<<endl;
     cin>>a>>b;
     cout<<endl;
     
     cout<<"what would you like to do?"<<endl
     <<"1. multiply"<<endl<<"2. divide"<<endl
     <<"3. add"<<endl<<"4. subtract"<<endl<<"5. remainder"<<endl;
     cin>>choice;
     cout<<endl;
      
      switch(choice)
      {
        case 1:
        {
          value = a * b;
          cout<<value;
        }break;
        case 2:
         {
          if(b == 0)
          {
            cout<<"cannot divide by zero.";
          }
          else
          {
            value = a / b;
            cout<<value;
          }
         }break;
        case 3:
        {
         value = a + b;
         cout<<value;
        }break;
        case 4:
        {
         value = a - b;
         cout<<value;
        }break;
        case 5:
        {
         value = a % b;
         cout<<value;
        }break;
        defalut:
        {
          cout<<"That was not an option. Ending program.";
          return 0;
    }
        }break;
      }
      cin.ignore(2); // do this so u dont have to type it twice.
      return 0;
    }
    This works if you wanna use it. You would have to change the cout statements to suit you.
    p.s. just random to save a little typing when declaring you can use this instead /* -(declartions)- */
    example:
    Code:
    /*
    Name
    Project
    Date
    */
    Last edited by Chaosreborn; 09-27-2007 at 10:57 PM.

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    i used hk_mp5kpdw's solution but now if i type the full word "add" instead of just "a" the program terminates.... and i also cant use this solution for the remainder because i cant used double. only int how do i fix this?
    Code:
    #include <iostream>
    using namespace std;
    double main () {
    	double a,b;
    	char choice ;
    	cout << "Give me a number: ";
    	cin >> a;
    	cout << "Give me another number: ";
    	cin >> b;
    	cout << "What do you want to do? ";
    	cin >> choice;	
    	if (choice == 'm')
    		cout << a << " * " << b << " = " << a*b;
    	if (choice == 'd')
    		cout << a << " / " << b << " = " << a/b;
    	if (choice == 's')
    		cout << a << " - " << b << " = " << a-b;
    	if (choice == 'a')
    		cout << a << " + " << b << " = " << a+b;
    
    
    	cin.ignore(); cin.ignore();
    return 0;
    }

  12. #12
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127

    Cool

    I don't want to do your homework, but let me help you as much as possible...

    In each of the following examples, only ONE of the "cout" statements will execute:

    Example 1) The IF statement:
    Code:
    int x = 1;
    
    if(x == 1)
    {
        cout << "X is equal to one." << endl;
    }
    In Example 1, the "cout" statement will execute.

    Example 2) The IF / ELSE statement:
    Code:
    int x = 0;
    
    if(x == 1)
    {
        cout << "X is equal to one." << endl;
    }
    
    else    // This implies X is NOT equal to one, therefore the IF condition was not met
    {
        cout << "X is NOT equal to one." << endl;
    }
    In Example 2, the "cout" statement within the ELSE block will execute.

    Example 3) The IF / ELSE IF / ELSE statement:
    Code:
    int x = 0;
    
    if(x == 1)
    {
        cout << "X is equal to one." << endl;
    }
    
    else if(x == 0)    // A second logical test, there can be as many ELSE-IF statements as you need...
    {
        cout << "X is equal to zero." << endl;
    }
    
    else
    {
        cout << "X is not equal to one, and X is not equal to zero." << endl;
    }
    In Example 3, the "cout" statement within the ELSE IF block will execute.

    Another thing you need to consider in your program, is what to do if the user's input isn't valid. We won't worry about what number they enter, but what about the operand they select? You have to ask yourself some questions about your program. Does the user know HOW to enter the operand? Do they type "add"? Do they simply enter the '+' char? You can add a level of protection against unwanted input with the following code:
    Code:
    char choice;
    
     cout << "Would you like to add, subtract, multiply or divide?" << endl
             << "Enter [+]-add [-]-subtract[*]-multiply [/]-divide then press [Enter]" << endl;
    
    while ((choice != '+') && (choice != '-') && (choice != '*') && (choice != '/'))
    {
        cin  >> choice;
    }
    The above continues to execute if the user enters something that is not what you specify in the WHILE statement.


    BTW!!!
    I'm not sitting in front of a computer with a compiler on it right now, so if anything doesn't work, please, nobody flame me! It's late and I have to get up in three hours to go to work!

  13. #13
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by sh3rpa View Post
    I'm not sitting in front of a computer with a compiler on it right now
    You compile code before you post it here?

    QuantumPete

    BTW, xclarkiex5x!

    You should use braces for *all* if statements! i.e.
    Code:
    	if (choice == 'm') {
    		cout << a << " * " << b << " = " << a*b;
    	}
    	if (choice == 'd') {
    		cout << a << " / " << b << " = " << a/b;
    	}
    	if (choice == 's') {
    		cout << a << " - " << b << " = " << a-b;
    	}
    	if (choice == 'a') {
    		cout << a << " + " << b << " = " << a+b;
    	}
    Last edited by QuantumPete; 09-28-2007 at 01:38 AM.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  14. #14
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127

    Lightbulb

    Quote Originally Posted by QuantumPete
    You compile code before you post it here?
    What can I say? I'm a rebel!

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you don't have a compiler and want to post code, you can check the syntax with Comeau online

    This wouldn't help you actually test the code, though.

    The missing braces are about style and good habit, though.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed