need help

This is a discussion on need help within the C++ Programming forums, part of the General Programming Boards category; ok so ive been working on this project for about 3 days now and i still have a few problems. ...

  1. #16
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    ok so ive been working on this project for about 3 days now and i still have a few problems. 1. capitals don't work. 2. i have no idea how to make remainder work because it wont work with double. 3. i need to display a message, if the choice is none of what i have in my code that simply says that their choice is invalid. code is as follows. 4. if the user types in more than just the first letter the program crashes. i dont know how to allow the user to type more than just the firsrt letter without the program crashing.
    Code:
    #include <iostream>
    using namespace std;
    double main () {
    	double a,b;
    	char choice ;
    	while (true){
    		cout << "\nGive 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')
    		{
    			if (b == 0)
    				cout << "Cannot divide by zero. Start over and choose another number.";
    			else
    				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;
    }

  2. #17
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    First, main returns an int, not a double.
    Code:
    double main () {
    Should be:
    Code:
    int main () {
    Second, you need to decide what type of input you are going to allow for the operation part of the exercise. A single character, or whole word. If you want to use a whole word then yes, your current setup is going to cause problems. Lets go through some sample input/output:

    1. You enter you two numbers and then type in "add" for the choice.
    2. The cin >> choice; operation reads the first character (not the whole word) 'a'.
    3. The addition operation is processed and you see the result.
    4. Now we go back into the beginning of the loop and try to get another number but guess what, we have a problem.
    5. The "dd" left over when we tried to process the user's choice is still in the input stream.
    6. The cin >> a; operation tries to read a number but chokes when it tries to process the "dd".
    7. The stream goes into a fail state and no further input can be processed.


    So, what do you want to do? Do you want to process a whole word for the user's "choice" or just a single letter? If you want to process a whole word, then use a string instead of a char:
    Code:
    #include <string>
    
    ...
    
    string choice;
    while (true){
        cout << "\nGive me a number: ";
        cin >> a;
        cout << "Give me another number: ";
        cin >> b;
        cout << "What do you want to do? ";
        cin >> choice;
    Of course you need to change the if tests also.
    Last edited by hk_mp5kpdw; 09-30-2007 at 12:49 AM.
    "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. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    1. You need to add an || to your if statements. For example, if choice is 'm' or choice is 'M'.

    2. Use int. Is there a reason you were using double in the first place? The other operations all work with int as well.

    3. Change your if sequence to an if/else if sequence. Then you can use the final else section to put the message.

    4. Are you sure it crashes? I would expect it to just close the window because the cin.ignore()'s won't work if they type in the whole word. The reason is that the cin.ignore() ignores one character. If the user types "add", then the choice will be 'a', and the two cin.ignore()'s will ignore the two 'd's. Then the window won't be left open waiting for you to hit enter.

    One solution is to use a different version of cin.ignore() that ignores multiple characters:
    Code:
    cin.ignore(1000, '\n');
    cin.ignore();

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21