Results 1 to 6 of 6

Thread: C++ Homework Help (while loop)?

  1. #1

    C++ Homework Help (while loop)?

    #include <iostream.h>
    #include <conio.h>

    int number = 0;
    int number2 = 0;
    int response = 0;
    while (response = 5)
    {

    void main ()

    {
    cout<<"Enter the first number: ";
    cin>>number;
    cout<<"Enter the second number: ";
    cin>>number2;
    cout<<"Menu Items\n 1)add\n 2)subutract\n 3)multiply\n 4)divide\n";
    cin>>response;
    if (response != 1-4)
    {
    cout<<"That is not a valid number"<<endl;
    }

    switch (response)
    {
    case 1:
    cout<<number + number2<<endl;
    break;
    case 2:
    cout<<number - number2<<endl;
    break;
    case 3:
    cout<<number * number2<<endl;
    break;
    case 4:
    cout<<number / number2<<endl;
    break;
    case 5:
    cout<<response<<endl;
    }

    }


    }

    Basically I don't want the program to exit once I get an anser.
    So I put in a while loop to equal 5 so every equation gets worked once, but I know it's not right and my reference book doesn't give much insight to what I'm supposed to do. Could someone help me by telling me where to put this while loop in?

    It's C++ and I'm using the Micorosoft Visual, this is a ms-dos .exe so at the end of the program I get that "Press any key" thing.

  2. #2
    Old-Fogey:Addicts founder Terr's Avatar
    Join Date
    Aug 2001
    Location
    Seattle, WA
    Posts
    2,007

    Re: C++ Homework Help (while loop)?

    Originally posted by freeOn
    cin>>response;
    if (response != 1-4)
    {
    cout<<"That is not a valid number"<<endl;
    }
    Doesn't that equate to "If the response does not equal -3" ? I'll post something later once I test it out, I've been away from any C++ for a long time.

    Your "while" is outside of main, by the way... I'm not even gonna guess if that works or not.
    [HvC]Terr: L33T Technical Proficiency

  3. #3
    PHP/PostgreSQL guy
    Join Date
    Dec 2001
    Posts
    1,164
    I'd take out the while (response == 5) and go with:

    void main() {

    while (response != "end") {

    if (response < 1 && response > 4 && response != "end") {
    cout << "1,2,3,4 or 'end'\n" << endl;
    } // end response check

    } // end while

    } // end main

    There are probably better ways to do this but it's quick-n-dirty and gets the job done, I think. This is without a compiler to check.
    We the willing, led by the unknowing, have been doing the impossible for the ungrateful. We have done so much with so little for so long that we are now qualified to do just about anything with almost nothing.

  4. #4
    no that's all about saying that if a menu item doesn't equal 1,2,3,4. then print out "that is not a valid number".

    I'm just looking for a while loop so the program repeats.

    Hey vorlin It's a requirement to use the case/switch, or else I would do the same thing.

  5. #5
    Old-Fogey:Addicts founder Terr's Avatar
    Join Date
    Aug 2001
    Location
    Seattle, WA
    Posts
    2,007
    Here's something that I think would work.

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    int number = 0;
    int number2 = 0;
    int response = 0;
    
    void main()
    
    {
            while(response != 5)
            {
                    cout<<"Enter the first number: ";
                    cin>>number;
                    cout<<"Enter the second number: ";
                    cin>>number2;
                    cout<<"Menu Items\n 1)Add\n 2)Subutract\n 3)Multiply\n 4)Divide\n 5)Quit\n";
                    cout<<"Command: ";
                    cin>>response;
                    cout<<endl;
                                  
                                    
                    switch (response)
                    {
                    	case 1:
                    	cout<<number<<" plus "<<number2<<" equals ";
                    	cout<<number + number2<<endl;
                    	break;
                    	case 2:
                            cout<<number<<" minus "<<number2<<" equals ";
                            cout<<number - number2<<endl;
                            break;
                            case 3:
                            cout<<number<<" times "<<number2<<" equals ";
                            cout<<number * number2<<endl;
                            break;
                            case 4:
                            cout<<number<<" divided by "<<number2<<" equals ";
                            cout<<number / number2<<endl;
                            break;
                            default:
                            cout<<"Invalid Command."<<endl;
                    
            	}
            	cout<<"-------"<<endl;
    	}
    }
    EDIT: I know that there is no case for five, but the program will end itself before the user even sees the whole 'invalid command' bit. I still oughta have added an "any key to quit" part... Also, due to the data types (integers) this program will round the results... If you change the data types for Number and Number2 to "double" then you'll get much more accurate answers when decimals are involved in the input or if you are dividing.
    [HvC]Terr: L33T Technical Proficiency

  6. #6
    oh cool, Terr you are the ****!. Thanks alot man it works and I also see what I was doing wrong. Thanks Alot!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •