user216087
user216087

Reputation:

Basic question in C++

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?

Upvotes: 0

Views: 267

Answers (3)

paxdiablo
paxdiablo

Reputation: 881153

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

Upvotes: 2

Sesh
Sesh

Reputation: 6182

  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.

Upvotes: 0

Dani
Dani

Reputation: 15069

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

Upvotes: 0

Related Questions