I'd like to check in an if statement for several words from a defined string (cin), so that it accepts different using of capital and small initial letters .
#include <iostream>
using namespace std;
int main()
{
string BothMods;
cout << "Are both online?" << endl;
cin >> BothMods;
if (BothMods == "Yes", "YES", "yes"{
cout <<"Both are online" << endl;
...
But when I type in one of the three conditions, the condition is always false (else gets executed). If I only use one (like if (BothMods == "Yes") ) it works.
You need to check each case individually using the OR operator if you want to check more than one case.
if (BothMods == "Yes" || BothMods == "YES" || BothMods == "yes") {
// do whatever
}
Related
#include <iostream>
#include <string>
using namespace std;
int main()
{
string vegetarian, vegan, gluten_free;
cout << "Welcome to Restaurant Selector!" << endl;
cout << "Are you a vegetarian?"<<endl;
cin >> vegetarian;
cout << "Are you a vegan?"<<endl;
cin >> vegan;
cout << "Are you gluten-free?"<<endl;
cin >> gluten_free;
if (gluten_free == "yes" && vegan == "yes" && vegetarian == "yes" )
{
cout<<"The Chef's Kitchen"<<endl;
}
else if (gluten_free == "yes" && vegan == "yes" )
{
cout<<"Vegan's lair"<<endl;
}
else if (gluten_free == "yes")
{
cout<<"Starbuck's"<<endl;
}
else if ( gluten_free == "no" || "No" && vegan == "no" || "No" && vegetarian == "no"||"No")
cout<<"Burger King"<<endl;
return 0;
}
How can I get the first if expression to use multiple variations of yes.
I want it to include: yes, Yes, and YES. however I can only use the or operator on two strings. I would also like to do this for the subsequent lines. What should I do?
How can I get the first if expression to use multiple variations of yes. I want it to include: yes, Yes, and YES.
Convert the input string to lower case before comparison.
A trivial example:
auto tolower = [](unsigned char c){
return std::tolower(c);
};
std::transform(
vegan.begin(),
vegan.end(),
vegan.begin(),
tolower
);
Note that simple string processing such as this work only with fixed width encodings and not with unicode.
Today I want to test if a user types the word "yes" in console application, then the function will proceed, however, I am unable to do so. (I am a new person, sorry)
Any help on this?
I know when testing a variable like.. int x = 14, and if (a < 14) print something.. but instead of number I'd like to try with text.
Here is the source code:
int main()
{
char a = yes;
char b = no;
cout << "hi, press yes to start or no to cancel";
cin >> a;
if (a == yes)
{
cout << "Cool person";
}
else if(b == no)
{
cout << "not a cool person";
}
}
I keep getting "yes" is not defined in scope.
Any help would be appreciated. Thank You!
At a bare minimum, the following problems exist in your code:
Tokens yes and no are identifiers. If you wanted them to be characters, that would be 'yes' and 'no. Except that they're not characters since they're too long. So, they should probably be strings like "yes" and "no".
The b variable is totally useless here, you should have one variable for receiving information from the user and checking it against multiple possible values. It's also a good idea to choose meaningful variable names.
You aren't including the requisite headers, nor are you using the correct namespace for the std functions and types (either by explicitly prepending std:: to each, or with a using namespace std for them all).
With that in mind, try out the following program as a starting point for your further education:
#include <iostream>
#include <string>
int main() {
std::string userInput;
std::cout << "Hi, enter yes to start or no to cancel: ";
std::cin >> userInput; // probably better: std::getline(std::cin, userInput);
if (userInput == "yes") {
std::cout << "Cool person\n";
} else if (userInput == "no") {
std::cout << "Not a cool person\n";
} else {
std::cout << "Hey, can't you read? I said yes or no :-)\n";
}
}
The code runs and all but it doesn't print out the vowels, but instead prints a "1".
#include <iostream>
#include <string>
using namespace std;
int countVowels(string sentence,int numVowels)
{
for(int i =0; i<sentence.length(); i++)
{
if((sentence[i]==('a'))||(sentence[i]==('e'))||(sentence[i]==('i'))||(sentence[i]==('o'))||(sentence[i]==('u'))||(sentence[i]==('A'))||(sentence[i]==('E'))||(sentence[i]==('I'))||(sentence[i]==('O'))||(sentence[i]==('U')))
numVowels=numVowels+1;
}
}
int main()
{
string sentence;
int numVowels = 0;
do{
cout << "Enter a sentence or q to quit: ";
cin >> ws;
getline(cin,sentence);
}
if(sentence == 'q'|| sentence == 'Q');
cout << "There are " << countVowels << " vowels in your sentence." << endl;
return 0;
}
The output should be like this:
Enter a sentence or a to quit: I like apples!
There are 4 vowels in your sentence, and 11 letters.
Enter a sentence or q to quit: q
Bye!
My problem:
Can someone explain to me why it keeps printing a "1", and my "if" statement where I am supposed to assign the hotkey "q" to exit the program isn't working. When I run the program I get an error at the if statement saying "no match for operators=="
I usually don't like just providing a full solution, but since your question shows you have made a good effort, here's how I would write it (well, not quite, I simplified a little to be more beginner friendly):
#include <algorithm>
#include <iostream>
#include <string>
bool isVowel(char c)
{
// A simple function that returns true if the character passed in matches
// any of the list of vowels, and returns false on any other input.
if ( 'a' == c ||
'e' == c ||
'i' == c ||
'o' == c ||
'u' == c ||
'A' == c ||
'E' == c ||
'I' == c ||
'O' == c ||
'U' == c) {
return true; // return true if it's a vowel
}
return false; // remember to return false if it isn't
}
std::size_t countVowels(std::string const& sentence)
{
// Use the standard count_if algorithm to loop over the string and count
// all characters that the predicate returns true for.
// Note that we return the resulting total.
return std::count_if(std::begin(sentence),
std::end (sentence),
isVowel);
}
int main() {
std::string sentence;
std::cout << "Please enter a sentence, or q to quit: ";
std::getline(std::cin, sentence);
if ( "q" == sentence ||
"Q" == sentence) {
// Quit if the user entered a string containing just a single letter q.
// Note we compare against a string literal, not a single character.
return 0;
}
// Call the counting function and print the result.
std::cout << "There are "
<< countVowels(sentence) // Call the function on the input.
<< " vowels in your sentence\n";
return 0;
}
Hopefully the comments make it all clear.
Now you might have been told that you can't use the standard algorithms (std::count_if), since part of the exercise seems to be to write that. Well I'll leave that to you. Your current version is close to correct, but remember to return the result. And you don't really need to pass in the numVowels count, just create that within the function, and remember to return it.
This question already has answers here:
if statement not working right?
(5 answers)
Closed 7 years ago.
after a good amount of time trying to get my else if statement to work, it just doesn't. This program keeps returning the first one, no matter what I input. Please help.
#include <iostream>
#include <string>
using namespace std;
string arehap;
int main()
{
cout << "Are you happy?" << endl;
cin >> arehap;
if (arehap == "Yes" || "Y")
{
cout << "Good." << endl;
}
else if (arehap == "No" || "N")
{
cout << "Bad." << endl;
}
return 0;
}
You should use this:
if (arehap == "Yes" || arehap == "Y")
{
cout << "Good." << endl;
}
else if (arehap == "No" || arehap == "N")
{
cout << "Bad." << endl;
}
When you're using the || operator, you have to compare two boolean values. If arehap is equal to "Y", the following statement will be True: arehap == "Y". In that case your computer will "understand" this as if (True || False) { /* do smth */} and this will evaluate to True and the code you want to execute will be run.
Your problem lies in this line:
if (arehap == "Yes" || "Y")
C++ understands this as
if ((arehap == "Yes") || ("Y"))
and while the first check (arehap == "Yes") might be false, the second check -- which is just "Yes" is always true.
This happens, because the "Yes" gets understood as a char const* -- and this pointer must obviously not be NULL, but point to the character 'Y'!
I'm making a small program that uses a if else statement, but instead of using numbers to control the flow i want to be able to make the control work with with yes and no;
for example:
cout << "would you like to continue?" << endl;
cout << "\nYES or NO" << endl;
int input =0;
cin >> input;
string Yes = "YES";
string No = "NO";
if (input == no)
{
cout << "testone" << endl;
}
if (input == yes)
{
cout << "test two" << endl;
//the rest of the program goes here i guess?
}
else
{
cout << "you entered the wrong thing, start again" << endl;
//maybe some type of loop structure to go back
}
but I can't seem to get any variations of this to work, i could make the user type a 0 or 1 instead but that seems really stupid, i'd rather it be as natural as possible, users don't speak numbers do they?
also i need to be able to simply add more words, for example "no NO No noo no n" all would have to mean no
hopefully that makes some sense
also i would love to make this using a window but i've only learned basic c++ so far not even that and i cant find any good resources online about basic windows programming.
You're not reading in a string, you're reading in an int.
Try this:
string input;
instead of
int input = 0;
Also, C++ is case-sensitive, so you can't define a variable called Yes and then try to use it as yes. They need to be in the same case.
btw, your second if statement should be an else if, otherwise if you type in "NO" then it will still go into that last else block.
First of all, input must be std::string, not int.
Also, you've written yes and no wrong:
v
if (input == No)
// ..
// v
else if (input == Yes)
^^^^
If you want your program to work with "no no no ..", you could use std::string::find:
if( std::string::npos != input.find( "no" ) )
// ..
The same with "Yes".
Also, you could do this to be almost case-insensitive - transform the input to upper-case letters (or lower, whatever ), and then use find.This way, yEs will be still a valid answer.
bool yesno(char const* prompt, bool default_yes=true) {
using namespace std;
if (prompt && cin.tie()) {
*cin.tie() << prompt << (default_yes ? " [Yn] " : " [yN] ");
}
string line;
if (!getline(cin, line)) {
throw std::runtime_error("yesno: unexpected input error");
}
else if (line.size() == 0) {
return default_yes;
}
else {
return line[0] == 'Y' || line[0] == 'y';
}
}
string input;
cin >> input;
if (input == "yes"){
}
else if (input == "no"{
}
else {
//blah
}