THE IDEA (C++):
The idea is simple, if you're under 21 and in full time education, you're eligible (no idea for what, it's just homework). If you're not eligible, you have to tell the user why.
int main()
{
string education;
int age;
cout << "Are you in full time education? (y/n): ";
cin >> education;
cout << "\nEnter your age: ";
cin >> age;
system("cls");
if (((education == "yes" || education == "y")) && (age <= 21))
{
cout << "You are eligible.";
}
else if (((education == "yes" || "y")) && (age > 21))
{
cout << "You are not eligible because you are over 21.";
}
else if (((education == "no" || "n")) && (age <= 21))
{
cout << "You are not eligible because you are not in full time education.";
}
else if (((education == "no" || "n")) && (age > 21))
{
cout << "You are not eligible because you are not in full time education and you are over 21.";
}
else
{
cout << "There is a problem with your input.";
}
}
THE PROBLEM:
Now, if I input that I'm NOT in fulltime education AND over 21, the output is "You are not eligible because you are over 21.", which is technically true, but it should be giving me "You are not eligible because you are not in full time education and you are over 21." instead!
Things to note:
My #include statements are cut out of the screenshot, but don't worry about them, I know they're fine.
All the "else if" statements were originally just "if", but I made them this way to try and fix the issue.. to no avail clearly.
You can't use the or operator like this
a == 'first' || 'second' // education == 'yes' || 'y'
in order to say "if a is equal to first or second", you have to repeat the a== also on the right hand side:
a == 'first' || a == 'second' // education == 'yes' || education == 'y'
Related
I'm new to C++, teaching myself via youtube and some books I bought. I can not for the life of me figure out why the 2nd while statement will not work. from a mathematical stand point I feel it should work. If month does not = june or july then do the if else statement. But even when I run the right answer it always runs the if not the else. I feel it has something to do with it being a string, so I tested it without the or "||" and it worked. So maybe it has to do with combining strings and or statements. So did research on using these together and could not find much. Thanks for the help.
int main()
{
int year;
int day = 0;
string month = "x";
do
{
if (day == 0)
{
cout << "hello" << endl;
cout << "Please Enter your B-Day as Day, Month, Year" << endl;
cout << "day" << endl;
cin >> day;
}
else
{
cout << "Please enter a correct day" << endl;
cin >> day;
}
} while (day > 31 || day < 1);
do
{
if (month == "x")
{
cout << "Please enter the month you were born" << endl;
cin >> month;
}
else
{
cout << "Please Enter a correct Month." << endl;
cin >> month;
}
}
**while (month != "june" || month != "july");**
return 0;
}
If you do:
while (month != "june" && month != "july");
Or alternatively,
while (!(month == "june" || month == "july"));
Rather than:
while (month != "june" || month != "july");
Your program will be working fine even with multiple logical OR.
while (month != "june" || month != "july");
There is something called short-circuit evaluation in C++ which will in this case will not evaluate the right of || if the first operand returns true. Likewise for &&, it will not evaluate right operand if first is false. So be sure how you want the logic to behave and write the code.
I have difficulty with my C++ assignment.
The first problem is at the end of the loop with (answer != "yes" && customers <= 5), the output is not working, because it gives both condition. The second problem is that the code is too complex and need to be simplify (any suggestion)?
The code:
#include <iostream> // Access output and input stream
#include <string> // Access string variable
using namespace std;
int main() {
int characters, food, movie, customers=0; //Declare variables
char gender; //Declare variables
string name, answer; //Declare variables
cout <<"Is there a customer? <enter yes if there is and anything else to end program>"<<endl;
cin>>answer;
while (answer == "yes")
{
customers++;
cout <<"\nWhat is your name dear? ";
cin >> name;
cout <<"Well "<<name<<", welcome to my salon!\n";
cout <<"I will ask you a few questions and your answers will determine which haircut I will give you.\nPlease enter your choice by using the character between < > next to your choice.\n\n";
cout <<"Are you <m>ale or <f>emale? ";
cin >>gender;
if (gender == 'm')
{
cout <<"Are you a Super Hero<0> or a Super Villain<1>? ";
cin >>characters;
if (characters == 1)
{cout <<name <<", You should get a mohawk.\n";}
else if (characters == 0)
{
cout <<"OK "<<name<<", do you prefer steak<0> or sushi<1>? ";
cin >>food;
if (food == 0)
cout <<name<<", You should get a flat top.\n";
else if (food == 1)
cout <<name<<", You should get a pompadour.\n";
}
cout <<"Hope you like it!!\n------------\n";
}
else if (gender == 'f')
{
cout <<"Are you a Super Hero<0> or a Super Villain<1>? ";
cin >>characters;
if (characters == 1)
{cout <<name <<", You should get a mohawk.\n";}
else if (characters == 0)
{
cout <<"OK "<<name<<", do you prefer anime<0> or sitcom<1>? ";
cin >>movie;
if (movie == 0)
cout <<name<<", You should go with bangs.\n";
else if (movie == 1)
cout <<name<<", You should go with feathered.\n";
}
cout <<"Hope you like it!!\n------------\n";
}
cout <<"Any other customers? <enter yes if there are and anything else if I am done for the day> "<<endl;
cin >>answer;
if (answer != "yes" && customers >= 5)
cout<<"\nWell that was a good day! I had " <<customers<<" customer<s> today. Tomorrow is another day ..."<< endl;
else if (answer != "yes" && customers < 5)
cout<<"\nWell that was a poor day! I had " <<customers<<" customer<s> today. Tomorrow is another day ..."<< endl;
}
cout<<"\nWell that was a poor day! I had " <<customers<<" customer<s> today. Tomorrow is another day ..."<< endl;
return 0;
}
Put the if else condition outside the while loop and remove cout<<"\nWell that was a poor day! I had " <<customers<<" customer<s> today. Tomorrow is another day ..."<< endl; outside your while loop.
while (answer == "yes")
{
...
}
if (customers >= 5)
cout<<"\nWell that was a good day! I had "
<<customers
<<" customer<s> today. Tomorrow is another day ..."
<< endl;
else
cout<<"\nWell that was a poor day! I had "
<<customers
<<" customer<s> today. Tomorrow is another day ..."
<< endl;
int OnLoad() {
cout << "Hi whats your name? ";
cin >> name;
system("cls");
cout << "Hi " << name << "." << " Are you here to Take Over the city from zombies?"<< endl;
cin >> userInput;
if (userInput == "yes" || "Yes") {
cout << "Yes" << endl;
}
else if (userInput == "no" || "No") {
cout << "No" << endl;
}
else {
cout << "I don't understand." << endl;
}
return 0;
}
int main() {
OnLoad();
system("pause");
return 0;
}
This code only returns Yes back, after the console window pops up and ask are you here to take over the city from zombies even after i type no it returns yes!
if (userInput == "yes" || "Yes")
actually means
if ((userInput == "yes") || ("Yes"))
It's logical OR between two expressions: userInput == "yes" and "Yes". The first one is correct and evaluates to bool directly. The second one is just a char* that will be converted to bool implicitly. Since it's a compile time string it cannot be nullptr, which means it will always evaluate to true. And that, in turn, means the whole condition is always true (that's how logical OR works).
The correct code is
if (userInput == "yes" || userInput == "Yes")
P. S. This is why I always recommend compiling with the highest warning level possible (/W4 for MSVC, -Wall -pedantic-errors for GCC and clang). Most compilers will generate a warning in this case.
that's not how the || operator works, if you just put "Yes" as a condition it will always evaluate to true
if (userInput == "yes" || userInput == "Yes") {
cout << "Yes" << endl;
}
the reason why is because of precedence
userInput == "yes"
and
userInput == "Yes"
get evaluated before || ( logical OR)
The direction in the project states the following:
“For the activity level, if the input is not valid, print a message and tell the user you are assuming that they are sedentary.”
The following is my current code, I am trying to get it to default to "Sedentary" which the user can input as SA or sa. The default for an invalid input is supposed to make the program default to sedentary, and I am a little confused whether I am on the right track or not.
else if (gender == 'F' || gender == 'f') {
cout << "Please enter your Height in inches. (You may include a decimal) ";
cin >> height;
cout << "Please enter your Weight in pounds as a whole number. ";
cin >> weight;
cout << "Please enter your Age in years. ";
cin >> age;
cout << endl;
if (activity_level = 'SA' || activity_level = 'sa' || activity_level = 'LA' || activity_level = 'la' ||
activity_level = 'MA' || activity_level = 'ma' || activity_level = 'VA' || activity_level = 'va') {
cout << "Please enter your activity level." << endl << endl;
cout << "You may enter one of the following choices." << endl << endl;
cout << "Sedentary (Little or no exercise) \"SA\" or \"sa\" " << endl;
cout << "Lightly Active (Light exercise/sports 1-3 days a week) \"LA\" or \"la\" " << endl;
cout << "Moderately Active (Moderate exercise/sports 3-5 days a week) \"MA\" or \"ma\" " << endl;
cout << "Very Active (Hard exercise/sports 6-7 days a week) \"VA\" or \"va\" " << endl << endl;
cout << "Enter your activity level now. ";
cin >> activity_level;
cout << endl;
}
// Output for the message to defualt to Sedentary if you do not select activity level throught he proper input.
else {
activity_level =
cout << "I'm sorry I did not recogonize that activity level. We will assume a sedentary amount of exercise. "
}
}
Basically I am wondering, if what I am doing; the use of another if statement within the else if statement, will work out, and I am wondering if the way I have it set up at the moment will produce the required result.
If you want to default to SA then you could do this:
//Assume activity_level has had something assigned to it, to compare to.
if (activity_level == "SA" || activity_level == "sa" || activity_level == "LA" || activity_level == "la" ||
activity_level == "MA" || activity_level == "ma" || activity_level == "VA" || activity_level == "va")
{
//Do stuff if input is valid
}
// Output for the message to defualt to Sedentary if you do not select activity level throught he proper input.
else
{
activity_level = "SA";
std::cout << "I'm sorry I did not recogonize that activity level. We will assume a sedentary amount of exercise.";
}
Also something with single quotes can only be a single char, anything else needs double quotes around it, because it's a string.
You must check the variable `activity_level' after it has been assigned a value. Also you should use == to make comparisons. And you could use the ! operator to negate a condition.
/*********************************************************
** Purpose: Asks the user for cable package they bought **
** and the amount of hrs they used and //tells them **
** their monthly bill **
**********************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//Defining variables
double hours_over; //Amount of hrs the user went over their monthly allottment
double extra_pay; //Extra bill amount for going over monthly hrs allotted
double monthly_bill; //Monthly bill the user will pay
int hours; // How many hours the user used during the month
char package; //The package the user chose
//Getting the package the user bought
cout << "Your monthly subscription bill is based on your package.";
cout << "\n\nWhat package did you buy? Enter A, B or C: ";
cin >> package;
//Validating user input-must enter A, B or C
while (package != 'A' || package != 'B' || package != 'C')
{
cout << "\nPlease enter A, B or C(capitalized).";
cout << "\n\nWhat package did you buy?: ";
cin >> package;
}
//Getting hours the user used during month
cout << "How many hours did you use?: ";
cin >> hours;
//Validating user input-hrs cant exceed 744
while (hours > 744)
{
cout << "I'm sorry but your monthly usage cannot exceed 744 hrs.";
cout << "\nPlease enter another number.";
cout << "How many hours did you use?: ";
cin >> hours;
}
//Fixing decimal place of answers
cout << setprecision(2) << fixed << showpoint << endl;
//Switch statement-go to the package the user bought
switch (package)
{
case 'A':
if (hours > 10)
{
hours_over=hours-10;
extra_pay=hours_over*(2.00);
monthly_bill=9.95+extra_pay;
cout << "Your monthly bill is: $" << monthly_bill << endl;
}
else
{
cout << "Your monthly bill is: $9.95";
}
break;
case 'B':
if (hours > 20)
{
hours_over=hours-20;
extra_pay=hours_over;
monthly_bill=14.95+extra_pay;
cout << "Your monthly bill is: $" << monthly_bill << endl;
}
else
{
cout << "Your monthly bill is: $14.95";
}
break;
case 'C':
cout << "Your monthly bill is: $19.95";
break;
default:
break;
}
cin.get();
return 0;
}
Your test for A, B, or C is wrong
while (package != 'A' || package != 'B' || package != 'C')
should be
while (package != 'A' && package != 'B' && package != 'C')
Consider your expression:
while (package != 'A' || package != 'B' || package != 'C') {
Let package have the value 'A'.
This evaluates to
false || true || true
which is of course true.
This line always evaluates to true:
while (package != 'A' || package != 'B' || package != 'C')
it should probably be:
while (package != 'A' && package != 'B' && package != 'C')
You should be checking to see if cin has been able to stream a value of the desired type, ala if (cin >> my_int), then using std::cin.clear() after an erroneous input before getting them to reenter the value. Otherwise, garbage values like say some text input that can't be converted to an int leave std::cin in an error state and the next std::cin >> xxx isn't even attempted.
The "while" will only loop when you want it to, but the "if" will always fire; is that what you mean? The "if" concerning A, B, or C always fires because you've used "||" meaning "or" to link conditions. It is always true, for any value of your variable, that it's not A, or not B, or not C!
First of all, this code:
//Validating user input-must enter A, B or C
if (package != 'A' || package != 'B' || package != 'C')
{
cout << "\nPlease enter A, B or C(capitalized).";
cout << "\n\nWhat package did you buy?: ";
cin >> package;
}
won't work, because (1) you're comparing a string (package) to characters, and (2) you're using || (or) instead of && (and). Also (3) you probably want "while" instead of "if".
The while loop worked fine for me.
cin >> package;
//Validating user input-must enter A, B or C
while (package != 'A' || package != 'B' || package != 'C')
{
cout << "\nPlease enter A, B or C(capitalized).";
cout << "\n\nWhat package did you buy?: ";
cin >> package;
}
What if the package value is B entered inside the loop. It satisfies the first condition package != 'A' and since it is an OR operation after it( true || false || true leads to true), loop enters. You should use && instead. So, change
while (package != 'A' && package != 'B' && package != 'C')
{
// .....
}