Question on switch

Program is working just need help in a few areas

Page 1 of 1

7 Replies - 1020 Views - Last Post: 12 August 2009 - 01:12 PM Rate Topic: -----

#1 vwyodapink   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Question on switch

Post icon  Posted 12 August 2009 - 12:03 PM

The program is working already just not fully how the assignment is asking and I am not sure how to finish. It wants me to make sure in the subtraction area to always subtract the smaller of the two numbers from the larger of the two. and also in the division area to always divide the larger number from the smaller one. Am I needing to add in a while statement to make sure it does this or how would you go about this the correct way. Hope this makes sense.

Thanks as always everyone!!!

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{	
	char calc = ' ';
	int num1 = 0;
	int num2 = 0;
	int total = 0;

	//enter input data
	cout << "Type A for addition, S for subtraction, M for multiplication, D for division:";
	cin >> calc;
	calc = toupper(calc);

	switch (calc)
	{
	case 'A':
		cout << "Enter the first number you would like to add:" << endl;
		cin >> num1;
			cout << "Enter the second number you would like to add:" << endl;
			cin >> num2;
			total = num1 + num2;
				cout << "Your total is: " << total << endl;
			break;
	case 'S':
		cout << "Enter the first number you would like to subtract:" << endl;
		cin >> num1;
		cout << "Enter the second number you would like to subtract:" << endl;
		cin >> num2;
		total = num1 - num2;
		cout << "Your total is: " << total << endl;
		break;
	case 'M':
		cout << "Enter the frist number you would like to multiply:" << endl;
		cin >> num1;
		cout << "Enter the second number you would like to multiply:" << endl;
		cin >> num2;
		total = num1 * num2;
		cout << "Your total is: " << total << endl;
		break;
	case 'D':
		cout << "Enter the first number you would like to divide:" << endl;
		cin >> num1;
		cout << "Enter the second number you would like to divide:" << endl;
		cin >> num2;
		total = num1 / num2;
		cout << "Your total is: " << total << endl;
		break;
	default:
		cout << "Error" << endl;
	} //end switch
	

	return 0;
}   //end of main function



Is This A Good Question/Topic? 0
  • +

Replies To: Question on switch

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Question on switch

Posted 12 August 2009 - 12:07 PM

I would think a single if ... else statement could be used for this. Here's an example

case 'S':
		cout << "Enter the first number you would like to subtract:" << endl;
		cin >> num1;
		cout << "Enter the second number you would like to subtract:" << endl;
		cin >> num2;
   
		if(num1 > num2)
			  total = num1 - num2;
		else
			  total = num2 - num1;

		cout << "Your total is: " << total << endl;
		break;




Hope that helps :)
Was This Post Helpful? 1
  • +
  • -

#3 bimbolena   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 50
  • Joined: 09-August 09

Re: Question on switch

Posted 12 August 2009 - 12:08 PM

You mean like this?

		if(num1>num2) total = num1 - num2; else total=num2-num1;
...
		if(num1>num2) total = num1 / num2; else total=num2/num1;


Was This Post Helpful? 0
  • +
  • -

#4 computerfox   User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Question on switch

Posted 12 August 2009 - 12:09 PM

just add for each of those switch statements an if statement to check which is the larger number. that should fix your problem.

hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#5 vwyodapink   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Re: Question on switch

Posted 12 August 2009 - 12:36 PM

Thanks everyone!!!!!! Problem solved
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Question on switch

Posted 12 August 2009 - 12:39 PM

Glad we were able to help you find a solution :)
Was This Post Helpful? 0
  • +
  • -

#7 computerfox   User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Question on switch

Posted 12 August 2009 - 12:42 PM

sweet! don't forget to keep coming back for more knowledge and understanding of C/C++. you can never know enough, well, cept if you're psychocoder here :wink:
Was This Post Helpful? 0
  • +
  • -

#8 vwyodapink   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Re: Question on switch

Posted 12 August 2009 - 01:12 PM

This site has become one of my tops. I tend to come here daily now to read others posts to see if I can learn something from their errors and see if I can spot anything out with my limited knowledge.

I am in a C++ class and a Java class right now.

Fall will be starting Flash, PHP, and more Java.

What is the best way to keep up on a language you may not use all the time? Should I just keep trying to write programs in that language and just keeping the syntax fresh in my head? Or do most people just keep what their primary language is in the brain and then use reference sheets to be reminded of the differences in syntax between the different languages? Just looking to stay sharp and be steadily moving forward. Thanks for the tips and advice
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1