views:

157

answers:

3

I am new to C++ and I have some confusion regarding this program. I am running this in Visual Studio 2008 as win32 console application.

    #include <iomanip>
    #include <cmath>
    #include <string>
    using namespace std;
    #define PI 3.14
    int l=1;
    int x;
    void main()
    {
        do
        {
            cout << "choose 1";
            cout << "choose 2";
            cin >> x;
            switch(x)
            {
                case 1:
                    cout << "action 1";
                    break;
                case 2:
                    cout << "action 2";
                    break;
                default:
                    cout << "unknown command";
                    break;
            }
        } while (l=1)
    }

When I run this program and type anything else than 1 or 2, it is not displaying the default option in the switch. I am unable to figure out the problem. How do I fix this problem?

+2  A: 

This is a better first attempt, which does perform all three cases depending on the input. The original version you gave didn't even compile due to various errors.

I suggest you start from this one:

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
int main()
{
    int x;
    do
    {
        cout<<"choose 1 or 2: ";
        cin>>x;
        switch(x)
        {
            case 1:
                cout<<"action 1"<<endl;
                break;
            case 2:
                cout<<"action 2"<<endl;
                break;
            default:
                cout<<"unknown command"<<endl;
                break;
        }
    } while(1==1);
    return 0;
}

Here's a sample run:

choose 1 or 2: 1
action 1
choose 1 or 2: 2
action 2
choose 1 or 2: 3
unknown command
choose 1 or 2: ^C

There are still problems even with the fixed code such as when you enter a non-numeric. You really should be getting strings from standard input and checking them for validity before converting to a number.

To handle non-numerics, this would be a good start:

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
int main()
{
    string x;
    do
    {
        cout<<"choose 1 or 2: ";
        cin>>x;
        if (!isdigit(x[0])) {
            cout<<"non-numeric command"<<endl;
        } else {
            switch(x[0])
            {
                case '1':
                    cout<<"action 1"<<endl;
                    break;
                case '2':
                    cout<<"action 2"<<endl;
                    break;
                default:
                    cout<<"unknown command"<<endl;
                    break;
           }
       }
    } while(1==1);
    return 0;
}
paxdiablo
A: 

This Works - only for int (if you type a char.. it will get messy)

#include <iomanip>
#include <cmath>
#include <string>
#include <iostream> 
using namespace std;
#define PI 3.14 int l=1; void main() {  int x;
    do
    {
        cout<<"choose 1";
        cout<<"choose 2";
        cin>>x;
        switch(x)
        {
            case 1:
                cout<<"action 1";
                break;
            case 2:
                cout<<"action 2";
                break;
            default:
                cout<<"unknown command";
                break;
        }
    } while(1==1);

}

How to handle input:

// iostream_cin.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;

int main()
{
   int x;
   cout << "enter choice:";
   cin >> x;
   while (x < 1 || x > 4)
   {
      cout << "Invalid choice, try again:";
      cin >> x;
      // not a numeric character, probably
      // clear the failure and pull off the non-numeric character
      if (cin.fail())
      {
         cin.clear();
         char c;
         cin >> c;
      }
   }
}
Dani
then how to handle char types? any error handling?
HPro
make x a char and than parse it's value or limit the input legnthI've added something to the edit from msdn - how to handle cin errors:
Dani
A: 
  1. I cannot compile in my VS2008 unless I include for the cin, cout to work.
  2. After that it works fine - it prints 'unknown command' for values other than 1 and 2.
Sesh
you mean for char values also? what happens if you give 'c' or any other alphabet?
HPro
Yes. Basically in C++, if you define a variable outside the main, they will be initialized. So in your case the variable 'x' should have been initialized to zero.Now char input fails, however the value remains zero, so it should always hit the 'default' section of the switch.
Sesh