5 Replies - 1680 Views - Last Post: 25 January 2009 - 06:55 PM Rate Topic: -----

#1 mono15591  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 406
  • Joined: 05-November 08

Switch statement problem

Posted 25 January 2009 - 06:38 PM

		switch(operation){
			case "*":
				multiplication(firstNumber,secondNumber);
			case "+":
				addition(firstNumber,secondNumber);
			case "-":
				subtraction(firstNumber,secondNumber);
			case "/":
				division(firstNumber,secondNumber);
			  break;
			default:
				cout<< "Not an option!  (restart now!)";
		}



This isn't working for me
these are the errors I'm getting...

error C2051: case expression not constant
error C2051: case expression not constant
error C2051: case expression not constant
error C2051: case expression not constant
warning C4065: switch statement contains 'default' but no 'case' labels

Does a switch statement not work with strings and chars?
should I use an if statement?

Is This A Good Question/Topic? 0
  • +

Replies To: Switch statement problem

#2 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Switch statement problem

Posted 25 January 2009 - 06:40 PM

You're trying to use double quotation marks, which is a string constant.
Try this:

		switch(operation){
			case '*':
				multiplication(firstNumber,secondNumber);
			case '+':
				addition(firstNumber,secondNumber);
			case '-':
				subtraction(firstNumber,secondNumber);
			case '/':
				division(firstNumber,secondNumber);
			  break;
			default:
				cout<< "Not an option!  (restart now!)";
		}


Also, the reason you're getting the "warning" is because you aren't using break's after the case.
If your switch gets the asterisk (*), it'll execute that function, then the function below it, and the one below that, etc.

By putting a break after multiplication(firstNumber,secondNumber); like so:
multiplication(firstNumber,secondNumber); break; it'll execute only that one argument, and then exit the switch statement.

This post has been edited by Hyper: 25 January 2009 - 06:42 PM

Was This Post Helpful? 0
  • +
  • -

#3 mono15591  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 406
  • Joined: 05-November 08

Re: Switch statement problem

Posted 25 January 2009 - 06:48 PM

Yea I found that out TY anyways though


but now I have another problem...

#include <iostream>
#include <string>
using namespace std;

void addition(int a, int b)
{
	int answer = a+b;
	cout<< "Your answer is: " << answer;
	cout<< "\n\n********************\n\n";
}

void subtraction(int a, int b)
{
	int answer = a-b;
	cout<< "Your answer is: " << answer;
	cout<< "\n\n********************\n\n";
}

void multiplication(int a, int b)
{
	int answer = a*b;
	cout<< "Your answer is: " << answer;
	cout<< "\n\n********************\n\n";
}

void division(int a, int b)
{
	double answer = a/b;
	cout<< "Your answer is: " << answer;
	cout<< "\n\n********************\n\n";
}

int main(int argc, char* argv[])
{
	for (int i=0; i<100000; ++i)
	{
		cout<< "Enter the first number: ";
		int firstNumber;
		cin>> firstNumber;
		
		cout<< "Enter the second number: ";
		int secondNumber;
		cin>> secondNumber;

		cout<< "Choose you operation( +,-,*,/ ) ";
		char operation;
		cin>> operation;

		switch(operation){
			case '*':
				multiplication(firstNumber,secondNumber);
			case '+':
				addition(firstNumber,secondNumber);
			case '-':
				subtraction(firstNumber,secondNumber);
			case '/':
				division(firstNumber,secondNumber);
			  break;
			default:
				cout<< "Not an option!  (restart now!)";
		}
	}
	return 0;
}


Well when I run this it gives you the answer but it also repeats some extra stuff at the end. Idk why though

also is there a way to hide my functions somewhere else so my code isn't so long?
Was This Post Helpful? 0
  • +
  • -

#4 Blade2021  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 204
  • Joined: 31-October 08

Re: Switch statement problem

Posted 25 January 2009 - 06:50 PM

Quote

Also, the reason you're getting the "warning" is because you aren't using break's after the case.
If your switch gets the asterisk (*), it'll execute that function, then the function below it, and the one below that, etc.

By putting a break after multiplication(firstNumber,secondNumber); like so:
multiplication(firstNumber,secondNumber); break; it'll execute only that one argument, and then exit the switch statement.


Also when I started C++ and started my calculator I used YouTube (Visual Learning) here is a link if you want it. It shows a slightly diffrent way to program it then the way you have. Click here

This post has been edited by Blade2021: 25 January 2009 - 06:53 PM

Was This Post Helpful? 0
  • +
  • -

#5 mono15591  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 406
  • Joined: 05-November 08

Re: Switch statement problem

Posted 25 January 2009 - 06:52 PM

I'm a bad reader :P
Thank you!
Was This Post Helpful? 0
  • +
  • -

#6 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Switch statement problem

Posted 25 January 2009 - 06:55 PM

"also is there a way to hide my functions somewhere else so my code isn't so long?"

#include "HeaderFileName.h"

You put the functions n' stuff in the header file.
Put the #include above the main and presto, it'll work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1