#include <iostream>
#include <string>
using namespace std;
int main()
{
string myString;
int x;
cout << "please enter your name.. \n";
getline(cin, myString);
cout << "Hello " << myString << ". \n";
cout << " In what year were you born?.\n";
cin >> x;
cout << "So that makes you " << (2012 - x) << " years old. \n";
switch( x)
{
case 29:
cout << " Stafford is " << x << " Too! \n";
break;
case 26:
cout << " Sammy Sosa " << x << " Too! \n";
break;
default:
cout << " I cannot make any comparrison " << endl;
break;
}
return 0;
}
3 Replies - 204 Views - Last Post: 23 July 2012 - 05:20 PM
#1
Not getting wanted results with switch case statement.
Posted 23 July 2012 - 04:36 PM
Hello all!, I am just learning the C++ language, and have seemed to have run into a road block. I am trying to have the user input data and have that data pass through the switch case statement, particularly the the last cout. When I enter the numerical value for 'x' in this case the "year" it does in fact do (2012 - x), but it doesn't carry that new value for 'x' to the switch statement, rather it just tries to pass the cin input.Thanks all.
Replies To: Not getting wanted results with switch case statement.
#2
Re: Not getting wanted results with switch case statement.
Posted 23 July 2012 - 04:41 PM
Shouldn't it be
Or if you want x to be 29 you need to do
2012-x doesn't change the value of x.
switch(2012-x)on line 20?
Or if you want x to be 29 you need to do
x = 2012 - x;
2012-x doesn't change the value of x.
This post has been edited by simeesta: 23 July 2012 - 04:43 PM
#3
Re: Not getting wanted results with switch case statement.
Posted 23 July 2012 - 04:53 PM
Hello and thank you for the prompt response, I tell you that sometimes the most simplest things can become so hard, especially when your learning something new. Thanks mate I've only just started coding C++ yesterday.
#4
Re: Not getting wanted results with switch case statement.
Posted 23 July 2012 - 05:20 PM
Welcome to rewarding and frustrating world that C++ opens up for you!
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|