do.. while loops

This is a discussion on do.. while loops within the C++ Programming forums, part of the General Programming Boards category; whats up fellas. another assignment that is due somewhat soon that i have some questions on. the assignment description is: ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    = is assignment, == is comparison
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    you should enter the scond number as well.
    don't you want to ask user to enter it?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    21,451
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    also using == on doubles is not recomended...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    21,451
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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.

Page 1 of 2 12 LastLast
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, 12:18 AM
  3. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03: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

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