I have an assignment for my Intro to Comp Sci course at college. We are told to use only Loops & Boolean Expressions to create the program.
Here is a link to the assignment sheet directly:
http://cs.saddleback.edu/michele/Teaching/CS1A/Assignments/AS8%20-%20Boolean%20Expressions.pdf
I got it working with a bunch of If, Then, Else statements until I read the directions again and had to change it.
I got the 'm' 'f' 'M' 'F' part to work, but I can not get the heightOK or the weightOK (directions #2 and #3) to work out.
Please help, thanks!
PS I am brand new to programming...
Here is what I have so far: `
char gender;
int weight;
int height;
bool heightOK;
bool weightOK;
cout << "Please enter the candidate’s information (enter ‘X’ to exit).";
cout << "Gender: ";
cin.get(gender);
cin.getline(100 , '\n');
if (gender == 'm' || 'M' || 'f' || 'F')
{
}
else
{
cout << "***** Invalid gender; please enter M or F *****";
}
cout << "Height: ";
cin >> height;
cout << "Weight: ";
cin >> weight;`
You can do this without if statements. You should use do-while loops for each input, such that you loop while the input is invalid. Then, you can set your bool variables like this:
heightOK = ((gender == 'm' || gender == 'M') &&
(height > MALE_TOO_SHORT && height < MALE_TOO_TALL));
heightOK = (heightOK || (/*same as above, female version*/));
You could do that all in one line, but that gets hard to read, IMO. You should be able to set weightOK the same way.
EDIT: The do-while loop asks for and gets the input. Then, the while statement tests the input for validity.
do {
cout << "enter gender (m/f)";
cin >> gender;
} while ( !(gender == 'm' || gender == 'M' || gender == 'f' || gender == 'F') );
If you want to tell the user that the input is invalid, you can use a while loop after getting the input the first time.
Here's the expression showing that the ternary operator is a valid Boolean function with respect to
(p ∧ q) ∨ (¬p ∧ r)
"(p and q) or ((not p) and r))" or "if p then q, else r"
See also: http://en.wikipedia.org/wiki/%3F:, http://en.wikipedia.org/wiki/Conditioned_disjunction
Full disclosure, I didn't read the assignment. I just saw the implication in your post that you're restricted from using "if then else" statements and thought I'd offer a creative way to cheat.
Related
do {
cout << "Enter the account type (C for current and S for savings): ";
cin >> account_type;
} while (account_type != 'S' || 'C');
I have account_type set as char,the problem is that everytime i run the program and i input S or C the loop keeps repeating.Can anyone help me know why its happening?
All non-zero values in c++ evaluate to true when used in a boolean operation. So account_type != 'S' || 'C' is the same as account_type != 'S' || true. Which means your loop never exits.
What you need to do is to preform both checks
do {
cout << "Enter the account type (C for current and S for savings): ";
cin >> account_type;
} while (account_type != 'S' && account_type != 'C');
Its because you cant say 'S' || 'C', you would think c++ would think that you mean if account_type is S or C however c++ looks at this in 2 separate sections: (account_type == 'S') || ('C'). ('C') will default to true therefore the loop loops forever.
What you need to write is:
do {
cout << "Enter the account type (C for current and S for savings): ";
cin >> account_type;
} while (account_type != 'S' && account_type != 'C');
You need to change the || to && because if account_type is S then it cant be C and the same vice versa therefore the loop never finishes.
Your while check is wrong. You have to write it like this:
while (account_type != 'S' || account_type != 'C')
You can't perform || checks, or any for that matter like that, you must always re-state the variable.
The application asks the user to enter the students major - either math or CIS and I wanted to make it so if the user were to enter something other than those two it would give an error message and prompt the user to enter the major again.
Here is what I have so far:
cout << "Math or CIS Major: ";
cin >> major;
if (major != "math" || major != "Math" || major != "CIS" || major != "cis") {
cout << "Invalid major" << endl;
}
cout << "Enter students GPA: ";
cin >> studentGpa;
This will give the invalid major message but it will just move onto the GPA
For some reason I can't remember the basics and this is tripping me up.
Also if someone could guide me in the right direction for an alternative to the ||'s I know this isn't the best way to do this but again I am struggling with coming up with a better way to do this again
Replace OR(||) with AND(&&)
if (major != "math" && major != "Math" && major != "CIS" && major != "cis")
If you want the user to have to input a valid major then you need to put it into a loop:
std::string major;
std::cout << "Major: ";
std::cin >> major;
while (major != "math" && major != "Math" && major != "CIS" && major != "cis")
{
std::cout << "Please enter a valid major!\n";
std::cout << "Major: ";
std::cin >> major;
}
This way the program will not continue unless they enter a valid major.
You are confused between OR (||) and AND (&&). Replacing || by && in the IF condition should do the trick.
What your current if statement checks (if A is not equal to B) OR (if A is not equal to C). If B and C are distinct, then A can at max be equal to one of them, or in other words, will always not be equal to at least one of them.
So, your condition will always evaluate to true. The correct term for this is that your if condition is a tautology.
If you want to skip all this and want the code:
if (major != "math" && major != "Math" && major != "CIS" && major != "cis") {
I'm stuck as to why the condition below isn't triggering when either an 'n' or a 'y' is entered at the console. When executed you can't get out the the if statement, but i know for sure that
!(cin >> again)
isn't the culprit, as that was previously the only condition in the if statement and I was able to skip/enter the if block if a character/numeral was entered, which was as expected. Here is the code:
char again;
while (1) {
cout << endl;
cout << "I see another one, care to shoot again? (y/n): ";
if (!(cin >> again) || (again != 'n') || (again != 'y')) {
// Error checking for numberals & non 'y' or 'n' characters
cout << "Please enter 'y' or 'n' only." << endl;
cin.clear();
cin.ignore(1000, '\n');
continue;
}
break;
}
I'm stumped on this so any help would be hugely appreciated!
if(...|| (again != 'n') || (again != 'y')) {
is faulty logic. What you say is
if "again" is not n or it's not y, then do the following...
now, since "again" can't be n and y at the same time, this always evaluates to true; most probably, even your compiler notices that and just jumps right into your if's content.
What you want is something like
if(!(cin>>again) || ( again != 'n' && again != 'y') {
Because that reads
if cin>>again didn't work or again is neither n nor y then,...
A bit earlier on an IRC channel, someone asked a question about his code - essentially, his program was running on an infinite loop:
#include <iostream>
using namespace std;
int main()
{
cout << "This program adds one to your input." << endl;
char choice = 'n';
int num = 0;
while (choice != 'y' || choice != 'Y')
{
cout << "Enter number: ";
cin >> num;
num++;
cout << "Your number plus 1 is: " << num << endl;
cout << endl << "Continue/Quit? Type 'Y' to quit, any other key to continue: ";
cin >> choice;
}
cout << "By choosing 'Y', you exit the loop." << endl;
return 0;
}
Paying attention to the loop header, it seems that the loop should be working perfectly fine, but whenever it comes time for me to enter Y or y in order to exit, the loop keeps running. Considering that the while loop won't evaluate the expression to the right if the one on the left is true, this makes it especially confusing. But in any case, even if I input Y or y the loop keeps running! I'd like a somewhat in-depth explanation of why this happens, I've been trying to reason it out, look back in my textbook etc. but I can't seem to understand why... I've changed the OR into an AND, but what makes OR so bad and causes it to malfunction?
Thanks all.
Condition (choice != 'y' || choice != 'Y') is always true, so loop will run indefinetely.
If choice == 'y', then you get (false || true) == true;
if choice == 'Y', then you get (true || false) == true.
You need to use while(choice != 'y' && choice != 'Y') instead, in this case you exit loop only if you get 'y' or 'Y', otherwise you get (true && true) and continue.
The OR operator between many statements returns true if at least 1 of the statements is true, no matter if the others are false or true. In your case, choice != 'y' || choice != 'Y' will be evaluated like this:
First statement is true: Execute while loop.
First statement is false: Check if the second statement is true.
Second statement is true: Execute while loop.
Second statement is false: don't execute while loop.
Specifically, when the compiler arrives at choice != 'y', if choice == 'Y', then it will still execute, because choice != 'y' is true, in fact choice is equals to Y, so it's different from y.
If, on the other hand, choice is equals to y, then it will check if the second statement is true, but we know that choice is equals to y, so choice is different from Y, and so on...
You make a mistake,you should change "||" to "&&"
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;
}