Unable to understand the flow of the program - c++

I am unable to understand as to why the switch block is not executed. I am trying to generate random numbers between 0 and 2 using rand() within comp_in() function. I return the number to the main function. Within the main function, I am trying to associate a char to each letter generated. The switch statement is not executed at all. Please help!
#include<iostream>
using namespace std;
int comp_in();
int main()
{
char h;
h = human_in();
int c = comp_in();
cout << "c is" << c << endl;
switch(c)
{
case '0' : cout << "Computer's choice is : 'R'" << endl;
break;
case '1' : cout << "Computer's choice is : 'P'" << endl;
break;
case '2' : cout << "Computer's choice is : 'S'" << endl;
break;
}
}
int comp_in()
{
int s;
for(int i=0; i<4; i++)
{
s=rand()%3;
}
cout << "s is : " << s << endl;
return s;
}
Output:-
s is : 1
c is1

The problem is that your comp_in function returns numbers, but your switch is comparing its result to characters. Simply remove the single quotes from each case, making them numbers, and it'll work:
switch(c)
{
case 0 : cout << "Computer's choice is : 'R'" << endl;
break;
case 1 : cout << "Computer's choice is : 'P'" << endl;
break;
case 2 : cout << "Computer's choice is : 'S'" << endl;
break;
default: cout << "Computer made a really strange choice: " << c << endl;
break;
}
Do note that at some point in the future, you might want to compare the human input with the computer input. Since your human_in function returns a character, you're going to have to convert it by using a function like atoi.
You can detect bugs like these more quickly if you output some sort of debug message in a default case, which I've also included in the code sample above.

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.

Returning the output of a function as the return code of main()

I'm self-teaching myself here so I don't have a model answer available.
Working through program flow examples and trying to get a number guesser based on binary searching. I've got it to run and catch edge cases successfully but one objective is to have main() return the number of guesses made. I refactored the main code into a separate function to make it clearer, but I can't get the return code correct, I suspect it's to do with variable scope but can't figure it out.
#include <iostream>
using namespace std;
int guessNumber(int highest, int lowest, int lAttempts)
{
int guess = lowest + ((highest - lowest) * 0.5);
char response = 'a';
lAttempts++;
cout << "My guess is " << guess << ", am I correct?" << endl;
cout << "(y)es/too (h)igh/too (l)ow/(q)uit" << endl;
cin >> response;
while (response != 'y' && response != 'h' && response != 'l' && response != 'q')
{
cout << "I'm sorry, I didn't understand that" << endl;
cout << "(y)es/too (h)igh/too (l)ow/(q)uit" << endl;
cin >> response;
}
switch (response)
{
case 'y':
cout << "I guessed correctly after " << lAttempts << " attempts";
break;
case 'h':
highest = guess;
guessNumber(highest, lowest, lAttempts);
break;
case 'l':
lowest = guess;
guessNumber(highest, lowest, lAttempts);
break;
case 'q':
cout << "Exiting program";
break;
}
return lAttempts;
}
int main()
{
cout << "Think of a number between 1-100" << endl;
int highest = 100;
int lowest = 0;
int attempts = 0;
attempts = attempts + guessNumber(highest, lowest, attempts);
return attempts;
}
cout returns the correct number of attempts but the program (so main()) always exits with 1.
What am I missing here?
Thanks.
You're missing to update your attempt variable within your switch statements.
It should be like this.
lAttempts = guessNumber(highest, lowest, lAttempts);

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*/
}
}

Character in Switch-Statement C++

Please help! I can't produce the output of my program. This is the condition:
Construct a program that gives a discount of 100 pesos if the shirt bought is XL and the the price is greater than 500; and a discount of 50 pesos if the shirt bought is L and the price is greater than 600.
#include <iostream>
using namespace std;
int main()
{
int p;
int s;
cout << "Input price: ";
cin >> p;
cout << "Input size: ";
cin >> s;
switch (s)
{
case 'XL': case 'xl':
{
if (p>500){
cout << "Total price: " << p-100 << " pesos.";
break;
}
else if ((s=='XL' || s=='xl') && (p<500)){
cout << "Total price: " << p << " pesos.";
break;
}
}
case 'L': case 'l':
{
if (p>600){
cout << "Total price: " << p-50 << " pesos.";
break;
}
else if ((s=='XL' || s=='xl') && (p<600)){
cout << "Total price: " << p << " pesos.";
break;
}
}
case 'M': case 'm':
{
cout << "Total price: " << p << " pesos.";
break;
}
case 'S': case 's':
{
cout << "Total price: " << p << " pesos.";
break;
}
}
return 0;
}
The output of the program:
Input price: 500
Input size: XL
Process returned 0 (0x0) execution time : 5.750 s
Press any key to continue.
P.S. How can I remove the warning (multi-character character constant) in my program?
Thanks in advance!
If the size can be more than a single character, then you'll need to represent it with a string. You can't switch on a string, so you'll have to use if..else..else.. to deal with the value:
std::string size;
cin >> size;
if (size == "XL") {
// deal with size XL
} else if (size == "L") {
// deal with size L
} // and so on
If it were a single character, then you could use char (not int) to represent that:
char size;
cin >> size;
switch (size) {
case 'L':
// deal with size L
break;
// and so on
}
but for multiple characters, you'll need a string.
switch statement can handle int and char in C++. char data type can hold only one letter. Thus, if you input only one letter (X) for XL size will be fine ...
cout << "Input size (X/L/M/S): ";
cin >> s;
switch (s){
case 'X': case 'x':
You've declared s as an integer but attempt to use it as a character and character array. You should probably declare it is char s; and then use it consistently as just a single character -- which does mean that you can't check for XL. You could, however, just check for X in your switch.
If you absolutely must check for XL, then you'll need to use either a character array or std::string, although switch statements can only be used with single characters, so you may have to nest your switch to check for multiple characters or just use a series of if (strncmp(...)...) calls.

Regarding calling inherited functions

I have a base class called account. Three classes are inheriting from account. These classes are savings, checkings, and creditcard. In my main() I am trying to create a switch menu so that when the user selects 1 it will call makeDeposit(), which is part of account, but do so through savings. This way when the user chooses 3 it will call makeDeposit(), but do so through checkings. Here is the code I have written. I have declared object saving sa; and when I call makeDeposit I am trying to write it as sa.makeDeposit(). Here is the code:
int main ()
{
saving sa;
creditCard cca;
checking ca;
string n;
int option;
int exit = 1;
cout << endl;
cout << "Checking Balance:" << " " << " " << "Savings balance:" << " " << " " << "Credit Card balance:" << " " << endl;
cout << endl;
cout << " (1) Savings Deposit " << endl;
cout << " (2) Savings withdrawel " << endl;
cout << " (3) Checking Deposit " << endl;
cout << " (4) Write A Check " << endl;
cout << " (5) Credit Card Payment " << endl;
cout << " (6) Make A Charge " << endl;
cout << " (7) Display Savings " << endl;
cout << " (8) Display Checkings " << endl;
cout << " (9) Display Credit Card " << endl;
cout << " (0) Exit " << endl;
cin >> option;
do{
switch ( option )
{
case 1 : double amtD;
cout << " Please enter how much you would like to deposit into savings " << endl;
cin >> amtD;
double sa.makeDeposit(double amtD);
break;
case 2 : double makeWithdrawel();
break;
case 3 : double makeDeposit();
break;
case 4 :
break;
case 5 :
break;
case 6 : double makeWithdrawel();
break;
case 7 : int display();
break;
case 8 : int display();
break;
case 9 : int display();
break;
case 0 : exit = 0;
break;
default : exit = 0;
cout << " ERROR ";
}
}
while(exit==1);
return 0;
}
Here is my class saving :
#include "stdafx.h"
#include "iostream"
#include "Account.h"
#include <string>
#include <sstream>
using namespace std;
class saving: public account
{
public :
double doWithdraw(double amount);
saving();
saving(string itsName, long itsTaxID, double itsBalance);
}
and my class for account :
#include "stdafx.h"
#include <string>
#include <sstream>
using namespace std;
class account {
public :
void setName(string name); void setTaxID(long taxID); void setBalance(double balance);
string getName(); long getTaxID(); double getBalance();
double makeDeposit( double amount );
account();
account(string itsName, long itsTaxID, double itsBalance);
int display();
private :
string itsName;
long itsTaxID;
double itsBalance;
protected :
double last10withdraws[10];
double last10deposits[10];
int numdeposits;
int numwithdraws;
};
Any idea on what I am doing wrong??
You're calling functions incorrectly. You do not need to include the types for the return value and parameters.
case 10: int function(); break;
is actually declaring a local function - not calling function as you expect.
Your switch statement should look something like below. Notice the absence of types in the calls to functions
switch ( option )
{
case 1 : double amtD;
cout << " Please enter how much you would like to deposit into savings " << endl;
cin >> amtD;
sa.makeDeposit(amtD);
break;
case 2 : makeWithdrawel();
break;
case 3 : makeDeposit();
break;
case 4 :
break;
case 5 :
break;
case 6 : makeWithdrawel();
break;
case 7 : display();
break;
case 8 : display();
break;
case 9 : display();
break;
case 0 : exit = 0;
break;
default : exit = 0;
cout << " ERROR ";
}
The following code looks a bit weird.
cin >> amtD;
double sa.makeDeposit(double amtD);
break;
You are trying to declare a variable with the double keyword, but I think you want to do one of two things:
Just make the deposit without using the returned double from makeDeposit() for anything. In that case, just write the line like this (using the returned double is optional):
sa.makeDeposit(amtD);
You want to make the deposit but also save some return information. Then you would probably do something like this (and use the new variable for something later).
double justMadeDeposit = sa.makeDeposit(amtD);
It's some guesswork. Maybe you don't want the makeDeposit() function to return anything at all, and then you could declare it as void instead.