-2
class password
{
private 
string password;

public:
void input();
}

void password::input()
{
 cout<<" Enter your string :";
getline(cin,password);
}


void main()
{

getline(cin,password);// taking inout first time
cin >> ch;// switch case input

switch (ch)
{
case 1:
       p.input(); // taking inout second time
       break;
case 2:
        // everything works fine in case 2
                
default:cout << "\n ... ";
}
}

As you can see above the code where I made a class called password with a member function input which when called takes input from user(string inputs).

Now there is no problem in compilation of the code, the problem is when I take input from the user . For first time it takes input properly but as soon as user inputs 1 for switch case as i have commented up there line of control comes in switch and if user tries to enter a string it takes one single word instead of whole string saying progran finished

P.s I have not written the whole program right here

Also, getline function works properly when i take first input but something gets messy inside the switch case for (ch=1)

Please provide me with some solution for this which would solve this problem.

  • `case 1:` => `case '1':` same for 2. Also note: https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction – πάντα ῥεῖ Dec 08 '20 at 17:23
  • Please edit this into a minimal reproducible example. This code doesn't compile for reasons that probably have nothing to do with the problems you're experiencing with your `input` function. At best I can _guess_ that mixing `operator>>` and `getline` has led you to the [same problems](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) other people have had mixing these input methods. – Nathan Pierson Dec 08 '20 at 17:23
  • I'll try working on it and get back to you soon if it works. – Dishant Shah Dec 08 '20 at 18:17

1 Answers1

-2

I believe the issue is on your code with switch-case statement where you are using 1, for the compiler it stands for true(which is always true because the compiler only knows 0 and 1, 0 is false and 1 is true) flag. Try with "ONE" and "TWO" in your switch statement. I hope it will work.

  • I understand what you are saying but the compler right here in this case is properly identifying difference between an integer '1' and true '1' even it properly executes the statements being written inside the input function. But can you please tell me that even if compiler if considering '1' as true and the ser inputs '1' how is it affecting the input function because at the end the criteria for executing case 1 is met... – Dishant Shah Dec 08 '20 at 18:10