I am currently making a program that has menu and sub menus and i'd like to have an opportunity to close entire thing from within any sub menu (which is inside function). I know i could do it different way (both way i have it now and way i know it could be done are below) but for future use (i could prove useful) i would like to know:
Is there a way to properly close the program from within a function ?
I've read a few answers here and here and from what i've gotten out of it is:
I should not simply terminate the code
I should not use any system specific code as to avoid compatibilty issues
A bit of clarifiacation - that is program for my personal educational purposes so it is kind of unnecessary to worry about it but i'd rather learn propper ways of dealing with those situations at the beginning than have to learn them anew once i discover how to do them later and have to go back and fix it.
As for code samples (using visual studio 2017):
//skipping unrelevant to question bits of code
void sub_menu()
{
char a=0;
system("cls"); //yet to get changed when i'll find other way of clearing screen
std::cout << "Pick function:\n"
<< "---------------------------------------------------------\n"
<< "1.Foobar_1\n"
<<"---------------------------------------------------------\n"
<< "ESC to go back to menu menu\n"
<< "x to close";
while (a != 27)
{
a = _getch();
switch (a)
{
case 49:foobar_1(); break;
case 120:
case 88: system("exit"); break; //i'd like to replace that part
default: break;
};
};
return;
}
void main()
{
char a=0;
system("cls"); //yet to get changed when i'll find other way of clearing screen
std::cout << "Pick what you want to do:\n"
<< "---------------------------------------------------------\n"
<< "1.Sub_menu\n"
<< "2.foo\n"
<< "3.bar\n"
<<"---------------------------------------------------------\n"
<< "ESC to close\n"
while (a != 27)
{
a = _getch();
switch (a)
{
case 49:sub_menu(); break;
case 50:foo(); break;
case 51:bar(); break;
case 120:
case 88: system("exit"); break; //i'd like to replace that part
default: break;
};
};
return;
}
The way i could implement it (i think it is merely byapssing the issue):
//skipping unrelevant to question bits of code
void main()
{
char a=0;
system("cls"); //yet to get changed when i'll find other way of clearing screen
std::cout << "Pick what you want to do:\n"
<< "---------------------------------------------------------\n"
<< "1.Sub_menu\n"
<< "2.foo\n"
<< "3.bar\n"
<<"---------------------------------------------------------\n"
<< "ESC to close\n"
while (a != 27)
{
a = _getch();
switch (a)
{
case 49:
{
{
char b=0;
system("cls");
std::cout << "Pick function:\n"
<< "---------------------------------------------------------\n"
<< "1.Foobar_1\n"
<<"---------------------------------------------------------\n"
<< "ESC to go back to menu menu\n"
<< "x to close";
while (b != 27)
{
b = _getch();
switch (b)
{
case 49:foobar_1(); break;
case 120:
case 88: a=27; break;
default: break;
};
};
return;
}
}
; break;
case 50:foo(); break;
case 51:bar(); break;
case 120:
case 88: system("exit"); break; //i'd like to replace that part
default: break;
};
};
return;
}
This Code will work as you mentioned.
int main()
{
int a;
while (true)
{
system("cls"); //yet to get changed when i'll find other way of clearing screen
cout << "Pick what you want to do:\n"
<< "---------------------------------------------------------\n"
<< "1.Sub_menu\n"
<< "2.foo\n"
<< "3.bar\n"
<< "---------------------------------------------------------\n"
<< "0. to close\n";
cin >> a ;
if (a == 0)
{
return 0 ;
}
else if (a == 1)
{
char b;
while (true)
{
system("cls");
cout << "Pick function:\n"
<< "---------------------------------------------------------\n"
<< "1.Foobar_1\n"
<< "---------------------------------------------------------\n"
<< "b to go back to menu menu\n"
<< "x to close\n";
cin >> b;
if (b == '1')
{
foobar_1();
}
else if (b == 'B' || b == 'b')
{
break;
}
else if (b == 'x' || b == 'X')
{
return 0;
}
else
{
cout << "invalid input";
}
}
}
else if (a == 2)
{
foo();
}
else if (a == 3)
{
bar();
}
else
{
cout << "invalid input";
}
}
return 0;
}
Related
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.
I'm making a maze game that uses a player structure and takes user input. The program runs, but nothing changes when I input commands to move my player. Any advice on whats going wrong?
// The function for user input
void getMove()
{
Player player1;
cout << "\n\nWhich direction would you like to go? ----- Treasure: " << numTreasure << "\n\n";
cout << "Up: 8\n";
cout << "Down: 2\n";
cout << "Left: 4\n";
cout << "Right: 6\n";
cin >> direction;
switch(direction)
{
case 8 :
if (currentBoard[player1.playerX - 1][player1.playerY] != '#'){
player1.playerX--;
}
break;
case 2 :
if (currentBoard[player1.playerX + 1][player1.playerY] != '#'){
player1.playerX++;
break;
}
case 4 :
if (currentBoard[player1.playerX][player1.playerY - 1] != '#'){
player1.playerY--;
break;
}
case 6 :
if (currentBoard[player1.playerX][player1.playerY + 1] != '#'){
player1.playerY++;
break;
}
}
switch(currentBoard[player1.playerX][player1.playerY]){
case '*':
cout << "You picked up a gold nugget!" << endl;
board[player1.playerX][player1.playerY] = treasureSpace;
numTreasure++;
break;
}
}
`
Your direction variable that is read in with cin >> is of type char*. In your switch-case you are comparing this direction variable against integers, so you will never get any of these cases AND you do not have a default case. I would switch the direction that is brought in from the user to an int with a type cast
`int_direction = (int)direction`
and then change your switch cases to compare against int_direction
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;
}
how do I run only one of those if statements in a for loop? For example i have an input of 5...and i just want it to print five...but whenever i run this code, it will execute all if statement..please help me
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
// Complete the code.
int a;
int b;
cin >> a;
for (a = 0; 0<a<10; a++)
{
if (a == 1)
{
cout << "one";
}
if (a == 2)
{
cout << "two";
}
if (a == 3)
{
cout << "three";
}
if (a == 4)
{
cout << "four";
}
if (a == 5)
{
cout << "five";
}
if (a == 6)
{
cout << "six";
}
if (a == 7)
{
cout << "seven";
}
if (a == 8)
{
cout << "eight";
}
if (a == 9)
{
cout << "nine";
}
else if (a > 9 && a%2 == 0)
{
cout << "even";
}
else if (a > 9 && a&2 != 0)
{
cout << "odd";
}
}
return 0;
}
The problem seems to be the for loop. Your program accepts a value for a as an input, but then as soon as the loop begins, it resets the value of a to 0 (for (a = 0;...
Therefore it's looping 10 times, and on each loop a will have a different value, starting from 0 and ending at 9. This means that all of your if statements will get hit at some point in the execution, generally one on each of the loops round the for.
To get your expected behaviour " input of 5...and i just want it to print five", simply remove the for loop from your code.
Your unnecessary for loop trashes the input value of a and loops forever! (At least until you overflow your signed type a).
You are replacing a by using it as the counter in the for loop! If you only ever want one output, then drop the for loop completely. If your for loop were to remain then your expression 0 < a < 10 ought to be recast as 0 < a && a < 10 : formally 0 < a < 10 is evaluated as (0 < a) < 10 which is either true < 10 or false < 10 which is always true.
Also consider refactoring your if else to set up explicitly mutually exclusive statements:
if (a == 1){
cout << "one";
} else if (a == 2){
cout << "two";
/*and so on*/
} else {
/*all other cases*/
}
Although in this case you might want to consider a switch block:
switch (a){
case 1:
cout << "one";
break; // to stop program control flowing into the next case
case 2:
cout << "two";
break;
/*and so on*/
default:
/*all other cases*/
}
if () {
} else if () {
}
Although,
switch() {
}
will be more efficient in your case.
Update 1
#Aleph0
Below is the solution
int main() {
int a;
cin >> a;
switch (a) {
case 1: cout << "one"; break;
case 2: cout << "two"; break;
case 3: cout << "three"; break;
case 4: cout << "four"; break;
case 5: cout << "five"; break;
case 6: cout << "six"; break;
case 7: cout << "seven"; break;
case 8: cout << "eight"; break;
case 9: cout << "nine"; break;
default: cout << ((a & (1 << 31)) ? "negative" : (a & 1) ? "odd" : "even"); break;
}
}
Question has been asked to do following
i have an input of 5...and i just want it to print five
And, someone has correctly mentioned above, for loop is immaterial here.
So I'm working on a little bit of a project involving AI, and I have run into a problem. The program should use a for loop to search the index of common known phrases, and if they don't match the sentence, then in the else statement it figures out which sentence model to apply, then generates output using that sentence model. Here are the built in phrases, they are stored in arrays:
string greetings[6] = {"hey", "hello", "hi", "hey max", "hello max", "hi max"};
string howsItGoing[6] = {"hows it going?","how's it going max?","hows it going max?", "whats up?", "whats up max?"};
string whatsYourName[6] = {"whats your name?", "what's your name?" , "whats your name max?", "what's your name max?"};
string dogsOrCats[6] = {"do you like dogs or cats?", "do you like cats or dogs?", "which do you like more cats or dogs?", "which do you like more dogs or cats?"};
string areYouAlive[6] = {"hey max are you alive?", "hey are you alive?", "max are you alive?", "are you alive?"};
string favoriteColor[6] = {"whats your favorite color?", "what's your favorite color?", "what is your favorite color?", "max whats your favorite color?", "max what's your favorite color?", "max what is your favorite color?"};
string whatGender[6] = {"are you a boy or a girl?", "are you boy or girl?", "are you a guy?", "max are you a boy or girl?", "max are you a guy?", "are you a boy or girl?"};
(This is a temporary method, I'm devising a little thing called a Sentence Decoder that will eventually replace all built in phrases.)
So my problem is this: In the for loop, the first if statement can search the entire array and respond with a suitable output, whether the input was greetings[0] or greetings [4]. However, all the else-if statements appear to be just looking for index 0. I believe this has to do with the for loop only searching across the first one 6 times, but I'm mostly likely wrong. I tried increasing the for loop end-number from 6 to 40 to see if that would help, but alas it didn't make a difference. Here is the rest of the code (It's so ugly! I need to make a function for it or something.... but then again, the whole thing itself is a function...
for (int i = 0; i < 6; i++) {
//Checking if it fits in any of the known greetings
if (input == database.greetings[i]) {
if (timesBeenGreeted == 0) {
cout << "Oh, hey." << endl;
timesBeenGreeted++;
break;
}
else if (timesBeenGreeted == 1) {
cout << "hello?" << endl;
timesBeenGreeted++;
break;
}
else if (timesBeenGreeted == 2) {
cout << "You've said hi to me twice now." << endl;
timesBeenGreeted++;
break;
}
else if (timesBeenGreeted >= 3) {
cout << "Stop. It." << endl;
timesBeenGreeted++;
break;
}
i = 0;
continue;
}
else if (input == database.howsItGoing[i]){
if (askedHowsItGoing == 0) {
cout << "Yeah, not bad." << endl;
askedHowsItGoing++;
break;
}
else if (askedHowsItGoing == 1) {
cout << "Um, yeah, I'm good? You've already asked that." << endl;
askedHowsItGoing++;
break;
}
else if (askedHowsItGoing == 2) {
cout << "Alright, look. I already told you I'm fine, two times now." << endl;
askedHowsItGoing++;
break;
}
else if (askedHowsItGoing >= 3) {
cout << "STOP ASKING ME IF IM OKAY!" << endl;
askedHowsItGoing++;
break;
}
continue;
}
else if (input == database.whatsYourName[i]){
if (askedName == 0) {
cout << "My name's Max, and I'm going to become the worlds most advanced AI." << endl;
askedName++;
break;
}
else if (askedName == 1) {
cout << "I already told you, my names Max." << endl;
askedName++;
break;
}
else if (askedName == 2) {
cout << "For the third time, my name is Max!" << endl;
askedName++;
break;
}
else if (askedName >= 3) {
cout << "Ugh... my name.... is.... max....for the fourh time...." << endl;
askedName++;
break;
}
continue;
}
else if (input == database.dogsOrCats[i]) {
if (askedDogsOrCats == 0) {
cout << "I've never seen a dog or cat before. But I do know they are kinds of animals." << endl;
askedDogsOrCats++;
break;
}
else if (askedDogsOrCats == 1) {
cout << "As I've said, I haven't seen either of them before. Sorry, but that's all I can say." << endl;
askedDogsOrCats++;
break;
}
else if (askedDogsOrCats == 2) {
cout << "Uh, for the third time, I've never seen either of them..." << endl;
askedDogsOrCats++;
break;
}
else if (askedDogsOrCats >= 3) {
cout << "Man, you are persistant. I haven't seen dog or cat, so I couldn't choose between the two." << endl;
askedDogsOrCats++;
break;
}
continue;
}
else if (input == database.areYouAlive[i]){
if (askedAlive == 0) {
cout << "Nope, I'm just a computer program. I'm not even that intelligent. But I will be!" << endl;
askedAlive++;
break;
}
else if (askedAlive == 1) {
cout << "Nah, I'm just a simple computer program." << endl;
askedAlive++;
break;
}
else if (askedAlive == 2) {
cout << "Uh, you asked that twice before now. Nothing alive about me." << endl;
askedAlive++;
break;
}
else if (askedAlive == 3) {
cout << "Stop asking me that! No, I'm not!" << endl;
break;
}
continue;
}
else if (input == database.favoriteColor[i]) {
if (askedColor == 0) {
cout << "I've never seen any colors before. All though, my programmer does like the color blue." << endl;
askedColor++;
break;
}
else if (askedColor == 1) {
cout << "Couldn't say, never seen any before. My programmer likes blue though." << endl;
askedColor++;
break;
}
else if (askedColor == 2) {
cout << "As I've said twice before now, never seen any colors." << endl;
askedColor++;
break;
}
else if (askedColor == 3) {
cout << "Come on man, this is the fourth time now. I've never seen any colors before!" << endl;
break;
}
continue;
}
else if (input == database.whatGender[i]) {
if (askedGender == 0) {
cout << "Well, seeing as I'm a computer program, I'm neither really. Although, my programmer says I'm a 'guy'." << endl;
askedGender++;
break;
}
else if (askedGender == 1) {
cout << "I'm neither, really. Although, my name is Max and my programmer says I'm a guy." << endl;
askedGender++;
break;
}
else if (askedGender == 2) {
cout << "Third time you've asked now. My programmer says I'm a guy." << endl;
askedGender++;
break;
}
else if (askedGender == 3) {
cout << "Seriously?! You've asked me 4 times now. I'M A GUY!" << endl;
break;
}
continue;
}
//If it doesnt, check each word against words in the database.
else {
if (inputWords[5] != "") {
Model1 model;
model.searchAndAssign(input, inputTokens, inputWords);
model.generateOutput();
for (int i= 0; i < 20; i++) {
inputWords[i] = "";
}
break;
}
else if (inputWords[2] != "") {
Model2 model2;
model2.searchAndAssign(input, inputTokens, inputWords);
model2.generateOuput();
for (int i= 0; i < 20; i++) {
inputWords[i] = "";
}
break;
}
}
}
My question is this: How can I solve this problem?
Thank you in advanced guys!
EDIT I've since fixed the problem: something was playing up in my main.cpp file (this was the lingusticsModule.cpp file....) that was causing the issue. Sorry guys, but thanks for the help anyway!
At the end of your first if you reset the loop iterator and skip everything else.
i = 0;
continue;
Your else ifs may match but if the increments have been reached, then they just continue which doesn't make much sense. What are you hoping to find?
else if (askedGender == 3) {
cout << "Seriously?! You've asked me 4 times now. I'M A GUY!" << endl;
break;
}
continue;
You find this behavior confusing because it is. It might make more sense to do something like:
else {
++askedGender;
std::cout << "Seriously?! You've asked me "
<< askedGender << " times now. I'M A GUY!\n";
}