GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 31-Oct-2009, 06:43
TitanGuy TitanGuy is offline
New Member
 
Join Date: Oct 2009
Posts: 29
TitanGuy is on a distinguished road

Do..While Loops


New Project... need help please:

Attached is the instructions.

Here is what I have so far and having trouble:

CPP / C++ / C Code:
#include <iostream>
using namespace std;

//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 a Do..While loop and "if" statements. 
int main()
{
	double total = 0, n = 0;
	char operation =  ' ';
	
	cout << "Current total is 0" << endl;

		//Asking for user input 
			do
			{
			cout << "Enter an operation: + - * / (or enter X to exit): ";
			cin >> operation;
			if (operation == 'X')
				{
					break;
				}
				 
				if ((operation != '+') && (operation != '-') && (operation != '*') && (operation != '/'))
						do 
						{
							cout << "Enter an operation: + - * / (or enter X to exit): ";
							cin >> operation;
						}
						while ((operation != '+') && (operation != '-') && (operation != '*') && (operation != '/'));
				
				cout << "Enter a number. " ;
				cin >> n;
				if (operation == '/') 
					
					do
					{
						cout << "Can not divide by zero!" << endl;
						cout << "Current total is " << total << endl;
					}	
					while (n = 0);

				 else cout << "Current total is ";
					
						if (operation == '+') 
							total = (total + n);
				 
							if (operation == '-') 
								total = (total - n);

								if (operation == '*') 
									total = (total * n);
	
									if (operation == '/') 
										total = (total / n);
									
											
					cout << total << endl;			
			
			}
			while (operation != 'X');	
			 
}
Attached Images
File Type: pdf assignment.pdf (24.9 KB, 5 views)
  #2  
Old 31-Oct-2009, 10:52
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Do..While Loops


Looks like you started out ok and then got tired.
This is YOUR homework but without giving too much away here are a couple of things:
Just get this out of your way (delete it)
CPP / C++ / C Code:
/*  if (operation == '/') 
    {  
      do
      {
        cout << "Can not divide by zero!" << endl;
        cout << "Current total is " << total << endl;
      }    
      while (n = 0);
    }
    else cout << "Current total is "; */
...and just handle that below in the if('/') section.
CPP / C++ / C Code:
    if (operation == '/')
    {
      if(n == 0)
      {
        cout << "    Can't divide by zero! Try again." << endl;
        continue;    // a very useful tool
      }
      total = (total / n);
    }
    cout << "Current total is: "<< total << endl;
Note that I just incorporated the "Current total is: " print into the value print you already had below tha calculations (which is the logical place don't you think?)
Also take note of the "continue;" which jumps to the top of the current looping structure. very handy.

Another suggestion would be to use a do{}while() around the "n" input like you did for the "operation" input.

You will find that you will also need to do something in the event someone would input:
Code:
Enter an operation: + - * / (or enter X to exit): + + or Enter an operation: + - * / (or enter X to exit): + Enter a number. +
See how you do with that and post back if you need to.
  #3  
Old 31-Oct-2009, 11:19
TitanGuy TitanGuy is offline
New Member
 
Join Date: Oct 2009
Posts: 29
TitanGuy is on a distinguished road

Re: Do..While Loops


Thank you so much!
  #4  
Old 31-Oct-2009, 11:25
TitanGuy TitanGuy is offline
New Member
 
Join Date: Oct 2009
Posts: 29
TitanGuy is on a distinguished road

Re: Do..While Loops


I have another question,

I tried this whole program in a different way, and it seems to work great, but the only problem is "+" doesn't work, but all other operations do,

Any ideas?

CPP / C++ / C 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;
		switch (operation)
                {
                        case '+':
                                n += n2;
                                break;
                        case '-':
                                n -= n2;
                                break;
                        case '*':
                                n *= n2;
                                break;
                        case '/':
                if (n2 == 0)
                {
                 cout << "Can not divide by zero!" << endl;
        }
                else
                {
                                n /= n2;
        }
                                break;
}
		cout << "Current Total is " << n << endl;
	} while (operation != 'X');

}
  #5  
Old 31-Oct-2009, 14:27
TitanGuy TitanGuy is offline
New Member
 
Join Date: Oct 2009
Posts: 29
TitanGuy is on a distinguished road

Re: Do..While Loops


Any ideas about the above Program?
  #6  
Old 31-Oct-2009, 16:19
TitanGuy TitanGuy is offline
New Member
 
Join Date: Oct 2009
Posts: 29
TitanGuy is on a distinguished road

Re: Do..While Loops


Actually it works great. Please delete my last program post. Apparently some other students are copying my work.

Thanks
  #7  
Old 13-Nov-2009, 09:11
saqib_ saqib_ is offline
New Member
 
Join Date: Oct 2009
Posts: 12
saqib_ has a little shameless behaviour in the past

Re: Do..While Loops


I think the question was to ask the user to input two integers though not explicitly stated that way in the statement.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
combine 2 struc loops into 1 loop roy918 C++ Forum 4 01-Mar-2007 09:28
Having Problems with Loops cristale10 C++ Forum 2 02-Oct-2006 15:58
nested loops.. need some help.. PeterPan Java Forum 1 21-Mar-2006 22:40
Help with nested for loops...please. sorry newbie keperry C++ Forum 14 16-Oct-2004 12:25
Help using nested for loops dontcare C++ Forum 1 03-Oct-2004 13:33

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:00.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.