What the code does is ask the user which card he wants and prints out a statement depending on which card is chosen.
My aim is to loop back to the card select function if numbers other than 1,2 and 3 are entered.
There is also a for loop which allows this process to go around multiple times.
What is the best way and how can I do this?
int CardSelect() {
cout << "Enter 1 for hearts" << endl;
cout << " " << endl;
cout << "Enter 2 for diamonds" << endl;
cout << " " << endl;
cout << "Enter 3 for joker" << endl;
return 0;
};
int main() {
for (int i = 1; i <= 5; i++) {
CardSelect();
int cardchoice;
cin >> cardchoice;
cardchoice = CardSelect();
if (cardchoice == 1) {
cout << "You got hearts" << endl;
loop = false;
} else if (cardchoice == 2) {
cout << "You got diamonds" << endl;
loop = false;
} else if (cardchoice == 3) {
cout << "You got joker" << endl;
loop = false;
} else {
cout << "Invalid choice" << endl;
cout << "Please ensure you type in the right numbers" << endl;
}
}
}
Change return type of CardSelect() to void, since your simply printing some statements in that function:
void CardSelect()
{ // Your cout statements
}
Call that in main(), and use a switch case for your cardchoice variable.
If you want to keep running the switch statement till you get a valid input, put everything in an inifinte loop (such as a while(1)) and set an exit condition by setting a boolean to true (set it to false initially) and using break when condition is satisified, to get out of the loop:
int main()
{
while(1)
{
bool valid = false;
CardSelect(); // call to your function
int cardchoice;
cin >> cardchoice;
switch(cardchoice)
{
case 1:
cout << "You got hearts" << endl;
valid = true;
break;
case 2:
cout << "You got diamonds" << endl;
valid = true;
break;
case 3:
cout << "You got joker" << endl;
valid = true;
break;
default:
cout << "Invalid choice" << endl;
cout << "Please ensure you type in the right numbers" << endl;
break;
} if(valid) break;
}
}
you should not call cardchoice = CardSelect();.
This call is overwriting cardchoice with 0. Remove this call.
You print values to see what is happening. Its a good way of learning.
Hope this will help.
First, what you are looking for is continue, second you need to get rid of this line which makes no sense :
cardchoice = CardSelect();
as it erases user input
int CardSelect() {
cout << "Enter 1 for hearts" << endl;
cout << " " << endl;
cout << "Enter 2 for diamonds" << endl;
cout << " " << endl;
cout << "Enter 3 for joker" << endl;
return 0;
};
int main() {
for (int i = 1; i <= 5; i++) {
CardSelect();
int cardchoice;
cin >> cardchoice;
if (cardchoice == 1) {
cout << "You got hearts" << endl;
}
else if (cardchoice == 2) {
cout << "You got diamonds" << endl;
}
else if (cardchoice == 3) {
cout << "You got joker" << endl;
}
else {
cout << "Invalid choice" << endl;
cout << "Please ensure you type in the right numbers" << endl;
}
}
}
Related
#include <iostream>
using namespace std;
bool play_game(int n) {
int guess;
bool noguesses = false;
int numofguesses = 0;
cout << "Welcome to my number guessing game\n";
while (n!=guess && !noguesses)
{
if (numofguesses < 6)
{
cout << "\n";
cout << "Enter your guess: ";
cin >> guess;
cout << "\n";
cout << "You entered: " << guess;
numofguesses++;
return false;
}
else
{
oog = true;
}
}
if (noguesses) {
cout << "I'm sorry. You didn't find my number.\n";
cout << "It was" << n << endl;
}
else
{
cout << "\n";
cout << "You found it in" << numofguesses << "guess(es)\n";
return true;
}
}
int main()
{
int secretnum = 5;
play_game(secretnum);
}
When I run this, the program stops after cout << "You entered: " << guess;. I want it to keep looping until the number of guesses reaches 6, or until the user inputs the correct answer.
Remove return false;
if (numofguesses < 6)
{
cout << "\n";
cout << "Enter your guess: ";
cin >> guess;
cout << "\n";
cout << "You entered: " << guess;
numofguesses++;
return false; //Remove this line
}
I know I am probably missing something obvious, but my code will only run the do loop in the main function once. After the selected function (with menu selection) is ran the program exits instead of restarting the loop. I am not sure what is wrong with my loop. Please Advise.
P.S. This program is written in C++
Here is the entire body of the program. All of the functions work with the exception of the do loop in the main function.
//It also includes functions to cancel a selection and to show all seats on the flight.
//These allow the predefined functions within the program to operate.
#include <iostream>
#include <string>
//This sets the namespace of the entire program to std
using namespace std;
//This creates a constant variable for the rows of seats on the flight.
const int rows = 13;
//This creates a constant variable for the seats in each row on the flight.
const int seats = 6;
//This line creates the array to store the seat assignments.
char flight[rows][seats];
//This function displays the seating chart for the flight
int display()
{
cout << "Displaying the current seating assignments for the flight." << endl;
cout << "The '*' symbol means the seat is available and 'X' means it is not." << endl;
cout << " A " << "B " << "C " << "D " << "E " << "F " << endl;
for(int i=0; i < rows; i++)
{
cout << "Row " << i+1 << " ";
for(int j=0; j < seats; j++)
{
cout << flight[i][j] << " ";
}
cout << endl;
}
}
//This function allows the user to assign their seat.
int assign()
{
int type;
cout << "To begin making your seat assignment please select your ticket type." << endl;
cout << "1. First Class" << endl;
cout << "2. Business Class" << endl;
cout << "3. Economy Class" << endl;
cout << "Please select 1, 2, or 3 from the menu." << endl;
cin >> type;
int fcrow;
string fcseat;
if(type == 1)
{
cout << "You selected First Class" << endl;
cout << "Please review available First Class Seats." << endl;
cout << " A " << "B " << "C " << "D " << "E " << "F " << endl;
for(int f = 0; f < 2; f++)
{
cout << "Row " << f+1 << " ";
for(int g = 0; g < seats; g++)
{
cout << flight[f][g] << " ";
}
cout << endl;
}
cout << "Please enter in the desired row (1 or 2)" << endl;
cin >> fcrow;
cout << "Please enter in the desired seat (A-F)" << endl;
cin >> fcseat;
cout << "You selected Row " << fcrow << " Seat " << fcseat << endl;
int fcsi;
if(fcseat == "A")
{
fcsi = 0;
}
if(fcseat == "B")
{
fcsi = 1;
}
if(fcseat == "C")
{
fcsi = 2;
}
if(fcseat == "D")
{
fcsi = 3;
}
if(fcseat == "E")
{
fcsi = 4;
}
if(fcseat == "F")
{
fcsi = 5;
}
flight[fcrow-1][fcsi] = 'X';
cout << "Displaying Updated Flight Seating Chart" << endl;
return display();
}
int bcrow;
string bcseat;
if(type == 2)
{
cout << "You selected Business Class" << endl;
cout << "Please review available Business Class Seats." << endl;
cout << " A " << "B " << "C " << "D " << "E " << "F " << endl;
for(int h = 2; h < 7; h++)
{
cout << "Row " << h+1 << " ";
for(int k = 0; k < seats; k++)
{
cout << flight[h][k] << " ";
}
cout << endl;
}
cout << "Please enter in the desired row (3-7)" << endl;
cin >> bcrow;
cout << "Please enter in the desired seat (A-F)" << endl;
cin >> bcseat;
cout << "You selected Row " << bcrow << " Seat " << bcseat << endl;
int bcsi;
if(bcseat == "A")
{
bcsi = 0;
}
if(bcseat == "B")
{
bcsi = 1;
}
if(bcseat == "C")
{
bcsi = 2;
}
if(bcseat == "D")
{
bcsi = 3;
}
if(bcseat == "E")
{
bcsi = 4;
}
if(bcseat == "F")
{
bcsi = 5;
}
flight[bcrow-1][bcsi] = 'X';
cout << "Displaying Updated Flight Seating Chart" << endl;
return display();
}
int ecrow;
string ecseat;
if(type == 3)
{
cout << "You selected Economy Class" << endl;
cout << "Please review available Economy Class Seats." << endl;
cout << " A " << "B " << "C " << "D " << "E " << "F " << endl;
for(int l = 7; l < rows; l++)
{
cout << "Row " << l+1 << " ";
for(int m = 0; m < seats; m++)
{
cout << flight[l][m] << " ";
}
cout << endl;
}
cout << "Please enter in the desired row (8-13)" << endl;
cin >> ecrow;
cout << "Please enter in the desired seat (A-F)" << endl;
cin >> ecseat;
cout << "You selected Row " << ecrow << " Seat " << ecseat << endl;
int ecsi;
if(ecseat == "A")
{
ecsi = 0;
}
if(ecseat == "B")
{
ecsi = 1;
}
if(ecseat == "C")
{
ecsi = 2;
}
if(ecseat == "D")
{
ecsi = 3;
}
if(ecseat == "E")
{
ecsi = 4;
}
if(ecseat == "F")
{
ecsi = 5;
}
flight[ecrow-1][ecsi] = 'X';
cout << "Displaying Updated Flight Seating Chart" << endl;
return display();
}
if(type != 1 && type != 2 && type !=3)
{
cout << "Error" << endl;
cout << "The Ticket Type Selected does not exist." << endl;
cout << "Exiting Program" << endl;
cout << ". . . . . . . ." << endl;
exit(13);
return(13);
}
}
//This function allows the user to cancel their seat selection.
int cancel()
{
int canrow;
string canseat;
cout << "We are sorry you wish to cancel your seat assignment." << endl;
cout << "Please enter the row of your seat (1-13)." << endl;
cin >> canrow;
cout << "Please enter the seat letter (A-F)." << endl;
cin >> canseat;
cout << "You selected Row " << canrow << " Seat " << canseat << endl;
int canseati;
if(canseat == "A")
{
canseati = 0;
}
if(canseat == "B")
{
canseati = 1;
}
if(canseat == "C")
{
canseati = 2;
}
if(canseat == "D")
{
canseati = 3;
}
if(canseat == "E")
{
canseati = 4;
}
if(canseat == "F")
{
canseati = 5;
}
flight[canrow-1][canseati] = '*';
return display();
}
//This function allows the user to exit the program.
int close()
{
cout << "Exiting Program" << endl;
cout << ". . . . . . . ." << endl;
exit(11);
return(11);
}
//This starts the main program/menu used to run the functions.
int main()
{
int z = 0;
do
{
//These for loops fill the flight array with '*' to show all seats as empty.
for(int a = 0; a < rows; a++)
{
for(int b = 0; b < seats; b++)
{
flight[a][b] = '*';
}
}
z = z+1;
}while (z = 0);
//This creates the LCV and sets it equal to 0 to run the do loop.
int x = 0;
do
{
//These four lines explain what the program does to the user.
cout << "Welcome to Flight Seat Assigner" << endl;
cout << "This program will allow you to select an available seat for your flight" << endl;
cout << "based on the ticket type and available seats." << endl;
cout << "You also can cancel a seat assignment or display the seating chart." << endl;
//This line tells the user to pick a menu option.
cout << "Please select from the following menu to begin." << endl;
//These lines tell the user what the menu options are.
cout << "1. Display Flight Seating Assignments" << endl;
cout << "2. Select a Seat" << endl;
cout << "3. Cancel a Seating Assignment" << endl;
cout << "4. Exit Program" << endl;
//This creates a variable to store the user's menu selection.
int menu = 0;
//This stores the user's selected menu option.
cin >> menu;
//This if statement runs if the user selects the first menu option.
if(menu == 1)
{
//This line runs the display function.
return display();
}
//This if statement runs if the user selects the second menu option.
if(menu == 2)
{
//This line runs the assign funciton.
return assign();
}
//This if statement runs if the user selects the third menu option.
if(menu == 3)
{
//This line runs the cancel function.
return cancel();
}
//This if statement runs if the user selects the fourth menu option.
if(menu == 4)
{
//This line runs the close function.
return close();
}
//This else statement runs if the user enters a non-menu option.
else
{
//These lines explain the user's meu selection mistake and closes the program.
cout << "Error" << endl;
cout << "The option selected does not exist." << endl;
cout << "Please enter a number between 1-4 when using the menu." << endl;
cout << "Closing Program" << endl;
cout << ". . . . . . . ." << endl;
exit (12);
return(12);
}
//This while statement controls when the do loop runs.
}while (x = 0 && x != 0);
}
Here is the code just for the do-while loop in question.
do
{
//These four lines explain what the program does to the user.
cout << "Welcome to Flight Seat Assigner" << endl;
cout << "This program will allow you to select an available seat for your flight" << endl;
cout << "based on the ticket type and available seats." << endl;
cout << "You also can cancel a seat assignment or display the seating chart." << endl;
//This line tells the user to pick a menu option.
cout << "Please select from the following menu to begin." << endl;
//These lines tell the user what the menu options are.
cout << "1. Display Flight Seating Assignments" << endl;
cout << "2. Select a Seat" << endl;
cout << "3. Cancel a Seating Assignment" << endl;
cout << "4. Exit Program" << endl;
//This creates a variable to store the user's menu selection.
int menu = 0;
//This stores the user's selected menu option.
cin >> menu;
//This if statement runs if the user selects the first menu option.
if(menu == 1)
{
//This line runs the display function.
return display();
}
//This if statement runs if the user selects the second menu option.
if(menu == 2)
{
//This line runs the assign funciton.
return assign();
}
//This if statement runs if the user selects the third menu option.
if(menu == 3)
{
//This line runs the cancel function.
return cancel();
}
//This if statement runs if the user selects the fourth menu option.
if(menu == 4)
{
//This line runs the close function.
return close();
}
//This else statement runs if the user enters a non-menu option.
else
{
//These lines explain the user's meu selection mistake and closes the program.
cout << "Error" << endl;
cout << "The option selected does not exist." << endl;
cout << "Please enter a number between 1-4 when using the menu." << endl;
cout << "Closing Program" << endl;
cout << ". . . . . . . ." << endl;
exit (12);
return(12);
}
//This while statement controls when the do loop runs.
}while (x = 0 && x != 0);
Sorry the code is so long, but the program is almost finished. I just need to fix the loop not restarting as far as I can tell.
BessieTheCow's comment led me to the correct solution for the program. I changed the functions to void and removed the returns for the most part. Now the program works great. Thank you for all of your help everyone. It is much appreciated by this programming noob.
I have some issues with looping. I am trying to loop multiple statements. Code is below.
int MenuSelect () {
cout << endl;
cout << YELLOW << "Enter 1 for info" << endl;
cout << " " << endl;
cout << "Enter 2 to Start" << endl;
cout << " " << endl;
cout << "Enter 3 to view settings" << endl;
cout << " " << endl;
cout << "Enter 4 to quit" << endl;
int selected = 0;
string input;
cin >> input;
if (stringstream(input) >> selected) {
return selected;
}
else {
return -1;
}
return 0;
};
int Menu(int) {
int selected {};
while ((selected = MenuSelect()) == 1) {
printmessage();
}
if (selected == 3) {
cout << "Current Settings" << endl;
somefunction();
}
else if (selected == 2) {
cout << "Starting Game..... " << endl;
}
else if (selected == 4) {
cout << "Exiting....." << endl;
exit (3);
}
else {
cout << "Invalid Entry" << endl;
exit (3);
}
cout << "Below is the Deck of cards and you will get to choose 5 cards to play with. Choose wisely." << endl;
return 0;
};
So as you can see the user can see the menu, then if and else statements do the work. At the moment I have managed to loop the first menu select so if the user enters 1 it will print the message and then loop back to the menu. What I want is to also loop the 3 - current settings and if the user enters a invalid number. I've tried to accomplish it but I can't seem to do it.
You probably want to repeatedly read user input and react accordingly? You can put all the ifs into the while:
int Menu(int) {
int selected {};
bool loop = true;
while (loop) {
selected = MenuSelect();
if(selected == 1) {
printmessage();
}
else if (selected == 3) {
cout << "Current Settings" << endl;
somefunction();
}
else if (selected == 2) {
cout << "Starting Game..... " << endl;
}
else if (selected == 4) {
cout << "Exiting....." << endl;
loop = false;
}
else {
cout << "Invalid Entry" << endl;
loop = false;
}
}
cout << "Below is the Deck of cards and you will get to choose 5 cards to play with. Choose wisely." << endl;
return 0;
};
Using a variable loop to exit the while is just personal preference, you could as well have an endless loop
while(true)
and use break; to exit the loop or exit() to exit the whole program, as you do now.
Really sorry if this is a dumb question. I know it must have a super easy solution but I've been staring at this for so long I can't see it. It doesn't help that I'm really new at this either.
Long story short for some reason entering an invalid input past the first time returns me back to my menu, and sometimes also asks me to enter weight immediately after instead of allowing me to enter a menu choice. It's just all around broken and I don't know why. Thanks.
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
bool loopFlag = true;
bool loopFlagTwo = true;
int choice = 0;
int time = 0;
float weightPounds = 0;
float weight = 0;
const int BIKING = 8;
const int RUNNING = 10;
const int LIFTING = 3;
const float YOGA = 2.5;
int main()
{
cout << "Welcome to my Fitness Center" << endl;
do
{
cout << "\n\t____________________________________________________________" << endl;
cout << "\n\t\t\tMy Fitness Center" << endl;
cout << "\t\t\tActivity System" << endl;
cout << "\t____________________________________________________________" << endl;
cout << "\t\t\t Main Menu\n" << endl;
cout << "\t\t\t1) Stationary Bike" << endl;
cout << "\t\t\t2) Treadmill" << endl;
cout << "\t\t\t3) Weight Lifting" << endl;
cout << "\t\t\t4) Hatha Yoga" << endl;
cout << "\t\t\t5) End" << endl;
cout << "\t____________________________________________________________" << endl;
cout << "\n\nEnter the workout that you wish to track, or end to exit:" << endl;
do
{
cin >> choice;
if (cin.fail() || choice > 5 || choice < 1)
{
cout << "Invalid choice. Please choose from option 1 through 5." << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
else if (choice == 5)
{
return 0;
}
else
{
loopFlag = false;
}
}
while (loopFlag);
do
{
cout << "\nPlease enter your weight in pounds: " << endl;
cin >> weightPounds;
if (cin.fail() || weightPounds <= 0)
{
cout << "Invalid weight entry!" << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
else
{
loopFlag = false;
}
}
while (loopFlag);
weight = weightPounds / 2.2;
cout << "\nYour weight is: \n" << fixed << setprecision(1) << weight << " kilograms." << endl;
if (choice == 1)
{
do
{
cout << "For how many minutes did you do this activity? " << endl;
cin >> time;
if (cin.fail() || time <= 0)
{
cout << "Invalid time entry!" << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
else
{
loopFlag = false;
}
}
while (loopFlag);
}
}
while (choice != 5);
return 0;
}
You need to set loopFlag to true before every do...while() you have, or use another flag, because after the first do...while(), loopFlag is always false.
I am current creating a weather application that is giving me an error. It said there is something wrong with != but I am not sure what is wrong so can anyone help. It is giving me the error operand types are incompatible ("std::string *" and "const char *")(there is 4) and '!=': no conversion from 'const char *' to 'std::string *'
Thank you
C++ code:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void moveTemperaturesToRight(double temperatures[], double windSpeed[], string windDirection[])
{
for (int i = 3; i > 0; i--)
{
temperatures[i] = temperatures[i - 1];
windSpeed[i] = windSpeed[i - 1];
windDirection[i] = windDirection[i - 1];
}
}
int main()
{
string name;
int choice;
int numOfReadings = 0;
double temperatures[4], windSpeeds[4];
string windDirections[4];
bool initialized = false;
string str;
//Have the user provide a name for the weather station upon entry.
cout << "Enter the name of weather station: ";
getline(cin, name);
//Control loop to perform various actions.
while (true)
{
cout << "1. Input a complete weather reading." << endl;
cout << "2. Print the current weather." << endl;
cout << "3. Print the weather history (from most recent to oldest)." << endl;
cout << "4. Exit the program." << endl;
cout << "Enter your choice: ";
cin >> str;
if (str.length() != 1 || str < "1" || str > "4")
choice = 0;
else
choice = atoi(str.c_str());
//Switch based on choice.
switch (choice)
{
case 1: moveTemperaturesToRight(temperatures, windSpeeds, windDirections);
do {
cout << "Enter the temperature (a value >=0):";
cin >> temperatures[0];
} while (temperatures < 0);
//get correct wind speed
do
{
cout << "Enter the wind speed (a value >=0):";
cin >> windSpeeds[0];
} while (windSpeeds < 0);
//get correct wind direction
do
{
cout << "Enter the wind direction (North,South,East or West):";
cin >> windDirections[0];
} while (windDirections != "North" && windDirections != "South" && windDirections != "East" && windDirections != "West");
initialized = true;
if(initialized)
numOfReadings++;
if (numOfReadings > 4)
numOfReadings = 4;
break;
case 3: //Print the current weather, if valid weather is entered.
for (int i = 0; i < numOfReadings; i++)
{
cout << "*****" << name << "*****" << endl;
cout << "Temperature: " << temperatures[i] << endl;
cout << "Wind speed: " << windSpeeds[i] << endl;
cout << "Wind direction: " << windDirections[i] << endl << endl;
}
if (numOfReadings == 0)
cout << "Please enter the details before asking to print." << endl;
break;
case 2: if (numOfReadings == 0)
{
cout << "Please enter the details before asking to print." << endl;
break;
}
cout << "*****" << name << "*****" << endl;
cout << "Temperature: " << temperatures[0] << endl;
cout << "Wind speed: " << windSpeeds[0] << endl;
cout << "Wind direction: " << windDirections[0] << endl << endl;
break;
case 4: return 0; //Stops execution.
default: cout << "Invalid choice. Please follow the menu." << endl;
}
}
}
You need to compare an element of windDirections with the literal.
Did you mean windDirections[0] != "North" &c.?
Currently you're attempting to compare an array of std::strings, and so the compiler issues a diagnostic. It does its best in decaying the array to a pointer to std::string (hence the specific error), but then gives up.