5 Replies - 941 Views - Last Post: 12 July 2009 - 01:51 AM Rate Topic: -----

#1 mvb5142   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 29-June 09

Please help trying to figure out this program using a switch..case sta

Posted 10 July 2009 - 07:14 PM

/* 
	Ok so I'm trying to w rite a simple calculator program, asking the user if s/he wants to 
	add ('+') subtract ('-'), multiply ('*') or divide ('/') and then 
	asking for the number. 

		I then want to perform the operation with the given number from the user, start the "display" at zero. 
		Stopping when the user enters 'X' (for exit). A switch..case statement is required to handle the user input (+,-,*,/,X). 

*/

Here is what I've come up with so far but it just continues to ask for the user to enter an operation instead of going on to entering a number...Please help



#include <cstdlib>
#include <iostream>
 
using namespace std;
 
int main()
{
	double num = 0;
	double num2;
	char answer;
   
	cout<<"Current total is " << num <<"\n";;
	

	for (;;)
	{
		do 
		{
			cout<<"Enter an operation: + - * / (or enter X to exit)\n";
			cin>> answer;
		} 
	
		while (answer != 'X');
	
		if (answer == 'X') break;
	
		switch (answer) 
		{
					  case '+':
				cout<<"Enter a number:\n ";
				cin>>num2;
				cout<<"Current total is\n " << num + num2 <<"\n";
								cout<<"\n";
								break;

					 case '-':
					cout<<"Enter a number:\n ";
				cin>>num2;
								cout<<"Current total is\n " << num - num2 <<"\n";
								cout<<"\n";
								break;

					case '*':
			   cout<<"Enter a number:\n ";
				   cin>>num2;
						   cout<<"Current total is\n " << num * num2 <<"\n";
						   cout<<"\n";
						   break;
		   
					case '/':
				cout<<"Enter a number:\n ";
				cin>>num2;
							cout<<"Current total is\n " << num / num2 <<"\n";
							cout<<"\n";
							break;

					default:
						   cout<<"That is not an option";
					
		}
 
	   }

return 0;
}


Mod edit - Please :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Please help trying to figure out this program using a switch..case sta

#2 ccubed   User is offline

  • It's That Guy
  • member icon

Reputation: 164
  • View blog
  • Posts: 1,416
  • Joined: 13-June 08

Re: Please help trying to figure out this program using a switch..case sta

Posted 10 July 2009 - 07:26 PM

use [code] tags please! :P

Also, it's because you have a do loop with no end condition. The do loop is not necessary here since you're only asking for the operation at the beginning of each for loop.
Was This Post Helpful? 0
  • +
  • -

#3 apw5020   User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: Please help trying to figure out this program using a switch..case sta

Posted 10 July 2009 - 07:49 PM

Well, he does have an end condition while (answer != 'X'); but it is not in the right place. A do-while loop would be a good idea here if you know where to put it. Then you can get rid of your for loop.

Change your do-while loop so that its body encloses the entire switch statement, then eliminate the for loop.

This post has been edited by apw5020: 10 July 2009 - 08:43 PM

Was This Post Helpful? 0
  • +
  • -

#4 mvb5142   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 29-June 09

Re: Please help trying to figure out this program using a switch..case sta

Posted 10 July 2009 - 08:17 PM

I'm fairly new to C++ programming, I'm more familiar with Java and I'm trying to teach myself

But the output I keep getting is

"Current total is 0
Enter an operation: + - * / (or enter X to exit)
(Then I would enter an answer) +
Enter an operation: + - * / (or enter X to exit)"

But the output I would like to get is

i.e.

"Current total is 0
Enter an operation: + - * / (or enter X to exit)
(Then I would enter an answer) +
Enter a number: (Then I would enter a number) 6
Current total is 6
(then I want to continue using 6 as the next total)
Enter an operation: + - * / (or enter X to exit) *
Enter a number: 4
Current total is 24
Enter an operation: + - * / (or enter X to exit) /
Enter a number: 3
Current total is 8
Enter an operation: + - * / (or enter X to exit) X
Press any key to continue."

This is what I would like for my program to look like but the code I have doesn't give me that and I can't seem to figure out why.
Could I be missing a counter?
Was This Post Helpful? 0
  • +
  • -

#5 apw5020   User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: Please help trying to figure out this program using a switch..case sta

Posted 10 July 2009 - 08:40 PM

Here, you want to do this:
#include <cstdlib>
#include <iostream>
 
using namespace std;
 
int main()
{
	double num = 0;
	double num2;
	char answer;
   
	cout<<"Current total is " << num <<"\n";;

		do 
		{
			cout<<"Enter an operation: + - * / (or enter X to exit)\n";
			cin>> answer;

   
		if (answer == 'X') 
		break;
	
		switch (answer) 
		{
					  case '+':
				cout<<"Enter a number:\n ";
				cin>>num2;
				cout<<"Current total is\n " << num + num2 <<"\n";
								cout<<"\n";
								break;

					 case '-':
					cout<<"Enter a number:\n ";
				cin>>num2;
								cout<<"Current total is\n " << num - num2 <<"\n";
								cout<<"\n";
								break;

					case '*':
			   cout<<"Enter a number:\n ";
				   cin>>num2;
						   cout<<"Current total is\n " << num * num2 <<"\n";
						   cout<<"\n";
						   break;
		   
					case '/':
				cout<<"Enter a number:\n ";
				cin>>num2;
							cout<<"Current total is\n " << num / num2 <<"\n";
							cout<<"\n";
							break;

					default:
						   cout<<"That is not an option";
					
		}
		} 
	
		while (answer != 'X');

return 0;
}


This post has been edited by apw5020: 10 July 2009 - 08:41 PM

Was This Post Helpful? 0
  • +
  • -

#6 blackiegray   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-July 09

Re: Please help trying to figure out this program using a switch..case sta

Posted 12 July 2009 - 01:51 AM

View Postapw5020, on 10 Jul, 2009 - 07:40 PM, said:

Here, you want to do this:
#include <cstdlib>
#include <iostream>
 
using namespace std;
 
int main()
{
	double num = 0;
	double num2;
	char answer;
   
	cout<<"Current total is " << num <<"\n";;

		do 
		{
			cout<<"Enter an operation: + - * / (or enter X to exit)\n";
			cin>> answer;

   
		if (answer == 'X') 
		break;
	
		switch (answer) 
		{
					  case '+':
				cout<<"Enter a number:\n ";
				cin>>num2;
				cout<<"Current total is\n " << num + num2 <<"\n";
								cout<<"\n";
								break;

					 case '-':
					cout<<"Enter a number:\n ";
				cin>>num2;
								cout<<"Current total is\n " << num - num2 <<"\n";
								cout<<"\n";
								break;

					case '*':
			   cout<<"Enter a number:\n ";
				   cin>>num2;
						   cout<<"Current total is\n " << num * num2 <<"\n";
						   cout<<"\n";
						   break;
		   
					case '/':
				cout<<"Enter a number:\n ";
				cin>>num2;
							cout<<"Current total is\n " << num / num2 <<"\n";
							cout<<"\n";
							break;

					default:
						   cout<<"That is not an option";
					
		}
		} 
	
		while (answer != 'X');

return 0;
}



Hey, I've just started learning programming and regularly trawl this site for info, but today's the day I make my first posting (go me! :D ). Anyhoo enough of the self praise.

I've modded the above code which seems almost correct except that it would default your number back to "0" at the next loop:

#include <iostream>
#include <cstdlib>

using namespace std;



 
int main()
{
	double num = 0;
	double num2 = 0;
	double total = 0;
	char answer;
   
	do 
		{
	num = total;
	cout<<"Current total is " << total <<"\n";;

		
			cout<<"Enter an operation: + - * / (or enter X to exit)\n";
			cin>> answer;

   
		if (answer == 'X') 
		break;
	
		switch (answer) 
		{
				case '+':
				cout<<"Enter a number:\n ";
				cin>>num2;
				total = num + num2;
				cout<<"\n"
				break;

				case '-':
				cout<<"Enter a number:\n ";
				cin>>num2;
				total = num - num2;
				cout<<"\n";
				break;

				case '*':
				cout<<"Enter a number:\n ";
				cin>>num2;
				total = num * num2;
				cout<<"\n";
				break;
		   
				case '/':
				cout<<"Enter a number:\n ";
				cin>>num2;
				total = num / num2;			
				cout<<"\n";
				break;

				default:
				cout<<"That is not an option";
					
		}
		} 
	
		while (answer != 'X');

system("pause");

return 0;
}




I put the "current total" inside the switch statement so that you don't need to add a separate cout at the end of each case, also added another double called "total", this is where we store the answer to the calculation.
At the start of the switch we call the variable "total" and assign its value to "num", that way when the calculation is getting done, it will use the answer of the previous calculation as its first variable (num) and the input from user (num2).

I also initialised all the doubles before I started as I read that it's safer to do so.

Hope this helps, obviously if someone else comes along with an easier solution, I'd go with them, I'm only 2 months in myself!

Cheers

JB
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1