C++ need help, random switch statements [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm writing a code which is kind of like a fortune teller but I'm having some trouble with my switch statements. When executed the code prints out the same message and doesn't pick a random case like its supposed to! can someone please help me! thank you!
heres my code
#include<iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
void printGreeting(); // function prototype
int main()
{
int choice;
printGreeting();
cout << "Would you like to see your fortune?" << endl;
cout << "Press 1 to see your fortune or 2 if you don't!" << endl;
cin >> choice;
if (choice == 1)
{
cout << "Great! Your fortune is: ";
// Function to generate random number
void rand1();
srand(time(NULL));
int MAX_NUM;
MAX_NUM = 5;
int random = rand() % MAX_NUM;
cout << random << endl;
int selection;
selection = 5;
switch (selection)
{
case 1:
cout << "Change can hurt, but it leads a path to something better!";
break;
case 2:
cout << "If you have something good in your life don't let it go!";
break;
case 3:
cout << "You're in for a treat today.";
break;
case 4:
cout << "Land is always on the mind of a flying bird";
break;
case 5:
cout << "A dream you have will come true";
break;
}
return 0;
}
else if (choice == 2)
{
cout << "Okay goodbye!" << endl;
}
}
// Prints greeting message
void printGreeting() // function header
{
cout << "Hello! Welcome to your fortune teller!" << endl; // function body
}

Because selection = 5;
You want to choose selection with a random value between 1 - 5, right?

You switch by selection variable, which is explicitly set to 5 right before the switch itself. Consider switching by random variable.

Related

C++ input error with switch statment and function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I don't know why but my code is not working when I sign out a book and click a number and then try to sign in a book it says I have the number one choice(Python book) even if i click any other option. I don't know why.It's probably something dumb so please go easy on me.
#include <iostream>
using namespace std;
string book;
void menu(){
cout << "*********Menu*********";
cout << "\n1) Sign out book";
cout << "\n2) sign in book";
cout << "\n3) See my books";
cout << "\n4) Exit";
}
void book1(){
book = {"Computer Science"};
}
void book2(){
book = {"Programming with C++"};
}
void book3(){
book = {"Python Programming"};
}
int main(){
//Vars
int choice;
do
{
menu();
cin >> choice;
switch (choice)
{
case 1:
int book_choice;
cout << "\n**********************";
cout << "\n1) Computer Science";
cout << "\n2) Programming with C++";
cout << "\n3) Python Programming";
cin >> book_choice;
switch (book_choice)
{
case 1:
book1();
case 2:
book2();
case 3:
book3();
default:
break;
}
break;
case 2:
cout << "\n**********************";
cout << "\nSigned out books: ";
cout << book;
//add books
case 3:
cout << "\n**********************";
default:
break;
}
} while (choice != 4);
}
The error lies in the book_choice switch:
switch (book_choice)
{
case 1:
book1();
// missing break;
case 2:
book2();
// missing break;
case 3:
book3();
// missing break;
default:
break;
}
You forgot to use break; after each case. The default behaviour in a switch is to fall through and execute the next case. That means you always end up executing book3() and setting your book to "Python Programming".

Function of Base Number 10 to 2,8,16 Ploblem? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I Beginner to Coding and English language maybe not good. (I'm living Bangkok,Thailand)
I created Function Base number 10 to 2,8,16 by using with Switch Case.
You can see my code C++ at here :
https://gist.github.com/anonymous/5d31d216c85194470def16d31a2b97cf
Ploblem
When i was run the program,i selected number Case 1 , 2 , 3 << it can't work,if i selected Case 4 to End Program is work.
I don't know that ploblem i thought write wrong code ?
Thank you.
One Problem is, as Tyger mentioned that you didn't initialize the x, you could get that from a cin like in the menu funciton.
The other problem is that even though you get the string form the functions, you doesn't write it out. So your main function should look something like this:
int main(){
string out; // Do you realy need the out string here?
int mod,x; // or the mod here
int choice;
cout << "Give x: " << endl;
cin >> x;
out = " ";
do{
menu(choice);
switch(choice)
{
case 1 : cout << base10to2(x) << endl; break;
case 2 : cout << base10to8(x) << endl; break;
case 3 : cout << base10to16(x) << endl; break;
}
}
while(choice != 4);{
cout<<"End Program / Thank You";
}
return 0;
}

What does it mean by "error: not declared in this scope?" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
#include <iostream>
#include <string>
using namespace std;
int main ()
{
do
{
string name, answer;
cout << "Welcome to the prime number checker! Please enter your name: ";
getline (cin, name);
int a;
cout << "\nHello " << name;
cout << "\nPlease enter an integer: ";
cin >> a;
cin.sync();
if (a == 2)
{
cout << "\nThis is a prime number" << endl;
}
else
{
for (int b = 2; b < a; b++)
{
if (a % b == 0)
{
cout << "This number is not prime number" << endl;
break;
}
else
{
cout << "This number is a prime number." << endl;
break;
}
}
}
cout << "Do you want to do this again (Yes or No)?";
getline (cin, answer);
}
while (answer == "yes" || answer == "YES" || answer == "Yes"); //Not declared in this scope
return 0;
}
You declared answer within the do block. But then try to reference answer outside of that scope block.
Declare answer at the top of main instead of in the do block.
You need to move the declaration of answer outside the loop:
string answer;
do {
string name;
...
} while (answer == "yes" || answer == "YES" || answer == "Yes");
If you declare it inside the loop, it no longer exists by the time the while clause is evaluated.
As other people said, the "answer" variable only exists inside the loop - it isn't accessible from outside it.
One other recommendation: rather than checking every possible permutation of capitalization just cast the whole string to lowercase. (You actually missed several - there are 6 total because each position could have one of 2 possible values. Presumably something like "YeS", for example, should still be accepted as "yes").

C++ switch not working with more than 2 cases [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm learning C++ so sorry for newbie question.
I'm doing exercises from S. Prata's Book. I'm currently on 6.4.
There is the code I've written:
#include <iostream>
using namespace std;
void showmenu();
void request();
const int strsize = 20;
const int templeSize = 5;
struct temple {
char name[strsize];
char job[strsize];
char psd[strsize];
int preference;
};
int main(){
temple members[templeSize] = {
{"Alan", "spy", "Kret", 0},
{"Bruce", "engi", "Mech", 2},
{"Zac", "engi", "Robot", 0},
{"Kevin", "teacher", "Kid", 1},
{"Maverick", "spy", "Shadow", 2}
};
char choice;
showmenu();
request();
cin >> choice;
while (choice != 'q'){
switch(choice){
case 'a' : for(int i; i< templeSize; i++)
cout << members[i].name << endl;
break;
case 'b' : for(int i; i< templeSize; i++)
cout << members[i].job << endl;
break;
case 'c' : for(int i; i< templeSize; i++)
cout << members[i].psd << endl;
break;
case 'd' : for(int i; i < templeSize;i++){
switch(members[i].preference){
case 0: cout << members[i].name; break;
case 1: cout << members[i].job; break;
case 2: cout << members[i].psd; break;
}
}
default : request();
}
showmenu();
cin >> choice;
}
cout << "\nBye!\n";
return 0;
}
void request(){
cout << "Choose one option:\n";
}
void showmenu(){
cout << "a. names b. jobs\n"
"c. psds d. preferences\n"
"q. Quit\n";
}
I have no ide what is wrong with that. Code is compiling (I'm using code::blocks), but only for cases 'a' and 'b'. When I input 'c' or 'd' it just showing menu again. Same if I choose a/b more than once.
I've found other solution via google, but I realy want to know what is wrong with my code.
i is not initialized in any of your case statement for loops
You are calling showmenu() outside of your switch statement. So no matter what the input you will leave the switch and call the function.
switch(choice){
...
}
showmenu();
...

C++ function that returns value into switch statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Currently I have a working program, but am trying different things to see if they work. below is the code I am trying to get to work but having a function that asks the user to make a selection. I want to return that value into my main function which will run a do while loop that includes a case. Originally I have it as an if else if statement, but want to cut that down if possible. Below is the code I am working with. When running the program it only returns the entire menu instead of running the function from the value that should be returned.
{
int choice;
createEmptySeats();
seatPrice();
do {
choice = menu();
switch (choice)
{
case '1':
reserveSeat();
break;
case '2':
displayInfo();
break;
case '3':
DisplaySummary();
break;
case '4':
break;
}
}
while (!seatFull());
return 0;
}
int menu()
{
int userSelection = -1;
while (userSelection < 1 || userSelection > 4)
{
cout << "Please Choose Option Below" << endl;
cout << "1: Reserve Seat(s)"<<endl;
cout << "2: Display Available Seats" << endl;
cout << "3: Display Information:" << endl;
cout << "4: Exit System" << endl;
cin >> userSelection;
}
return userSelection;
}
First: You're missing break; in your switch statement for each of the cases, except case 4 (not sure if it's by design).
Second: Choice is int, your cases are chars. Change one of them, so that both of them match. You're never finding a match for your case, so you just keep on looping.
choice is int, and you write char - '1', '2' in the case. Write as follows:
choice = menu();
switch (choice)
{
case 1:
reserveSeat();
break;
case 2:
displayInfo();
break;
// ...
In your code you can come to case '1' when choice is an ASCII code of '1', i.e. 49. In your code it could never happen, so you stuck in the infinite loop with menu().