Putting a switch inside of do...while - c++

So I have a do... while statement that is supposed to repeat unless the user enters one of four things. Those four things have a break and they set the variable that controls the while to true, where the default is false. If the user does not enter one of the four answers, the variable will stay false, which should make the do...while start again. It doesn't.
#include <string>
#include <iostream>
using namespace std;
int main()
{
int q5_answer = 1;
int q5_answerU;
int total_score;
bool q5_valid = false;
//Question_5:
do
{
cout << "Question 5 (#1 is correct)" <<endl;
cout << "1.) Answer 1" <<endl;
cout << "2.) Answer 2" <<endl;
cout << "3.) Answer 3" <<endl;
cout << "4.) Answer 4" <<endl;
cin >> q5_answerU;
switch (q5_answerU)
{
case 1:
cout << "Correct!" <<endl;
q5_valid = true;
(total_score = total_score + 1);
break;
case 2:
cout << "Incorrect" <<endl;
q5_valid = true;
break;
case 3:
cout << "Incorrect" <<endl;
q5_valid = true;
break;
case 4:
cout << "Incorrect" <<endl;
q5_valid = true;
break;
default:
cout << "Invalid answer" <<endl;
}
} while (q5_valid = false);
}

The condition inside the while is an "accidental" assignment, not a comparison. It needs to read while (q5_valid == false) or more idiomatic while (!q5_valid).

This is not a boolean expression:
q5_valid = false
It sets the variable q5_valid to false and also returns false. Replace it by:
!q5_valid
or
q5_valid == false
Which are the same. The former is better for readability.

Related

How to check if input is valid and keep asking for input if it's not

Can anybody please explain why this while loop is not working properly?
#include <iostream>
using namespace std;
void ask_user(int a, int b){
char choice = ' ';
while(choice != 'q' && choice != 'a' && choice != 's' && choice != 'm'){
cout << "choose operation" << endl;
cout << "a to add, s to subtract, m to multiply and q to quit" << endl;
cout << "----------------------------" << endl;
cin >> choice;
switch(choice){
case 'a' : cout << "a + b = " << a + b;
break;
case 's' : cout << "a - b = " << a + b;
break;
case 'm' : cout << "a * b = " << a + b;
break;
case 'q' : break;
default: cout << "please Enter a valid choice " << endl;
}
}
}
int main(){
ask_user(7, 9);
return 0;
}
If I enter p for exemple which is not valid then it works fine and asks for valid input again,
but if I enter pp that's when it starts bugging and prints the message twice. If I enter ppp it
prints three times etc...
First thing, you have a misunderstanding of how switch works. Each case must end with break statement so that the following one won't get executed.
Which means that a break will break the switch, not the while.
But the main issue is that the logic of your program is wrong.
You should not loop over the validity of the given input, let the switch statement handle that in the default clause.
Instead you should loop over a flag that will be set when the user press the q key.
So considering you have the following functions defined to respectively display the menu and ask for the operands to operate on (defined for code readability):
void display_menu(char & choice)
{
std::cout << "Operation:\na: Addition\nm: Multiplication\ns: Substraction\nq: Quit\n";
std::cin >> choice;
}
void ask_operands(int & a, int & b)
{
std::cout << "\na ?";
std::cin >> a;
std::cout << "\nb ?";
std::cin >> b;
std::cout << '\n';
}
The logic of your code can be then rewritten as:
int main()
{
bool quit = false;
char choice;
int a, b;
ask_operands(a, b); // Ask the user which operands to use
while(!quit) // loop over the flag
{
display_menu(choice);
switch(choice)
{
case 'a': std::cout << (a+b);
break;
case 'm': std::cout << (a*b);
break;
case 's': std::cout << (a-b);
break;
case 'q': std::cout << "Exiting...";
quit = true; // Set the flag to false
break;
default: std::cout << "Invalid choice, try again."; //Here you handle the invalid choices (i.e. let the loop iterate again)
}
std::cout << '\n';
}
return 0;
}
Live example
Note: If you want the user to be able to change the value of the operands at each iteration, just move the ask_operands(a, b); call inside the loop.

Can't get it back to loop. C++

I'm trying to get it back to loop if they enter anything from the choices. everytime I enter 4, it just ends. and if I pick the right one it also ends. Is there anyway I can get it to ask user to input the right one?
void towsoncourse ()
{
cout << "Enter Course: 1 is COSC,2 is ENGL,3 is MATH" << endl;
int course;
bool finish;
bool finishcourse = true;
cin >> course;
while (finishcourse != true)
{
cout << "Enter correct number for course" << endl;
if (course == 1 || course == 2 | course == 3)
{
finish = true;
}
else
{
cout<< "Error: Enter number corresponding to course." << endl;
}
}
switch (course)
{
case 1:
cout << "COSC" << endl;
break;
case 2:
cout << "ENGL" << endl;
break;
case 3:
cout << "MATH" << endl;
break;
default:
cout << "Error: Enter number corresponding to course" << endl;
}
}
int main ()
{
towsoncourse ();
return 0;
}
Not a complete answer, but rather a guide to point the way.
You want to keep reading an input until it is one of 3 possible values. So a good place to read and test the input would be inside a loop, exiting only when the test conditions are met.
while loops test continue criteria before each execution. do loops test continue criteria after each execution. In you case it is necessary to execute at least once.
There were some issues with the code.
1) while (finishcourse != true) condition was wrong. It should be while (finishcourse == true).
2) finish = true; assignment was wrong. It should have been finishcourse = false;
3) cin >> course; should be taken inside the loop. Because if you place it outside, it will lead to infinite loop in case of incorrect entry.
So, Just to ensure readability, I have rewritten the code. I have assumed that it gets back to the loop in case of incorrect entry and in case of correct entry, it terminates.
#include <iostream>
using namespace std;
void towsoncourse ()
{
bool finishcourse = true;
while (finishcourse == true)
{
int course;
cout << "Enter Course: 1 is COSC,2 is ENGL,3 is MATH" << endl;
cin >> course;
switch (course)
{
case 1:
cout << "COSC" << endl;
finishcourse = false;
break;
case 2:
cout << "ENGL" << endl;
finishcourse = false;
break;
case 3:
cout << "MATH" << endl;
finishcourse = false;
break;
default:
cout << "Error: Enter number corresponding to course." << endl;
}
}
}
int main ()
{
towsoncourse ();
return 0;
}

Getch in combination with while-loop triggers loopcontent twice

I'm not really a programmer so my knownledge is extremely limited, I'm just hoping to get a very basic program working for a project. I've been googling around a lot and from what I've found I'm guessing it has to do with getch reading blank spaces or new lines as an input or something along those lines. None of the solutions I have found seem to fix the issue for me however (I might be implementing them wrong though).
This is in the main function:
while (roundNum <= 20)
{
roundNum++;
cout << roundNum / 2 << endl;
arrowKey();
}
And the arrowKey function (which I found by googling so I don) looks like this:
int arrowKey()
{
int c = 0;
switch ((c = _getch()))
{
case KEY_UP:
cout << endl << "Up" << endl;
break;
case KEY_LEFT:
cout << endl << "Left" << endl;
break;
case KEY_RIGHT:
cout << endl << "Right" << endl;
break;
case KEY_DOWN:
cout << endl << "Down" << endl;
break;
}
return 0;
}
The problem is that the roundNum reads out twice as well as adds two to the number for every arrow key press, like this:
1
1
Up
2
2
Right
3
3
etc
Help is greatly appreciated!
The problem is that a char can only hold 255 values. Thus, in order to get more unique values getch sometimes uses two values to represent a key code, an instruction and a value. For example, try hitting the F keys and you'll notice the same issue arise.
Ultimately, the behavior you are experiencing is due to the fact that getch is being called twice for the arrow keys.
To fix this, check if the instruction is for the arrow keys, in this case the value is 224 for the arrows keys. Once you have identified an arrow keys is being pressed read in the second value with another call to getch and there you will be able to find the actual key.
#include <iostream>
#include <conio.h>
using namespace std;
enum KeyCodes {
KEY_UP = 72,
KEY_LEFT = 75,
KEY_RIGHT = 77,
KEY_DOWN = 80
};
int arrowKey()
{
int c = _getch();
if (c == 224) {
//another value
int key = _getch();
switch (key)
{
case KEY_UP:
cout << endl << "Up" << endl;
break;
case KEY_LEFT:
cout << endl << "Left" << endl;
break;
case KEY_RIGHT:
cout << endl << "Right" << endl;
break;
case KEY_DOWN:
cout << endl << "Down" << endl;
break;
}
}
return 0;
}
int main() {
int roundNum = 0;
while (roundNum <= 20)
{
roundNum++;
cout << roundNum / 2 << endl;
arrowKey();
}
}
It's also worth pointing out that getch is not apart of the std library and could thus have some variation.

Alternative to goto in nested loops?

This code is working fine, however this whole time I've tried avoiding using the goto statements that you will see in the switch (dice_total) statement.
Without the goto statements, the program will not loop back to the beginning of while (again=='y' || again=='Y'), and instead it keeps looping itself when it reaches the do-while loop.
However, I believe that it is also important to say, that if dice_total is = to the point_total the first time around then the program will function properly, and loop back to the beginning. For example, when the program starts, the first round will generate the point_total, which we will say its 10. Which is a value that will allow the program to continue to the next round, and if the dice_total also gets the same number, 10, the program will say you win, and the loop will work properly. However, if the program reaches the do while loop, and generates a number that isn't 10, but generates a 10 after a few loops, then the program will not loop to the beginning. So what I want to ask, what is wrong with my switch(dice_total) statement, and how can I fix it, to give the program the same effect without using the goto statements?
#include "stdafx.h"
#include <iostream>
#include <random>
#include <string>
using namespace std;
int main()
{
//Declared Variables***********************************
char again = 'y';
int point1;
int point2;
int point_total;
int round_1=1;
int dice1;
int dice2;
int dice_total;
//*****************************************************
//RANDOM SEED******************************************
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int>dist(1, 6);
//*****************************************************
start://TEMPORARY
while (again == 'y'||again=='Y')
{
int round_1 = 1;
system("CLS");
cout << "WELCOME TO THE CRAPS GAME" << endl;
cout << "THROWING ROUND:" << round_1 << " DICES.............." << endl;
point1 = dist(mt);
point2 = dist(mt);
point_total = point1 + point2;
cout << "ROUND: " << round_1 << " First dice is: " << point1 << " and second dice is: " << point2 <<" and the total is:"<<point_total<< endl;
switch (point_total)
{
case 7:
case 11:
cout << "YOU WON CONGRATS PRESS Y TO PLAY AGAIN!!" << endl;
cin >> again;
break;
case 2:
case 3:
case 12:
cout << "YOU LOST, PRESS Y TO TRY AGAIN" << endl;
cin >> again;
break;
default:
do
{
++round_1;
cout << "ROUND " << round_1 << endl;
dice1 = dist(mt);
dice2 = dist(mt);
dice_total = dice1 + dice2;
cout << "THROWING ROUND: " << round_1 << " DICES.............." << endl;
cout << "ROUND 1 DICE TOTAL IS: " << point_total << endl;
cout << "ROUND: " << round_1 << " First dice is: " << dice1 << " and second dice is: " << dice2 << " and the total is:" << dice_total << endl;
switch (dice_total)
{
case 11:
cout << "YOU WON CONGRATS PRESS Y TO PLAY AGAIN!!" << endl;
cin >> again;
goto start;
case 2:
case 3:
case 7:
case 12:
cout << "YOU LOST, PRESS Y TO TRY AGAIN" << endl;
cin >> again;
goto start;
default:
if (dice_total == point_total)
{
cout << "YOU WON CONGRATS PRESS Y TO PLAY AGAIN!!<<endl;
cin >> again;
break;
}//if
else
{
cout << "Going to next round" << endl;
}
}//dice_total
}//do
while (dice_total != point_total);
break;
}//switch point
}//again while
}//main
The problem you're facing is usual when you have too many nested loops in the same function, and is an indicator that you need to refactor parts of your code to be in their own functions.
If you do this, then you have more possibilities to control the flow of your code: in each function you have break and return, and as you can return a custom value, you can use it to determine in the surrounding function if you need to break or return again.
Besides, this gives you the opportunity to put self-explanatory names to your functions, which makes your code clearer for people that look at it for the first time (as it's written, it's so dense that I can't understand it unless I stare at it for some minutes).
An example of what I mean in code:
Before
int main() {
start:
while (a) {
b1();
switch(c) {
case 1:
do {
d();
if (cond) goto start;
} while(e);
break;
}
b2();
}
}
After
int main() {
while (a) {
if (!doStuff1())
break;
}
...
}
bool doStuff1() {
b1();
while (a) {
bool res = doStuff2();
if (res) return true;
}
b2();
...
}
bool doStuff2() {
switch(c) {
case 1:
if (doStuff3()) return true;
}
return false;
}
bool doStuff3() {
do {
d();
if (cond) return true;
} while (e);
return false;
}
How about this design?
bool stop=false;
while(!stop && (again == 'y'||again=='Y'))
{
while(again == 'y'||again=='Y')
{
// ...
break; /* breaks inner while*/
// ...
stop=true;
break; /* breaks inner while, and prevents running outer loop*/
}
}

I would like to remove using namespace std from this code, but am unsure what all needs to be prefixed with std::

I want to remove using namespace std, but I don't know what all needs to be prefixed.
#include <iostream>
#include "PlayingCard.h"
using namespace std;
PlayingCard makeValidCard(int value, int suit);
int main()
{
// Create a playing card
PlayingCard card1;
// Test the default constructor and GetCardCode
cout << "Testing default constructor. Expect card code to be 00\n card code is :";
cout << card1.getCardCode() << endl << endl;
// Test the setter and getter
cout << "Seting card to 'AH' using SetValue and SetSuit" << endl;
card1.setCard('A', 'H');
cout << "GetValue returns :" << card1.getValue() << endl;
cout << "GetSuit returns :" << card1.getSuit() << endl << endl;
// Test overloaded constructor
PlayingCard tenOfSpades('T', 'S');
cout << "Testing overloaded constructor. Expect card code to be TS\n card code is :";
cout << tenOfSpades.getCardCode() << endl << endl;
// Test IsValid with valid cards
cout << "Testing valid card codes.\n"
<< "Expect isValid to return true for all (except perhaps Jokers.)"
<< endl;
// Create and test valid cards
int validCards = 0; // cards that return true for IsValid
int invalidCards = 0; // cards that return false for IsValid
// Create and test four suits plus the jokers
for(int suit = 1; suit <= 5; suit++)
{
// Create and test ace, 2 - 9, Jack, Queen, and King
for(int value = 1; value <= 13; value++)
{
PlayingCard aCard = makeValidCard(value, suit);
cout << "Card Code: " << aCard.getCardCode() << " IsValid :";
if (aCard.isValid())
{
validCards++;
cout << "true" << endl;
}
else
{
invalidCards++;
cout << "false" << endl;
}
// suit 5 is just for creating the two Jokers
if (suit == 5 && value >= 2)
break;
}
}
cout << "IsValid returned false for " << invalidCards << " card codes" << endl;
cout << "IsValid returned true for " << validCards << " card codes" << endl;
cout << endl;
// Test IsValid with invalid cards
// Create and test invalid cards
cout << "Testing invalid card codes; isValid should return false for all." << endl;
validCards = 0;
invalidCards = 0;
// Loop through all possible ASCII character codes for card codes
for(int suit = 0; suit <= 255; suit++)
for(int value = 0; value <= 255; value++)
{
// Only check card codes that are not valid
PlayingCard aCard = makeValidCard(value, suit);
if (aCard.getCardCode() == "00")
{
if (aCard.isValid())
{
cout << "value :" << value << " suit :" <<suit << " IsValid :";
cout << "true" << endl;
validCards++;
}
else
{
invalidCards++;
}
}
}
cout << "IsValid returned false for " << invalidCards << " card codes" << endl;
cout << "IsValid returned true for " << validCards << " card codes" << endl;
return 0;
}
/******************************************************/
/* Test Functions */
/******************************************************/
PlayingCard makeValidCard(int iValue, int iSuit)
{
char value = '0';
char suit = '0';
switch (iValue)
{
case 1:
value = 'A';
break;
case 10:
value = 'T';
break;
case 11:
value = 'J';
break;
case 12:
value = 'Q';
break;
case 13:
value = 'K';
break;
default:
if ((iValue >= 2) && (iValue <= 9))
value = '0' + iValue;
break;
}
switch (iSuit)
{
case 1:
suit = 'D';
break;
case 2:
suit = 'S';
break;
case 3:
suit = 'C';
break;
case 4:
suit = 'H';
break;
// Special case for the Joker
case 5:
if(iValue == 1)
{
value = 'Z';
suit = 'B';
}
else if(iValue == 2)
{
value = 'Z';
suit = 'R';
}
else
{
value = '0';
suit = '0';
}
break;
}
PlayingCard testCard(value, suit);
return testCard;
}
Just remove it and then prefix everything that the compiler errors on. :P
With a quick glance, what definitely needs to be prefixed are cout and endl.
Also, please do not use endl to just insert newlines, use plain old '\n' instead. endl not only inserts a newline, but also flushs the stream, which can be quite the performance penalty if used frequently (which you do). Only use it if you need the stream flushed in addition to inserting a newline.
I didn't look over the code with a fine tooth comb, but probably just cout and endl. If the compiler gives you an error about an undefined symbol, try adding std to that too. Incidentally, you could also change using namespace std; to using std::cout; and using std::endl; That would keep the global namespace from being overly polluted, but avoid adding the qualifier to every instance in your code.
The only two I see are cout and endl everything else is a keyword or user defined.
Given the frequency that you use cout, you might want to replace using namespace std; with using std::cout;.
As Xeo says, you should search-and-replace endl with '\n'.
It looks like that might cover all your bases. Fix any other compiler errors by adding std:: as they appear.