Switch statemement reacting wrong after method getline c++ - c++

I am coding a banking app for university and i have stumbled upon a problem. Let's take for example a method of my class Customer.
void Customer::create_customer_data() {
cout << "Client' Address: ";
getline(cin, clientAddress);
cin.ignore();
cout << "Client's birth date: ";
getline(cin, clientBirthDate);
cin.ignore();
cout << "Client's telephone number: ";
getline(cin, clientTelephoneNumber);
cin.ignore();
cin.clear(); }
In the main function, i have a switch statement which handles the user choice.
int main() {
int choice, _globalClientNumber = 0;
Customer a(_globalClientNumber);
cout << "Welcome to bank manager 1.0!" << endl;
do {
cout << "Main Menu"<< endl;
cout << "Create a new customer (1)" << endl;
cout << "Create a new account (2)" << endl;
cout << "Cash money into account (3)" << endl;
cout << "Cash money out of account (4)" << endl;
cout << "Transfer money between two accounts (5)" << endl;
cout << "See current status of a customer and its accounts (6)" << endl;
cout << "End Application (0)" << endl;
cout << "Choice: ";
cin >> choice;
switch (choice) {
case 1:
a.create_customer_data();
break;
case 2:
a.create_new_account();
break;
case 3:
a.cash_in();
break;
case 4:
a.cash_out();
break;
case 5:
a.transfer_money();
break;
case 6:
a.print();
break;
default:
break;
}
cout << endl;
}
while (choice != 0);
return 0; }
The problem that i keep having is that the values that are written on the screen by the create_customer_data method are being processed by the do-while loop. So if the clientTelephoneNumber in the create_customer_data does not end in 0, the main menu is shown twice by the do-while loop. I would appreciate it if someone could tell me where my mistake is.
Edit: Shortly said: the choice variable gets overridden and the do-while loop gets executed once more resulting in double printing the menu.

i would say, that your cin needs to be flushed.
the answer can be found here:
How do I flush the cin buffer?

Related

How can I return user to original switch menu from do-while loop?

how can I get user to go back to original switch menu once the user selects N at the end. When user selects N, would I use another loop to get them back to original menu? Any help is greatly appreciated.
cout << "Total Chips: " << chips << endl;
cout << "1) xxxxx" << endl;
cout << "2) xxx" << endl;
cout << "Please enter an option" << endl;
int option;
cin >> option;
switch(option)
{
case 1:
{
char again;
do
{
/* code
*/
cout << "Would you like to play again? Y/N" << endl;
cin >> again;
}while(towlower(again) == 'y'); // I'm not sure whether to use another do-while loop.
When user selects N, would I use another loop to get them back to original menu?
Yes, one that is put around the original menu, eg:
bool keepRunning = true;
do {
cout << "Total Chips: " << chips << endl;
cout << "1) xxxxx" << endl;
cout << "2) xxx" << endl;
cout << "Please enter an option" << endl;
int option;
cin >> option;
switch (option)
{
case 1:
{
char again;
do
{
/* code
*/
cout << "Would you like to play again? Y/N" << endl;
cin >> again;
}
while (again == 'y' || again == 'Y');
break;
}
...
}
}
while (keepRunning);

C++ The loop in my switch format isn't allowing me to display menu options or accept input?

I'm needing to write a loan program for a course and I have this bit of code thus far:
/*
Project: Course Project - Loan Calculator
Program Description: To calculate customer's loan
information including interest rate, monthly
payment and number of months payments will be
required for.
Developer: ___________
Last Update Date: June 1st, 2020
*/
#include <iostream>
#include <string>
//variables listed here
double loanAmt = 0, intRate = 0, loanLength = 0;
std::string CustName[0];
int choice; //menu choice identifier
using namespace std;
int main()
{
//Welcome User
cout << "Thank you for using our calculator." << endl;
cout << endl;
cout << "Please enter the number of your preferred calculator and press 'Enter': " << endl;
cout << endl;
cin >> choice;
{
while (choice !=4);
switch (choice)
{
case 1:
cout << "Monthly Payment Calculator:";
cout << endl;
break;
case 2:
cout << "Interest Rate Calculator:";
cout << endl;
break;
case 3:
cout << "Payment Term (In Months):";
cout << endl;
case 4:
cout << "Exit Calculator:";
default: //all other choices
cout << "Please Select A Valid Option.";
break;
}
}
}
I've tried moving the cin << choice; to many different spots, including inside of the while loop written for the switch, but I can only seem to get it to show the prompt for the menu, but not the menu options themselves and it isn't taking input. If it makes a difference, I'm using Xcode for the first time and I need it to run there as I can't run C++ on my version of Visual studio (on Mac). Where may I have gone wrong here and any insight into why my version is wrong is appreciated as I'm still very new to coding overall. Thank you.
You had this:
while (choice !=4);
This makes it so that your code never continues further than this while loop. Unless your compiler removes this line during optimization.
This is the correct way:
while (choice !=4) {
switch (choice)
{
case 1:
cout << "Monthly Payment Calculator:";
cout << endl;
break;
case 2:
cout << "Interest Rate Calculator:";
cout << endl;
break;
case 3:
cout << "Payment Term (In Months):";
cout << endl;
case 4:
cout << "Exit Calculator:";
default: //all other choices
cout << "Please Select A Valid Option.";
break;
}
cin >> choice; // have it right after the switch
}

switch statement C++ [default part]

I'm writing a basic program about switch statement. The menu of 5 selections, if the use input invalid number, the user should be prompted again to make a selection (range is from 1-5). Here what I got so far:
#include <iostream>
#include "Menu.h"
using namespace std;
void Menu::inputChoices()
{
int choice;
cout << "IHCC Computer Science Registration Menu" << endl;
cout << "1. Welome to Computer Programming in C++" << endl;
cout << "2. Welcome to Java Programming" << endl;
cout << "3. Welcome to Android Programming" << endl;
cout << "4. Welcome to iOS Programming" << endl;
cout << "5. Exit" << endl;
cout << "\nEnter your selection: " << endl;
while ((choice = cin.get()) != EOF)
{
switch (choice)
{
case '1':
cout << "Welcome to Computer Programming in C++" << endl;
break;
case '2':
cout << "Welcome to Java Programming" << endl;
break;
case '3':
cout << "Welcome to Android Programming" << endl;
break;
case '4':
cout << "Welcome to iOS Programming" << endl;
break;
case '5':
cout << "Exiting program" << endl;
break;
default:
cout << "Invalid input. Re-Enter your selection: " << endl;
}
}
}
This project has 3 files, this is the source file. My problem is when I input a number is in the range (1-5), the default part of switch still also shows up. I just want show up my selection. Can anyone help me please! Thank you very much
The reason why you are also getting the default value is because cin.get() is reading the newline character (10).
I don't think using cin.get makes sense here since entering something like 23987928 or asdasf will output a ton of lines... You should use cin >> choice and convert your cases into ints. Something like:
cin >> choice;
switch (choice)
{
case 1:
cout << "Welcome to Computer Programming in C++" << endl;
break;
// ...
default:
cout << "Invalid input. Re-Enter your selection: " << endl;
}
The enter key '\n' should be handled. Try following and see if it works:
while ((choice = cin.get()) != EOF)
{
case 1:
// ...
// Basic idea is read extra `\n` and ignore it. Use below if condition
default:
if(choice != '\n')
cout << "Invalid input. Re-Enter your selection: " << endl;
}

Switch statement error in C++

Hello guys would someone please help me to fix this code of mine? It runs but it doesn't perform the task it should do. Once you run it, it automatically jumps in to the default statement and skips the cases. Please help me fix this thanks!
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
int choice;
char nbaPlayer, tele, food, subject, x;
cout << "This program determines your favorites.\n\n";
cout << "Please select the number of your corresponding choice.";
cout << "\n1. NBA Player";
cout << "\n2. Teleserye";
cout << "\n3. Food";
cout << "\n4. Subject";
cout << "\n5. Exit";
switch (choice)
{
case 1:
cout << "You have chosen NBA Player.\n";
cout << "Please enter your favorite NBA Player. \n";
cin >> nbaPlayer;
cout << "Your favorite NBA player is " << nbaPlayer;
break;
case 2:
cout << "You have chosen Teleserye.\n";
cout << "Please enter your favorite teleserye. \n";
cin >> tele;
cout << "Your favorite teleserye is " << tele;
break;
case 3:
cout << "You have chosen food.\n";
cout << "Please enter your favorite food. \n";
cin >> food;
cout << "Your favorite food is " << food;
break;
case 4:
cout << "You have chosen subject.\n";
cout << "Please enter your favorite subject. \n";
cin >> subject;
cout << "Your favorite subject is " << subject;
break;
case 5:
cout << "You chose to exit.\n";
break;
default:
cout <<"\nInvalid input";
}
getch();
}
You simply forgot:
cin>>choice
after you display available choice options. So when you never assign value to choice, it'll always choose default.

How do I prevent continuation of the program if player inputs invalid selection?

I created a text-based game to help improve my C++ skills since I am very new to it. I'm trying to learn the basics n' all.
Here is the program. In the switch statement, if a person were to select "3" it would broadcast an Error saying you can only select "1" or "2".
** The issue is the program continues and doesn't make the person RECHOOSE the selection. It goes right to Difficulty selecting.
What method do I use to force the program to halt until player chooses valid selection?
Thanks!
#include <iostream>
using namespace std;
int main()
{
cout << "\tWelcome to my text based game!\n";
char userName[100];
cout << "\nPlease enter your username: ";
cin >> userName;
cout << "Hello, " << userName << "!\n\n";
cout << "Please pick your race: \n";
cout << "1 - Human\n";
cout << "2 - Orc\n";
int pickRace;
cout << "Pick your race: ";
cin >> pickRace;
switch (pickRace)
{
case 1:
cout << "You picked the Human race." << endl;
break;
case 2:
cout << "You picked the Orc race." << endl;
break;
default:
cout << "Error - Invalid input; only 1 or 2 allowed!" << endl;
}
int difficulty;
cout << "\nPick your level difficulty: \n";
cout << "1 - Easy\n";
cout << "2 - Medium\n";
cout << "3 - Hard\n";
cout << "Pick your level difficulty: ";
cin >> difficulty;
switch (difficulty)
{
case 1:
cout << "You picked Easy" << endl;
break;
case 2:
cout << "You picked Medium" << endl;
break;
case 3:
cout << "You picked Hard" << endl;
break;
default:
cout << "Error - Invalid input, only 1,2 or 3 are allowed" << endl;
}
return 0;
}
You need to use loops. Wrap the input and the switch after it in a loop and break out of it when the input is valid.
Use a do ... while loop, like this
int pickRace;
do
{
cout << "Please pick your race: \n";
cout << "1 - Human\n";
cout << "2 - Orc\n";
cout << "Pick your race: ";
cin >> pickRace;
switch (pickRace)
{
case 1:
cout << "You picked the Human race." << endl;
break;
case 2:
cout << "You picked the Orc race." << endl;
break;
default:
cout << "Error - Invalid input; only 1 or 2 allowed!" << endl;
break;
}
}
while (pickRace != 1 && pickRace !=2);
This will keep on looping while the condition at the bottom is true (i.e. while they haven't picked a valid option).
One other comment. Since you are new you should really get into the habit of using string not char arrays. Char arrays will get you into all sorts of trouble in the future, so start using strings now. There's no point in learning bad habits.
#include <string>
string userName;
cout << "\nPlease enter your username: ";
cin >> userName;
cout << "Hello, " << userName << "!\n\n";
You can do - while loop with flags. The flag is by default false therefore allowing only one time input. If there is a need to enter another time, determined and controlled by the default case, then the flag is set to true, which makes the loop iterate once more. At the beginning of each iteration flag is reset to false, so each time it is assumed that this is the last one. Using flag will make the breaking operation much simple, and avoid complex while conditions.
int flag = 0;
do
{
cout << "Please pick your race: \n";
cout << "1 - Human\n";
cout << "2 - Orc\n";
cout << "Enter Choice: ";
cin >> pickRace;
flag = 0;
switch (pickRace)
{
case 1:
cout << "You picked the Human race." << endl;
break;
case 2:
cout << "You picked the Orc race." << endl;
break;
default:
cout << "Error - Invalid input; only 1 or 2 allowed!" << endl;
flag = 1;
}
} while (flag);