String and Bool (Int Condition to be false) C++ - c++

Im new to C++ and I tried searching but I have no idea on what to search. Sorry. My problem is:
When I set the bool condition false, it still requires me to input 2 x to terminate the compiler. Why is that so? (I tried using cin.fail () but it didnt work)
When I print the list of courses, it lists the one that should terminate the program (ie when you press x). How do I correct this?
Thank you for your help.
int main(void)
{
// Gather list of courses and their codes from user,
// storing data as a vector of strings
const string DegreeCode("PHYS");
string CourseTitle;
int CourseCode(0);
vector <string> CourseList;
vector <string> :: iterator iter;
bool not_finished(true);
do
{
if (CourseTitle == "x" && "X")
{
not_finished=false;
}
else
{
cout<<"Please enter a course code and a course title (or x to finish): "<<endl;
cin>>CourseCode;
cin.sync();
cin.clear();
getline(cin , CourseTitle);
ostringstream oss;
string outputCourseList (oss.str ());
oss << DegreeCode << " " << CourseCode << " "<< CourseTitle;
CourseList.push_back (oss.str ());
cout <<outputCourseList <<endl;
oss.str(""); //clear oss content
}
} while(not_finished);
// Print out full list of courses
cout<<"List of courses:\n"<<endl;
for (iter = CourseList.begin(); iter != CourseList.end(); iter++)
cout<<(*iter)<<endl;
return 0;
}

Your problem is your comparison in the if statement:
if (CourseTitle == "x" && "X")
The proper syntax is: (variable operator variable) && (variable operator variable)
The syntax corrected:
if ((CourseTitle == "x") && (CourseTitle == "X"))
There is a logic issue because a variable can't equal two values at the same time.
Maybe you want:
if ((CourseTitle == "x") || (CourseTitle == "X"))
which means one OR the expressions is true.
You could eliminate the two compares by transforming the string into all uppercase or all lowercase. Search the web for "C++ string transform tolower toupper".

if (CourseTitle == "x" && "X")
{
not_finished=false;
}
to
if (strcmp(CourseTitle.c_str(), "x") == 0 || strcmp(CourseTitle.c_str(), "X") == 0)
{
not_finished=false;
}
== is a pointer comparison, almost never true... "x" == "x" will even be false unless you're good with compiler flags
Make sure to
#include <string.h> //<----.h is needed!

Related

Having to hit enter twice with cin.getline()

I know for a fact similar questions have been asked before but I really can't figure out what's wrong with my code specifically. For some reason if I input "n" I have to press enter twice. But if I input "y", everything works fine and the code moves to the next section. My code is as follows:
do{
try {
if (test) cout << " Re-enter: ";
test = false;
getline(cin, choice);
checkinput(choice);
}
catch (int flag) {
if (flag == 1){ cout << "Error: Input must be y or n."; test = true; }
}
} while (test);
and the checkinput function is as follows:
// function for checking the input of y/n
string checkinput(string c) {
if (c != "Y" && c != "y" && c != "N" && c != "n") {
throw 1;
}
if (cin.fail()) throw 1;
return c;
}
I think you are trying to do too much here. You can simplify this.
There is no need to throw and catch exceptions inside checkinput. Since there is only two cases you can use a boolean. Secondly, you are returning c. I don't know why you are doing that, it isn't being used. You should instead, return a boolean.
checkinput becomes:
bool checkInput(string c) {
if (c.length() > 1)
return false;
return c == "Y" || c == "y" || c == "N" || c == "n";
}
Now you can simplify the do-while and remove the try statement. Additionally, there is no need for the test variable now since we are successfully grabbing any input:
int main() {
string choice = "";
do {
cout << "Enter yes or no (y/n): ";
getline(cin, choice); // or cin >> choice;
bool check = checkInput(choice);
if (!check)
cout << "Error: Input must be y or n." << endl;
} while (true);
}
You may also simplify this further but that will be at the cost of readability. Good luck!

Get yes/no in console always fails

I'm trying to make a program that will run over and over again, provided the user says yes every time. Unfortunately, it doesn't seem to recognize when I input yes or no into it, and always does the default "Come again?" message. This is the code I'm using to get the input from the console:
bool getYN(){
bool confirmed = 0;
bool answer = 0;
string input;
while(!confirmed){
getline(cin, input, '\n');
transform(input.begin(), input.end(), input.begin(), toupper);
if(input.c_str() == "Y" || input.c_str() == "YES"){ //If the user says yes
confirmed = 1;
answer = 1;
} else if(input.c_str() == "N" || input.c_str() == "NO"){ //If the user says no
confirmed = 1;
answer = 0;
} else { //If the user says something else entirely
printf("\nCome again? (Y/N) ");
};
};
return answer;
};
I have included <string> and <algorithm>. For some reason, it always acts like it's not getting y/yes or n/no when I type them in. It just keeps asking me to answer again.
if(input.c_str() == "Y" || input.c_str() == "YES"){ //If the user says yes
confirmed = 1;
answer = 1;
} else if(input.c_str() == "N" || input.c_str() == "NO"){ //If the user says no
confirmed = 1;
answer = 0;
}
You should not be doing c-string comparisons like this. You're taking the address of a char and comparing against the address of a text-allocated object. Of course the comparison is going to return false.
With c++ strings, simple operator== comparisons are valid:
if(input == "Y" || input == "YES"){ //If the user says yes
confirmed = 1;
answer = 1;
} else if(input == "N" || input == "NO"){ //If the user says no
confirmed = 1;
answer = 0;
}
#include <iostream>
#include <string>
using namespace std; // For speed
int main()
{
bool saidYes = false;
string input;
while (!saidYes)
{
cout << "Input yes or no: ";
getline(cin, input);
if (input == "no" || input == "n" || input == "NO")
{
saidYes = true; // breaks the loop
}
if (input == "y" || input == "Y" || input == "yes" || input == "YES")
{
saidYes = false;
}
}
return 0;
}
You can use the above example to eliminate a huge portion of unnecessary code, I chose not to add the else statement but it will work if you add that here as well.
You can also condense this code even further but this was only intended to be a simple example as to how to better get this working for you!
As it was said above you can use == to compare the strings, if you're coming from certain other languages it can be an annoying change to get used to lol.
I have included string and algorithm. For some reason, it always acts like it's not getting y/yes or n/no when I type them in. It just keeps asking me to answer again.
algorithm is not required for what you're trying to do, and your making the reading and acceptance of the string input much more difficult than it needs to be.
If you look above you'll see the string input; This is going to be your variable that you can use to store the user input string into.
You'll also notice getline(cin, input); This is what you can use to "read" the string that the user enters when they're prompted to.
#Kelvin Shadewing My initial answer was only directed at your Question, this next example is directed to your comment to me below!
So you've got quite a few options but assuming that you want the user to input either yes or no and depending on the input you want to produce a specific result while ensuring that the user is prompted over and over again to input either yes or no all you have to is modify my original answer like so.
#include <iostream>
#include <string>
using namespace std; // For speed
int main()
{
bool saidYes = false;
string input;
while (!saidYes)
{
cout << "Input yes or no: ";
getline(cin, input);
if (input == "no" || input == "n" || input == "NO")
{
saidYes = true;
cout << "you said no" << endl;
/* breaks the loop by changing the
bool (true or false flag) to true, if you want to produce a specific result,
whether it's a simple output statement or a function call you can put it here
*/
}
else if (input == "y" || input == "Y" || input == "yes" || input == "YES")
{
saidYes = true;
cout << "You said yes" << endl;
/* breaks the loop by changing the
bool (true or false flag) to true, if you want to produce a specific result,
whether it's a simple output statement or a function call you can put it here
*/
}
else saidYes = false;
}
return 0;
}
I've modified my code based on the current best answer, but I've also optimized it so that confirmed is no longer necessary.
bool getYN(){
bool answer = 0;
string input;
while(!answer){
getline(cin, input, '\n');
transform(input.begin(), input.end(), input.begin(), toupper);
if(input == "Y" || input == "YES"){
answer = 2;
} else if(input == "N" || input == "NO"){
answer = 1;
} else {
printf("\nCome again? (Y/N) ");
};
};
return answer - 1;
};
Small optimization, sure, but every little bit counts.

Code keeps printing "1" when everything is correct

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.

Char validator will not only accept lower and upper case M/F

Hey im trying to validate a char to limit it to accpeting an m or f for male or female. But it doesnt pass the while condition even when m or f is pressed and keeps looping the question.
Can anybody help me with this.
Thanks in advance.
Here is my code:
char Validator :: getChar(string q)
{
char input;
do
{
cout << q.c_str() << endl;
cin >> input;
}
while(!isalpha(input) && "M"||"F"||"m"||"f");
return input;
}
The "M"||"F"||"m"||"f" part of your code doesn't do what you think it does. What it does is check the ADDRESSES of those string constants. Since they are all non-NULL, this expression simply returns true, so your condition, essentially becomes: while(!isalpha(input) && true) which is the same as while(!isalpha(input)).
Try this instead:
char Validator::getChar(const string &q)
{
char input = 0;
do
{
cout << q << endl;
cin >> input;
}
while((input != 'M') && (input != 'F') && (input != 'm') && (input != 'f'));
return input;
}
The expression in the while doesn't mean what you think it does. First, the ! does not apply to the entire expression, and second, "equality" is not an implicit test. You need to write out everything you mean.
To test for equality, use the == or != operators. You have to use the operators on every value you want to test; the operator doesn't "distribute" over a list of values like it would in ordinary English. Write your condition like this:
while (input != 'M' && input != 'F' && input != 'm' && input != 'f');
You can see that the isalpha call isn't necessary; if input isn't equal to any of the listed values, then it doesn't really matter whether it's an alphabetical character.
Another way to write it is this:
while (!(input == 'M' || input == 'F' || input == 'm' || input == 'f'));
Notice that I've another set of parentheses around the internal terms so that the ! operator applies to the entire expression instead of just the first term.
Just for an alternative approach to the terminating condition:
char Validator::getChar(const string &q)
{
const std::set<char> valid_chars { 'M', 'm', 'F', 'f' };
char input = 0;
do
{
cout << q << endl;
cin >> input;
}
while (!valid_chars.count(q));
return input;
}

using user input such as YES and NO to control program flow in C++

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
}