loops in C++

Discussion in 'Mac Programming' started by iBookG4user, Sep 7, 2009.

  1. iBookG4user macrumors 604

    iBookG4user

    Joined:
    Jun 27, 2006
    Location:
    Seattle, WA
    #1
    Ok, I've gotten better at coding since the last thread and managed to do the previous 3 weeks of coursework. Although this one is throwing me for a loop, an infinite loop to be exact :p. For this weeks assignment we have to make a calculator that loops until the user inputs "done". The calculator part works perfectly, although I'm having trouble getting it to loop properly.

    Can someone please point me in the right direction? :)
    (I may have more questions about the next three assignments that I have to do, since we are starting to get into functions and I want to learn this.)

    Here's my current code:
    Code:
    #include <iostream>
    using namespace std;
    int main () 
    
    {
    	int var1;
    	int var2;
    	int math;
    	char done = math;
    	done=tolower(done);
    	{
    	cout << "Please enter two operators" << endl;
    		cin >> var1 >> var2;
    		cout << endl;	
    		cout << "Please choose an operation" << endl;
    		cout << "1. Addition" << endl;
    		cout << "2. Subtraction" << endl;
    		cout << "3. Multiplication" << endl;
    		cout << "4. Division" << endl;
    		cout << "5. Enter 'done' to exit calculator" << endl;	
    		cin >> math;
    		cout << endl;
    		if (math == done)
    		{
    			cout << "Thanks for using the calculator." << endl;
    			return 0;
    		}
    		switch (math)
    		{
    			case 1:
    				cout << var1 + var2 << endl;
    				break;
    			case 2:
    				cout << var1 - var2 << endl;
    				break;
    			case 3:
    				cout << var1 * var2 << endl;
    				break;
    			case 4:
    				if (var2 != 0)
    					cout << var1 / var2 << endl;
    				else
    					cout << "ERROR: Cannot divide by zero." << endl;
    				break;
    		}
    	        }
    	return 0;
    }
    
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    What word or words in the C++ language are used to designate a loop? For example, the word 'if' designates a conditional test, the word 'switch' designates a multi-way branch, and so on. So what is the word for designating a loop?

    If you don't immediately know the answer, then you first need to read the section of the language reference that covers looping. If you do know the answer, then point out where in your code you've placed the word, because I don't see it.

    I should also warn you that you have several bugs in your code, even after you figure out how to designate a loop.
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    I'm having a lot of trouble seeing how this loops at all. I don't see a do, a while, a for, etc.

    I also don't see how you're ever going to read "done" when you're read is into an integer, and what you'd store in the variable named done. I suppose in a 4-byte integer you could store the ascii values of the 4 characters, d, o, n, e... but that would be a pretty poor approach, and cin isn't going to store characters in an int this way.

    You may need to read into a std::string or char *, dependent on how far you are. Then check for done, and otherwise convert this to an int.

    Also, the declaration and initialization at the top of your program doesn't make much sense. You declare math as an int, but don't initialize it. You then assign this uninitialized value to a char, which will result in truncation of the garbage that's in there. Pretty much nothing the use enters will ever equal that reliably (it may once, but probably won't the next run).

    Also, in your first prompt you need to request two operands, not two operators.

    -Lee
     
  4. iBookG4user thread starter macrumors 604

    iBookG4user

    Joined:
    Jun 27, 2006
    Location:
    Seattle, WA
    #4
    Well, I took another look at my program and managed to get it working as I wanted. So it does loop and exit when needed. Here's the new code:

    Code:
    #include <iostream>
    using namespace std;
    int main () 
    
    { //main
    	int var1;
    	int var2;
    	int math;
    		while (math != 5)
    		{ //while
    	cout << "Please enter two operators" << endl;
    		cin >> var1 >> var2;
    		cout << endl;	
    		cout << "Please choose an operation" << endl;
    		cout << "1. Addition" << endl;
    		cout << "2. Subtraction" << endl;
    		cout << "3. Multiplication" << endl;
    		cout << "4. Division" << endl;
    		cout << "5. Exit calculator" << endl;	
    		cin >> math;
    		cout << endl;
    		switch (math)
    		{ //switch
    			case 1:
    				cout << var1 + var2 << endl;
    				break;
    			case 2:
    				cout << var1 - var2 << endl;
    				break;
    			case 3:
    				cout << var1 * var2 << endl;
    				break;
    			case 4:
    				if (var2 != 0)
    					cout << var1 / var2 << endl;
    				else
    					cout << "ERROR: Cannot divide by zero." << endl;
    				break;
    			case 5:
    				cout << "Thanks for using the calculator!" << endl;
    				return 0;
    		} //switch
    		} //while
    		cout << "Thanks for using the calculator!" << endl;
    	return 0;
    } //main
    
    I needed help in the first place to get it to loop, although I managed to figure that out. And I saw what you meant about the char, it didn't make much sense the way I was doing it originally, so I have edited as such. I changed the approach to exit the calculator to the switch statement, I believe that makes a lot more sense than my earlier code.
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    Glad you got it working. It's still asking for two operators instead of operands, though. Also, initialize math. It could be 5 when your program starts and you'll never get into the loop.

    -Lee
     
  6. iBookG4user thread starter macrumors 604

    iBookG4user

    Joined:
    Jun 27, 2006
    Location:
    Seattle, WA
    #6
    Hmm, I've run into another problem with the little game that I'm trying to write. Everything works correctly except for the random number generator, it always yields "2" for me. :confused:

    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    int main ()
    { //main
    	int bank = 10;
    	int win = 100;
    	int guess;
    	int response;
    	srand(time(0));
    	cout << "your bank is $" << bank << " would you like to play?  It only costs $1." << endl;
    	cout << "Enter '1' for yes or '2' for no." << endl;
    	cin >> response;
    	
    	while (response == 1)
    	{ //while
    		bank--;
    		cout << "Please enter your guess, 1-6." << endl;
    		cin >> guess;
    		int die = (rand() % 6) + 1;
    		if (guess == die)
    		{ //if
    			bank = bank+win;
    			cout << "Winner!  Congratulations, your new bank is $" << bank << ".  Would you like to play again?" << endl;
    			cout << "Enter '1' for yes or '2' for no." << endl;
    			cin >> response;
    		} //if
    		else
    		{ //else
    			cout << "Sorry, you didn't win this time.  The dice roll was " << die << ".  Your new bank is $" << bank << ".  Would you like to play again?" << endl;
    			cout << "Enter '1' for yes or '2' for no." << endl;
    			cin >> response;
    		} //else
    	} //while
    	cout << "Your final bank was $" << bank << ".  Please play again when you have time!" << endl;
    	return 0;
    }//main
    
    EDIT:
    I figured it out finally!
     

    Attached Files:

Share This Page