cin.getline Not working with switch statement [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Here is my piece of code from my program, cin.getline is not working with switch statement but without switch statement it works, when I use it with switch statement it just skips the input So in my code I have commented cin.getline and replaced it with cin>>userData;
Please tell me what is the problem When I use it with switch statement.
switch(option)
{
case 1:
cout<<"Enter string "<<endl;
//cin.getline(userData, 100);
cin.getline(userData,100);

It is likely that there is an \n in your input stream, from previous code that you unfortunately don't show in the question, and you need to flush it for cin.getline() to work.
The accepted wisdom is to use ignore:
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.getline(userData,100);

Your code works
#include <iostream>
int main(){
char userData[100];
auto option = 1;
switch(option) {
case 1:
std::cout << "Enter string" << std::endl;
std::cin.getline(userData, 100);
}
std::cout << userData;
return 0;
}
There must be something you're not showing.

Related

How to make a console application close automatically after command in C++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I would like to know How to make a console application close automatically after command in C++?
I want my program to ask "1) continue 2) Exit" and obviously proceed with the selected choice.
This is what I got so far:
#include <iostream>
int main() {
int choice;
std::cout << "1) continue 2) Exit. \n\n";
std::cout << std::endl;
std::cin >> choice;
std::cout << std::endl;
if (choice == 1) {
std::cout << " choice 1 selected, let's continue!";
}
if (choice != 1) {
//then close automatically?
}
}
If you really want to shut your window automatically when debugging stops,
"To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops."
Edit:
I forgot to mention that this is for Visual Studio 2019, Community Edition.

C++ while (true) vs while (!something) [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
So let's say we have a bit of code in which we want the user to say something specific, but we want to give them infinite tries if they get it wrong. I would use a do while loop.
#include <iostream>
#include <string>
int main() {
std::string input;
std::cout<<"Enter yes or no: ";
do {
std::cin>>input;
if (input == "yes") {
break;
} else if (input == "no") {
break;
} else {
std::cout<<std::endl<<"Enter yes or no: ";
}
} while (true);
}
But, I have been told that it is bad practice to use while (true);, and, (in this case), I should instead use while (input != "yes" && input != "no");. Which one of them is correct?
std::cout<<"Thanks!";
The loop should test for input-failure (you forgot), and then whether you have to loop on. Thus, it should be:
while ((std::cin >> input) && input != "yes" && input != "no")
std::cout << "\nEnter yes or no: ";
You shouldn't use std::endl when you don't need an explicit flush.
Or, better yet, encapsulate it in a re-usable function, and throw an exception on input-failure.

Click the alphabet twice? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
void password()
{
int i=0, j;
char str[30], ch;
gotoxy (23,10);
cout<< "Welcome to our project! \n";
delay (800);
gotoxy (23,11);
cout<< "Please enter the password = ";
while ((ch=getch())!=13)
{
ch=getch();
str[i]=ch;
i++;
clrscr();
gotoxy (20,20);
cout<< "Enter Password = ";
for (j=0; j<i; j++)
cout<< "*";
}
str[i]='\0';
if (strcmp(str,"script")!=0)
{
cout<< endl;
cout<< "Incorrect Password! \n";
cout<< "Access Denied! \n";
getche();
exit (0);
}
}
i made this a while back, and i came across a problem. So the problem is that, there are no errors whatsoever when I compile the program and run it. The only issue is that when it asks me to enter the password for the script, I have to PRESS every alphabet TWICE and it shows up once (for example, password is TOLS, I have to press it like TTOOLLSS and it shows up as TOLS). How can I fix it?
Remove the line:
ch=getch();
since you already got the char in the line:
while ((ch=getch())!=13)

What type of loop would I wrap around this statement? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
For some reason I'm getting extremely confused as to what type of loop I should be wrapping around this switch statement. If I have the switch statement below and I want the user to be able to keep inputting a keystroke until it hits one of the cases which type of loop is best to use for this?
Thanks,
int input;
cin >> input;
switch( input )
{
case 1:
playgame();
break;
case 2:
loadgame();
break;
case 3:
Multiplayer();
break;
case 4:
cout << "Thanks, exitting now \n";
break;
default:
cout << "error \n";
cin >> input;
break;
}
There is missing some stop condition.
int input = 0; // make a habit of initialising all variables
bool again = true;
while(again) { // could use input != 4 (stop value)
cin >> input;
...
case 4: // magic numbers are bad, maybe use enum for the cases.
cout << "Thanks, exiting now \n";
again = false; // <----- else you wont get out.
break;
...
}
I would recommend a do while loop because you can ensure that the loop was executed at least 1 full time before exiting. Also, since you don't know how many times the user might iterate through the loop, a For loop is not appropriate.

How to return to my Menu C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Here's my program , iam so sorry because iam just a noobie so i would like to ask how can i return to the menu because iam using a switch statement and i want to ask the user after he/she finish to convert . see the comment in the program.
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
int main()
{
int option;
float C,F,result;
char rpt;
cout<<"Temperature Converter"
<<"\n1.Celsius to Fahrenheit"
<<"\n2.Fahrenheit to Celsius"
<<"\n3.Exit"
<<"\n\nEnter a number from 1 to 3: ";
cin>>option;
switch (option)
{
case 1:
system ("CLS");
cout<<"Celsius to Fahrenheit!"<<endl;
cout<<"\nEnter Celsius: ";
cin>>C;
result=C*9/5+32;
cout<<C<<" Celsius is equivalent to "<<result<<" Fahrenheit."<<endl<<endl;
cout<<"Would you like to convert again? Type Y to try again OR N to exit. : ";
cin>>rpt;
if (rpt='Y')
//What Code should i put here?
break;
The code asking whether the user wants to repeat belongs after the switch block. Also you should be using the == operator for comparison. So something like
do {
// Get menu choice
...
switch (option)
{
case 1:
...
break;
case 2:
...
break;
...
}
cout<<"Would you like to convert again? Type Y to try again OR N to exit. : ";
cin>>rpt;
} while (rpt == 'Y');
you should use a loop. something like this:
bool ended = false;
while (!ended)
{
//some code
}
If the answer is N change ended to true.
Or, as some people prefer:
bool ended;
do
{
//some code
} while(!ended);
put the code into a do while loop
int main()
{
do {
.....
} while(rpt);// instead of if
}
You should make a function, that shows the menu and returns the selected option
Example :
int showMenu() {
//Show menu
//Read option
//Return option
}
// put the label Start below the declaration //
if (rpt == 'Y')
system ("CLS");
goto Start;
else
exit (0);