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.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
It seems, the if statement is not being called in the given program. The output says it's a consonant even if the input is a vowel.
#include <iostream>
using namespace std;
int main() {
char input[1];
cout << "Enter an alphabet:\n";
cin >> input;
if (input == "a" || input == "e" || input == "i" || input == "o" || input == "u") {
cout << "It is a vowel";
}
else
cout << "It is a consonant";
return 0;
}
First of all you don't need to use an array. And on top of that you should use single quotes, so you should have something like this :
int main(){
cout<<"Enter an alphabet:\n";
char input;
cin>> input;
if (input=='a' || input=='e' || input=='i' || input=='o' || input=='u' ){
cout<<"It is a vowel";
}
else
cout<<"It is a consonant";
return 0;
}
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 1 year ago.
Improve this question
Is it okay or valid to use this? string answer = "A || B"; than using this if (answer == A || answer == B)?Because in my program I want to use that condition inside a while if statements. I want to put that two option condition in my while statement but it gets error.
// variables
string answerA = "Coin";
string guess;
int guessCount = 0;
int guessLimit = 3;
bool outOfGuesses = false;
cout << "Question A!\n" << endl;
cout << "A. What has a head and a tail, but no body?\n";
// if the answer is not correct, ask again only 3 times
while (answerA != guess && !outOfGuesses) {
if (guessCount < guessLimit) {
cout << "Answer: ";
cin >> guess;
guessCount++;
} else {
outOfGuesses = true;
}
}
if (outOfGuesses)
{
cout << "Your answers are all wrong! Better luck next time :)";
}
else
{
cout << "Your answer is Correct! ";
}
One std::string can hold only one string. You can use std::unordered_set to hold a set of multiple strings.
#include <iostream>
#include <string>
#include <unordered_set>
int main(void) {
std::unordered_set<std::string> accept_list = {"A", "B"};
std::string answer = "A";
if (accept_list.find(answer) != accept_list.end()) {
std::cout << "accept\n";
} else {
std::cout << "reject\n";
}
return 0;
}
(std::unordered_set is available since C++11. If your compiler doesn't support that, try std::set instead. Also initializer lists like accept_list = {"A", "B"} is since C++11, you may have to add each candidates separately in that case)
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 1 year ago.
Improve this question
int main(){
while(true){
char input = getchar();
int x, y;
POINT xypos;
if (input == 'S' || input == 's'){
std::cout <<"enter new position" << std::endl;
std::cin >> x >> y;
SetCursorPos(x, y);
} else if (input == 'g' || input == 'G'){
GetCursorPos(&xypos);
std::cout << "X: " << xypos.x << "Y " << xypos.y << std::endl;
}
}
return 0;
}
Can someone please explain why with GetCursorPos, it has to reference the xypos object in the parameters? Why is it not possible to directly utilize it? Thanks
You probably mean why GetCursorPos(from the WinAPI) doesn't just return the position instead of taking a pointer and filling that right?
That's how the WinAPI works, almost all functions return BOOL to indicate success or failure and take information they populate by pointer.
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.
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 9 years ago.
Improve this question
How can I allow the user to enter a number in the form of e^x, or sqrt(x) when asked, instead of just entering a number in numerical form? Thanks.
Generally this is a question of parsing. In the case of these two forms exactly, a simple solution could be something like:
string input;
getline(cin, input);
if (input[0] == 'e' && input[1] == '^') {
int num = atoi(input.substr(2).c_str()); // probably better to use stringstreams here
cout << exp(num) << endl;
} else if (input.substr(0, 5) == "sqrt(" && input[input.size() - 1] == ')') {
int num = atoi(input.substr(5, input.size() - 6).c_str());
cout << sqrt(num) << endl;
} else {
cout << "error" << endl;
}
Didn't test that but should be roughly right. If you need to handle more nuanced cases or more than just these 2 cases, you'll have to do some more elaborate parsing.