Thread: do.. while loops

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    30

    do.. while loops

    whats up fellas. another assignment that is due somewhat soon that i have some questions on. the assignment description is:
    Write a simple calculator program. Ask the user if s/he wants to add ('+') subtract ('-'), multiply ('*') or divide ('/') and then ask for the number. Perform the operation with the given number. Start the "display" at zero. Stop when the user enters 'X' (for exit). Use Do.. While and if statements.

    .... so far i have this:

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	double n;
    	char operation;
    
    	cout << "Current Total is 0" << endl;
    	do
    	{
    		cout << "Enter an operation: + - * / (or enter X to exit):";
    		cin >> operation;
    		cout << "Enter a number: ";
    		cin >> n;
    		cout << "Current total is " << n << endl;
    
    	} while (operation != 'X');
    }
    the output is coming out somewhat nice, but i am trying to figure out how to incorporate the operation into the output of the numbers. (ie: if user enters +, and then enters the number 20, the total displayed would be 20. but, after that, i am not sure how the next operation the user enters can effect the previously entered number.

    trying to explain this differently, using the same example, if the user entered +, then entered 20. the number displayed is 20. then, if the user entered the * operation next, and entered 2 for the next number, the 'new' current total should be 40. this is the part i am not sure how to do.

    if any of you guys know how to help me with this, it would be awesome. and if you would like me to try to explain it a little better, i will do so as well.

    again, thanks for any help guys.

  2. #2
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    You will need IF-statements to check what the user has inputted and calculate the new value accordingly. You may also want to put in another input option that it resets n to zero. They key here is not to put n in the cin>> (swap it with another temporary value so it doesn't mess up what you've already got).

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    30
    thanks for the quick response. i think i am on the right track, but being that the operation comes before the first number entered, the 'new' number displayed is coming out with something all screwey (-1#IND).
    i remember you mentioning something about making a temporary double number, and setting it to zero, im not sure how to do that. if i make the cin >> n2, then all the 'n = ..." wont work because of not knowing which double to use.


    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	double n, n2;
    	char operation;
    
    	cout << "Current Total is 0" << endl;
    	do
    	{
    		cout << "Enter an operation: + - * / (or enter X to exit):";
    		cin >> operation;
    		cout << "Enter a number: ";
    		cin >> n;
    		if (operation = '+') n = n + n;
    		if (operation = '-') n = n - n;
    		if (operation = '*') n = n * n;
    		if (operation = '/') n = n / n;
    		cout << "Current total is " << n << endl;
    
    	} while (operation != 'X');
    }
    thanks again.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    = is assignment, == is comparison
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    30
    i have been working on this forever. i looked in my book everywhere and cant seem to find how to finish this. the if statements work, but i cant seem to find out how to make the previous number be used for the operations following.

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	double n, n2=0;
    	char operation;
    
    	
    	cout << "Current Total is " << n2 << endl;
    	do
    	{
    		
    		cout << "Enter an operation: + - * / (or enter X to exit):";
    		cin >> operation;
    		cout << "Enter a number: ";
    		cin >> n;
    		if (operation == '+')
    		{
    			n = n + n;
    			cout << "Current Total is " << n << endl;
    		}
    		if (operation == '-')
    		{
    			n = n - n;
    			cout << "Current Total is " << n << endl;
    		}
    		if (operation == '*')
    		{
    			n = n * n;
    			cout << "Current Total is " << n << endl;
    		}
    		if (operation == '/')
    		{
    			n = n / n;
    			cout << "Current Total is " << n << endl;
    		}
    
    		
    	} while (operation != 'X');
    }
    i think i am getting on the right track.

    thanks for any help.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should enter the scond number as well.
    don't you want to ask user to enter it?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    30
    yeah, the user is supposed to enter the number after putting in the desired operation. so if the number was 40, and the user selected '-' and put in the number 3, the new number should be 37.

    i have been racking my brain forever on this stuff. and it is due tomorrow morning. thank you for the continued help. this c++ stuff is tough.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Something like
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	double n, n2=0;
    	char operation;
    
    	
    	cout << "Enter a first operand: ";
    	cin >> n;
    	do
    	{
    		
    		cout << "Enter an operation: + - * / (or enter X to exit):";
    		cin >> operation;
    		if(operation == 'X')
    			break;
    		cout << "Enter a second operand: ";
    		cin >> n2;
    		if (operation == '+')
    		{
    			n += n2;
    		}
    		else if (operation == '-')
    		{
    			n -= n2;
    		}
    		else if (operation == '*')
    		{
    			n *= n2;
    		}
    		else if (operation == '/')
    		{
    			n /= n2;
    		}
    		cout << "Current Total is " << n << endl;
    
    		
    	} while (operation != 'X');
    
    	cout << "The final result is " << n << endl;
    	return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    30
    youre great man. thanks a lot. there is only 1 problem. when the user enters '/' and then enters the # 0, it is supposed to come up with a message and display the previous number.

    would that be 2 separate if statements, because i feel as though i would need more than 1, under each statement. again, sorry to be bothersome, but i am a beginner and trying to learn on the go.

    thanks again.

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    30
    i tried doing something like this:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	double n=0, n2=0;
    	char operation;
    
    	cout << "Current Total is " << n << endl;
    
    	do
    	{
    esc: cout << "Enter an operation + - * / (or enter X to exit):" << endl;
    		cin >> operation;
    		if (operation == 'X')
    			break;
    		cout << "Enter a number: " << endl;
    		cin >> n2;
    		if (operation == '+')
    			n += n2;
    		else if (operation == '-')
    			n -= n2;
    		else if (operation == '*')
    			n *= n2;
    		else if (operation == '/')
    			n /= n2;
    		else if ((operation == '/') && (n2 = 0)) 
    		{
    			goto esc;
    			cout << "error";
    		}
    		cout << "Current Total is " << n << endl;
    	} while (operation != 'X');
    
    
    
    }
    that doesnt seem to work either.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should change (n2 = 0) to (n2 == 0). Furthermore, you should re-order the statements, otherwise that block will never be reached since for it to be reached, both (operation == '/') and (n2 == 0) must be true, but if that is so, then (operation == '/') is true, hence control enters the block with n /= n2; instead.

    Not only that, but just say no to goto until you have enough experience to discern the special cases where it can be tolerated. In this case, you can either use continue, or simply let control pass to the statement that prints the current total since that makes sense.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    also using == on doubles is not recomended...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    30
    i did what you said, and it is still coming up with what has been coming up. funny enough, my professor told me the exact same thing you (laserlight) told me. i switched them and made it n2 == instead of =, but i am still getting the same problem.

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	double n=0, n2=0;
    	char operation;
    
    	cout << "Current Total is " << n << endl;
    
    	do
    	{
    		cout << "Enter an operation + - * / (or enter X to exit):" << endl;
    		cin >> operation;
    		if (operation == 'X')
    			break;
    		cout << "Enter a number: " << endl;
    		cin >> n2;
    		if (operation == '+')
    			n += n2;
    		else if (operation == '-')
    			n -= n2;
    		else if (operation == '*')
    			n *= n2;
    		else if (operation == '/')
    			n /= n2;
    		else if ((n2 == 0) && (operation == '/'))
    		{
    			cout << "error";
    		}
    		cout << "Current Total is " << n << endl;
    	} while (operation != 'X');
    
    
    
    }
    again, thanks for any help.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is still there: trace the code when operation == '/'. You will see that the error message can never be printed, because the only case where it can be printed is when operation != '/' && n2 == 0 && operation == '/', which is a contradiction.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    30
    ahhh, nevermind. i figured that problem out. i had to switch the order of the else if statements (put the one with the error message before just the one with the / symbol).

    now i have to modify this to work with switch.. case programs. ill start working on it and post if i need some help (which i am sure i will need).

    thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  4. for loops in C
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-15-2001, 05:09 PM
  5. exiting loops with ease?
    By KingRuss in forum Game Programming
    Replies: 3
    Last Post: 09-24-2001, 08:46 PM