3 Replies - 294 Views - Last Post: 23 July 2012 - 05:20 PM Rate Topic: -----

#1 HiTTman  Icon User is offline

  • New D.I.C Head

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

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.

#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;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Not getting wanted results with switch case statement.

#2 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 218
  • View blog
  • Posts: 591
  • Joined: 04-August 09

Re: Not getting wanted results with switch case statement.

Posted 23 July 2012 - 04:41 PM

Shouldn't it be
 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

Was This Post Helpful? 1
  • +
  • -

#3 HiTTman  Icon User is offline

  • New D.I.C Head

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

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.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3470
  • View blog
  • Posts: 10,703
  • Joined: 05-May 12

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!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1