-2

I am creating a program which asks user for reply and how many time you want to print reply and display it. I have used a while loop and switch case in the program. But when I store input in variable a with help of std::cin, the same input is not received by switch case. Any help is appreciated.

#include <iostream>

using namespace std;

int a;
int input;
int i=1;
void display()
{
    cout << "Select a choice for reply" << endl;
    cout << "1.Thank You" << endl;
    cout << "2.Welcome" << endl;
    cout << "3.Okay" << endl;
}

int main()
{
    display();
    cout << "Enter Choice" << endl;
    cin >> a;
    input='a';
    switch (input)
    {
        case '1': {
            int x;
            cout << "Enter no. of times  you want to print reply line" << endl;
            cin >> x;
            while (i <= x)
            {
                cout << "Thank you" << endl;
            }

            break;
        }
        case '2': {
            int x;
            cout << "Enter no. of times  you want to print line" << endl;
            cin >> x;
            while (i <= x)
            {
                cout << "Welcome" << endl;
            }

            break;
        }
        case '3': {
            int x;
            cout << "Enter no. of times  you want to print line" << endl;
            cin >> x;
            while (i <= x)
            {
                cout << "okay" << endl;
            }

            break;
        }
        default: {
            cout << "wrong choice" << endl;
        }

        cout << "Thank you for replying" << endl;
    }

    return 0;
}
Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
  • 1
    `case '1'` - um. little chance that is going to work for you. I don't suppose you tried `case 1` (i.e., don't use character literals). And since neither `i` nor `x` are changed in your ensuing `while` loops, it is destined to spin to infinity, even if you fix the first problem. I *strongly* suggest a [good book on C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – WhozCraig Sep 05 '17 at 06:59
  • Please format your code correctly, like the samples in your C++ textbook. – Jabberwocky Sep 05 '17 at 07:02
  • you declared `input` as `integer` then assigned `input='a'`. after that you put `switch(input)`. – kiLLua Sep 05 '17 at 07:03
  • Compile with all warnings and debug info (e.g. `g++ -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/)...) then **use the debugger** (`gdb`) – Basile Starynkevitch Sep 05 '17 at 07:04
  • I think you're confusing digits (`'9'`) with numbers (`9`). – molbdnilo Sep 05 '17 at 07:20

4 Answers4

3

The core Problem is:

input = 'a'

And:

case '1':

What you probably want is:

input = a

case 1:

After all the Compiler should have thrown a warning. But why copy the value after all? Just do

switch(a)

Actually, there are many problems with your code, but I'll let other people elaborate this.

HenrikS
  • 402
  • 3
  • 13
0

Your problem is input='a';, input value will be 97, it is the same if you write input = (int)'a';

Why are you declare 'a' variable? Just use input cin >> input;

Ivan Sheihets
  • 802
  • 8
  • 17
0
#include <iostream>
using namespace std;
int a;
int input;
int i=1;
void display()
{
    cout<<"Select a choice for reply"<<endl;
    cout<<"1.Thank You"<<endl;
    cout<<"2.Welcome"<<endl;
    cout<<"3.Okay"<<endl;
}

int main()
{
    display();
    cout<<"Enter Choice"<<endl;
    cin>>a;
    input=a;
    switch(input)
    {
        case 1 :
        {
            int x;
            cout<<"Enter no. of times  you want to print reply 
            line<<endl;
            cin>>x;
            while(i<=x)
            {
                cout<<"Thank you"<<endl;
                x--;
            }
            break;
        }
        case 2 :
        {
            int x;
            cout<<"Enter no. of times  you want to print line" <<endl;
            cin>>x;
            while(i<=x)
            {
                cout<<"Welcome"<<endl;
                x--;
            }
            break;
        }
        case 3 :
        {
            int x;
            cout<<"Enter no. of times  you want to print line"<<endl;
            cin>>x;
            while(i<=x)
            {
                cout<<"okay"<<endl;
                x--;
            }
            break;
        }
        default:
        {
            cout<<"wrong choice"<<endl;
        }
        cout<<"Thank you for replying"<<endl;
    }
    return 0;
}
xMutzelx
  • 566
  • 8
  • 22
  • Your problem is input='a';, input value will be 97, it is the same if you write input = (int)'a';Also there was no need to take two different variables that is a and input,one was sufficient.But since you declared and used these two variables i didn't removed that from your program.When you write case st. in switch then always remember syntax for integer is (case 1 :)where 1 can be replaced by any number you providing as a choice and syntax for character (case 'a':).Also in while loop you forgot to gave terminating condition(x--;). – yamini jhamnani Sep 05 '17 at 07:37
0

You have several problems in your code:
1) You try to give an integer an char: input = 'a';
To do it right you should use the variable directly: input = a; (without ')

2) In your switch case you compare with '1', '2' (type char) but input is of type integer. Try this:
case 1: and case 2:(without ')

3) You dont increment/decrement i or x in your while loops, they will never stop. Try something like: i++ or x-- somewhere in your loops.

Some remarks:
1) You can use switch case without curly brackets ({ }):

int a = 0;
cin >> 1;
switch(a)
{
    case 1:
        break;
    case 2:
        int tmp = 0;
        cout<<"tmp: "<<tmp<<endl;
        break;
    default:
        break;
}

2) Try to avoid global variables, they are in most cases not necessary and bad practice.

3) You dont need your variable a, you can use input directly, like:

cout << "Enter Choice" << endl;
cin >> input;
switch (input)

4) Last but not least I recommend you to read some good c++ books, it is realy worth it. (Good books)

xMutzelx
  • 566
  • 8
  • 22