Undeclared Identifier C2065 "P" - c++

I am a new programming student and this is my first time posting here so I apologize if I did it wrong. I have attached my code below. I am receiving two errors when I try to build my solution. They are : error C2065: 'p': undeclared identifier. I'm confused why p is undeclared but not m or q. I am using visual studio 2017. I understand this may be a simple fix, but I'm new to this and still learning the basics.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
//declare variables//
int kilogram, kilometer, liter;
unsigned char choice = p, m , q;
double pounds, miles, quarts;
//create menu for user//
cout << "Please enter your choice from the menu below\n"
<< "(p)ounds to kilograms\n"
<< "(m)iles to kilometers\n"
<< "(q)uarts to liters\n"
<< "Please enter your conversion choice\n";
cin >> choice;
//validate choice//
if (choice != 'p' && choice != 'm' && choice != 'q')
{
cout << "Invalid Choice\n"
<< "Please enter choice" << endl;
cin >> choice;
}
//Make conversion//
if (choice == p)
{
//Get metric value//
cout << "Please enter the kilogram value\n";
cin >> kilogram;
}
//calculate conversion//
else
{
pounds = kilogram * 2.2046;
cout << kilogram << "kilograms is" << pounds << "Pounds" << endl;
}
//Validate input//
if (kilogram < 1)
{
cout << "Invalid Input" << endl;
cout << "Enter the kilogram value" << endl;
cin >> kilogram;
}
if (choice == m)
{
//Get metric value//
cout << "Please enter the kilometer value\n";
cin >> kilometer;
}
//calculate conversion//
else
{
miles = kilometer * 0.621388;
cout << kilometer << "kilometer is " << miles << "miles" << endl;
}
//Validate Input//
if (kilometer < 1)
{
cout << "Invalid Input" << endl
<< "Enter the kilometer value" << endl;
cin >> kilometer;
}
//Make conversion
if (choice == q)
{
//Get metric value//
cout << "Please enter the liter value\n";
cin >> liter;
}
//Calculate conversion
else
{
quarts = liter * 0.877193;
cout << liter << "liter is" << quarts << "quarts" << endl;
}
//Validate input//
if (liter < 1)
{
cout << "Invalid Input" << endl
<< "Enter the liter value" << endl;
cin >> liter;
}
return 0;
}

because you seperated with comman, so the compiler will consider the code as
unsigned char choice = p;
unsigned char m;
unsigned char q;
So the p is undeclared

Related

Why am I getting Warning: C4244?

So I've been getting: Warning C4244 '=': conversion from 'double' to 'long', possible loss of data line 158. From what I understand is that 'pow' and 'long int result' are somehow connected to this, I have been messing around with it and changed 'long int result' to 'double result' and got rid of the warning. I have a solution to get rid of the warning, but it won't matter since this program need long int to handle more data otherwise it will overflow if I use double.
If I decided to keep this warning in the program will there be potenial issues with it?
Can I somehow get rid of the warning and still be able to use 'long int result' or at least be able to handle more data some other way?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
char menu, choice;
int numberYears = 0;
long int deposit, withdraw;
long int result = 0;
long int annualDeposit = 0;
float interestRate = 0.0;
long int balance = 0;
cout << "------------------------------\n" << "Welcome to Bank Simulator 3000" << "\n------------------------------\n\n";
while (true)
{
cout << "XXXXXXX[MENU]XXXXXXXX\n";
cout << "[D]eposit\n";
cout << "[W]ithdrawal\n";
cout << "[B]alance\n";
cout << "[I]nterest payment\n";
cout << "[E]xit\n";
cout << "XXXXXXXXXXXXXXXXXXXXX\n";
cin >> menu;
switch (menu)
{
case'd':
case'D':
cout << "\n[DEPOSIT]\n";
cout << "Do you want to make a deposit?\n" << "Y/N\n";
cin >> choice;
if (choice == 'Y' || choice == 'y')
{
cout << "\nHow much do you want to deposit?\n" << "\n:";
}
else
{
cout << "\nReturning to menu.\n\n";
continue;
}
cin >> deposit;
balance += deposit;
cout << "\n" << deposit << " Kr has been added to balance.\n\n";
continue;
case'w':
case'W':
cout << "\n[WITHDRAWAL]\n" << "Do you want to make a withdrawal?\n" << "Y/N\n";
cin >> choice;
if (choice == 'Y' || choice == 'y')
{
cout << "\nHow much do you want to withdraw?\n" << "\n:";
}
else
{
cout << "\nReturning to menu.\n\n";
continue;
}
cin >> withdraw;
if (withdraw == 0)
{
cout << "\nYou withdrew 0 amount. No changes will apply.\n\n";
continue;
}
balance -= withdraw;
cout << "\n" << withdraw << " Kr has been drawn from balance.\n\n";
continue;
case'b':
case'B':
cout << "\n[BALANCE]\n";
cout << balance << " Kr\n\n";
continue;
case'i':
case'I':
cout << "\n[INTEREST PAYMENT]\n";
cout << "Do you want to calculate your interest payment?\n" << "Y/N\n";
cin >> choice;
if (choice == 'Y' || choice == 'y')
{
cout << "What's your annual deposit?\n" << ":";
}
else
{
cout << "\nReturning to menu.\n\n";
continue;
}
cin >> annualDeposit;
if (annualDeposit == 0)
{
cout << "You typed 0 in your annual deposits, this will give unwanted results.\n" << "Do you want to continue?\n" << "Y/N\n";
cin >> choice;
if (choice == 'Y' || choice == 'y');
else
{
cout << "Returning to menu.\n\n";
continue;
}
}
cout << "What's Your Interest Rate?\n" << ":";
cin >> interestRate;
if (interestRate == 0)
{
cout << "You typed 0 in your interest rate, this will give unwanted results.\n" << "Do you want to continue?\n" << "Y/N\n";
cin >> choice;
if (choice == 'Y' || choice == 'y');
else
{
cout << "Returning to menu.\n\n";
continue;
}
}
cout << "How many years do you want to save to?\n" << ":";
cin >> numberYears;
if (numberYears <= 0)
{
cout << "You typed 0 or less in number of years, this will give unwanted results.\n" << "Do you want to continue?\n" << "Y/N\n";
cin >> choice;
if (choice == 'Y' || choice == 'y');
else
{
cout << "Returning to menu.\n\n";
continue;
}
}
result = annualDeposit * pow(1 + interestRate / 100, numberYears);
cout << "Your Interest Payment Will Be: " << result << " Kr In " << numberYears << " Years\n\n";
continue;
default:
cout << "\nPlease use the following example\n" << "D = Deposit | W = Withdrawal | B = Balance | I = Interest payment | E = Exit\n\n";
continue;
case'e':
case'E':
cout << "Thanks for using Bank Simulator 3000!\n";
cout << "Press any key to close";
system("pause>0");
break;
}
break;
}
return(0);
}
In typical environment, a double variable can store a floating-point number upto about 10^300 (assuming IEEE754 64-bit is used).
On the other hand, a long int can store an integer only upto about 10^9 (32-bit) or 10^18 (64-bit).
Therefore, the maximum value to handle by long int is much smaller than one for double. This is why conversion from double to long int can cause loss of data.
You can add an explicit cast to suppress the warning.
result = static_cast<long int>(annualDeposit * pow(1 + interestRate / 100, numberYears));

What should I do when one function finishes another one opens up?

I have a Problem. I'm Trying to make a regular Calculator and Shape's Area and Perimeter finder.It's a combination. I didn't start on my Shape's Area and Perimeter Finder. This is My main.cpp.
#include <iostream>
#include <string>
#include "AAPO.h" // Its a Header File.
using namespace std;
void Calculators_Operation();
int main()
{
string opera;
cout << "Do you want Arithmetic Calculator or Area and Perimeter Calculator"
<< endl;
cin >> opera;
if (opera == "Arithmetic Calculator" or "arithmetic calculator" or "AC")
{
Calculators_Operation();
}
return 0;
}
This is my Operation Chooser.
#include <iostream>
#include <string>
#include "Arithmetic Chooser.h"
using namespace std;
void Calculators_Addition();
void Calculators_Subtraction();
void Calculators_Multiplication();
void Calculators_Division();
void Calculators_Operation()
{
string answera;
cout << "What Operation do you Want?" << endl;
cin >> answera;
if (answera == "Addition" or "addition" or "+");
{
Calculators_Addition();
};
if (answera == "Subtraction" or "subtraction" or "-");
{
Calculators_Subtraction();
};
if (answera == "Multiplication" or "multiplication" or "*" or "x" or "X")
{
Calculators_Multiplication();
};
if (answera == "Division" or "division" or "/")
{
Calculators_Division();
};
return;
}
This is my AAPO.h.
#ifndef AAPO_H_INCLUDED
#define AAPO_H_INCLUDED
void Calculators_Operation();
#endif // AAPO_H_INCLUDED
My Addition.
#include <iostream>
#include <string>
using namespace std;
void calculators_Addition_2();
void calculators_Addition_3();
void calculators_Addition_4();
void calculators_Addition_5();
void Calculators_Addition()
{
//ADDITION COMPLETE
string numberadd;
cout << "How much numbers do you want?" << endl;
cin >> numberadd;
if (numberadd == "2")
{
calculators_Addition_2();
return;
};
if (numberadd == "3")
{
calculators_Addition_3();
return;
};
if (numberadd == "4")
{
calculators_Addition_4();
return;
};
if (numberadd == "5")
{
calculators_Addition_5();
return;
}
}
void calculators_Addition_2()
{
int add11;
int add12;
int sum;
cout << "Enter the first number" << endl;
cin >> add11;
cout << "Enter the second number" << endl;
cin >> add12;
sum = add11 + add12;
cout << "The sum of the numbers are " << sum << endl;
return;
}
void calculators_Addition_3()
{
int add13;
int add23;
int add33;
int sum2;
cout << "Enter the First Number" << endl;
cin >> add13;
cout << "Enter the Second Number" << endl;
cin >> add23;
cout << "Enter the Third Number" << endl;
cin >> add33;
sum2 = add13 + add23 + add33;
cout << "The Sum of the Numbers are " << sum2 << endl;
return;
}
void calculators_Addition_4()
{
int add14;
int add24;
int add34;
int add44;
int sum3;
cout << "Enter the First Number" << endl;
cin >> add14;
cout << "Enter the Second Number" << endl;
cin >> add24;
cout << "Enter the Third Number" << endl;
cin >> add34;
cout << "Enter the Fourth Number" << endl;
cin >> add44;
sum3 = add14 + add24 + add34 + add44;
cout << "The Sum of the Numbers are " << sum3 << endl;
return;
}
void calculators_Addition_5()
{
int a15;
int a25;
int a35;
int a45;
int a55;
int sum4;
cout << "Enter the First Number" << endl;
cin >> a15;
cout << "Enter the Second Number" << endl;
cin >> a25;
cout << "Enter the Third Number" << endl;
cin >> a35;
cout << "Enter the Fourth Number" << endl;
cin >> a45;
cout << "Enter the Fifth Number" << endl;
cin >> a55;
sum4 = a15 + a25 + a35 + a45 + a55;
cout << "The Sum of the Numbers are " << sum4 << endl;
return;
}
My Subtraction.
#include <iostream>
using namespace std;
void Calculators_Subtraction()
{
int subractify;
int subracta;
int differencea;
cout << "Type in the First Number!" << endl;
cin >> subractify;
cout << "Type in the Second Number!" << endl;
cin >> subracta;
differencea = subractify - subracta;
cout << "The Difference is " << differencea << endl;
return;
}
My Multiplication.
#include <iostream>
#include <string>
using namespace std;
void Calculators_Multiplication_2();
void Calculators_Multiplication_3();
void Calculators_Multiplication_4();
void Calculators_Multiplication_5();
void Calculators_Multiplication()
{
string multicipia;
cout << "How much numbers do you want?" << endl;
cin >> multicipia;
if (multicipia == "2" or "Two" or "two")
{
Calculators_Multiplication_2();
};
if (multicipia == "3" or "Three" or "three")
{
Calculators_Multiplication_3();
};
if (multicipia == "4" or "Four" or "four")
{
Calculators_Multiplication_4();
};
if (multicipia == "5" or "Five" or "five")
{
Calculators_Multiplication_5();
};
return;
}
void Calculators_Multiplication_2()
{
int multi2a;
int multi2b;
int product2;
cout << "Type in the First Number." << endl;
cin >> multi2a;
cout << "Type in the Second Number." << endl;
cin >> multi2b;
product2 = multi2a * multi2b;
cout << "The Product is " << product2 << "." << endl;
return;
}
void Calculators_Multiplication_3()
{
int multi3a;
int multi3b;
int multi3c;
int product3;
cout << "Enter the First Number!" << endl;
cin >> multi3a;
cout << "Enter the Second Number!" << endl;
cin >> multi3b;
cout << "Enter the Third Number!" << endl;
cin >> multi3c;
product3 = multi3a * multi3b * multi3c;
cout << "The Product is" << product3 << "." << endl;
return;
}
void Calculators_Multiplication_4()
{
int multi4a;
int multi4b;
int multi4c;
int multi4d;
int product4;
cout << "Enter the First Number!" << endl;
cin >> multi4a;
cout << "Enter the Second Number!" << endl;
cin >> multi4b;
cout << "Enter the Third Number!" << endl;
cin >> multi4c;
cout << "Enter the Fourth Number!" << endl;
cin >> multi4b;
product4 = multi4a * multi4b * multi4c * multi4d;
cout << "The Product of the Numbers are " << product4 << "!" << endl;
return;
}
void Calculators_Multiplication_5()
{
int multi5a;
int multi5b;
int multi5c;
int multi5d;
int multi5e;
int product5;
cout << "Enter the First Number!" << endl;
cin >> multi5a;
cout << "Enter the Second Number!" << endl;
cin >> multi5b;
cout << "Enter the Third Number!" << endl;
cin >> multi5c;
cout << "Enter the Fourth Number!" << endl;
cin >> multi5d;
cout << "Enter the Fifth Number!" << endl;
cin >> multi5e;
product5 = multi5a * multi5b * multi5c * multi5d * multi5e;
cout << "The Product of the Numbers are" << product5 << "!" << endl;
return;
}
My Division.
#include <iostream>
using namespace std;
void Calculators_Division()
{
float divisia;
float divisiab;
float quotient;
cout << "Enter the Divisor" << endl;
cin >> divisia;
cout << "Enter the Dividend" << endl;
cin >> divisiab;
quotient = divisia / divisiab;
cout << "The Quotient of the Numbers are " << quotient << endl;
return;
}
The Problem right now is that when addition finishes , subtraction starts. After Subtraction, Multiplication. After Multiplication, Division. After That the program ends. I'm sorry. Its just that I'm new to programming (Like one month).
There are many things to improve (some of them already mentioned in comments), but exact answer to your question is:
you have semicolon after your if-statement lines in Calculators_Operation() function body, which makes if-statements useless and your "operation" functions are being called every time (what you already noticed).
you use "or" operator in incorrect way, if you use (answera == "addition" or "+"), you basically say: ((answera == "addition) or "+") and that evaluates to true (see here)
EDIT:
Your function Calculators_Operation may be changed to:
void Calculators_Operation()
{
string answera;
cout << "What Operation do you Want?" << endl;
cin >> answera;
if (answera == "Addition" || answera == "addition" || answera == "+")
{
Calculators_Addition();
}
else if (answera == "Subtraction" || answera == "subtraction" || answera == "-")
{
Calculators_Subtraction();
}
else if (answera == "Multiplication" || answera == "multiplication" || answera == "*" || answera == "x" || answera == "X")
{
Calculators_Multiplication();
}
else if (answera == "Division" || answera == "division" || answera == "/")
{
Calculators_Division();
} else {
cout << "Unknown operation entered!" << endl;
}
}
NOTE: all your if statements need to be changed according to this function.
If anything does not work, please either put in comments what exactly does not work or edit your question with new code posting and question.

C++ Program crashes/infinite looping if a letter is inputted as an answer instead of a number [duplicate]

Sorry if I fail to be clear enough or make any mistakes, this is my first time posting.
My code runs without errors when complied but the first while loop (in int main) gets stuck looping whenever a user types a letter (like "a") for cin >> select; instead of the required 1, 2, or 3.
However, when I input "4" or any other random string of numbers, it runs fine and goes to my error message like it should.
Why is this and what can I do to make it run normally? (run the error message in response to letters entered as if they were numbers).
My code:
#include <iostream>
#include <string>
using namespace std;
void calculator();
void unavailableitem();
int main()
{
string select;
while (true)
{
cout << "\t[Main Menu]\n";
cout << " 1. Calculator\n";
cout << " 2. [unavailable]\n";
cout << " 3. [unavailable]\n";
cout << "\n Enter the number of your selection: ";
cin >> select;
if (select == "1")
{
cout << endl;
calculator();
break;
}
else if (select == "2")
{
unavailableitem();
break;
}
else if (select == "3")
{
unavailableitem();
break;
}
else
cout << "\nInvalid response.\n";
}
}
void unavailableitem()
{
string react;
cout << "\n \t [ITEM UNAVAILABLE]\n";
while (true)
{
cout << "\nEnter 'menu' to return to main menu: ";
cin >> react;
if (react == "menu")
{
cout << endl;
main();
break;
}
else
cout << "\nInvalid response.\n";
}
}
void calculator()
{
int choice;
double num1;
double num2;
double answer;
string choicesymbol;
cout << "List of operations:\n";
cout << " 1. Addition\n";
cout << " 2. Subtraction\n";
cout << " 3. Multiplication\n";
cout << " 4. Division\n";
cout << "Enter the number on the left to pick an operation: ";
cin >> choice;
cout << "\nEnter number 1: ";
cin >> num1;
cout << "\nEnter number 2: ";
cin >> num2;
if (choice == 1)
{
answer = num1 + num2;
choicesymbol = " + ";
}
if (choice == 2)
{
answer = num1 - num2;
choicesymbol = " - ";
}
if (choice == 3)
{
answer = num1 * num2;
choicesymbol = " * ";
}
if (choice == 4)
{
answer = num1 / num2;
choicesymbol = " / ";
}
cout << endl;
cout << num1 << choicesymbol << num2 << " = " << answer;
}
New code:
#include <iostream>
#include <string>
using namespace std;
void calculator();
void unavailableitem();
int main()
{
int select;
while (true)
{
cout << "\t[Main Menu]\n";
cout << " 1. Calculator\n";
cout << " 2. [unavailable]\n";
cout << " 3. [unavailable]\n";
cout << "\n Enter the number of your selection: ";
cin >> select;
if(!(cin >> select))
{
cout << "Input must be an integer.\n";
cin.clear();
continue;
}
else if (select == 1)
{
cout << endl;
calculator();
break;
}
else if (select == 2)
{
unavailableitem();
break;
}
else if (select == 3)
{
unavailableitem();
break;
}
}
}
void unavailableitem()
{
string react;
cout << "\n \t [ITEM UNAVAILABLE]\n";
while (true)
{
cout << "\nEnter 'menu' to return to main menu: ";
cin >> react;
if (react == "menu")
{
cout << endl;
return;
break;
}
else
cout << "\nInvalid response.\n";
}
}
void calculator()
{
int choice;
double num1;
double num2;
double answer;
string choicesymbol;
cout << "List of operations:\n";
cout << " 1. Addition\n";
cout << " 2. Subtraction\n";
cout << " 3. Multiplication\n";
cout << " 4. Division\n";
cout << "Enter the number on the left to pick an operation: ";
cin >> choice;
cout << "\nEnter number 1: ";
cin >> num1;
cout << "\nEnter number 2: ";
cin >> num2;
if (choice == 1)
{
answer = num1 + num2;
choicesymbol = " + ";
}
if (choice == 2)
{
answer = num1 - num2;
choicesymbol = " - ";
}
if (choice == 3)
{
answer = num1 * num2;
choicesymbol = " * ";
}
if (choice == 4)
{
answer = num1 / num2;
choicesymbol = " / ";
}
cout << endl;
cout << num1 << choicesymbol << num2 << " = " << answer;
}
Ad Ed Heal mentioned, the issue here is cin's failbit. When you do cin >> choice, and the user types "a", then the conversion to int fails. This sets cin's failbit, making all future reads from it fail until the failbit is cleared. So the next time you reach cin >> choice, the user won't even get to type anything.
You can use cin.clear() to restore to working order.
To do this a bit more robustly, you could do something like
while(true)
{
cout >> "Enter choice [1-4]: ";
if(!(cin >> choice))
{
cout << "Input must be an integer.\n";
cin.clear();
continue;
}
do_stuff_with_choice();
}
I am a newbie to programming in general, but playing with your code and looking up stuff made me find some sort of solution.
The cin.clear only clears the error log of the input, and I believe that it still retains the value of the letter.
What you should add right after is a cin.ignore(#,'\n') (# being a very, very large number) to have it avoid the line and skip right through it.
Found the solution in another question that explains the use of both cin commands.

Program stuck in an infinite loop

I'm trying to get this program to run properly. it should do as the pseudo code do as described although when I execute the program and try to open a new account if I put more than one character in the customer name field the program just goes into an infinite loop and I have clue how to fix this issue.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
#include <iomanip>
using namespace std;
int choice, account_number;
long acc_entry;
long acc_no = 112280;
double balance, deposit, withdrawal;
int interest = 1.67;
string customer_name;
void display_menu();
void get_choice();
void menu_selection(int selection);
void open_account();
void make_withdrawal();
void make_deposit();
void add_interest();
void display_transaction();
void main()
{
get_choice();
}
void display_menu()
{
system("CLS");
cout << "\n\n\t\t\t\tACCOUNT MENU";
cout << "\n\t\t\t\t============\n";
cout << "\n\t\t\t\t1. Open Account";
cout << "\n\t\t\t\t2. Make Withdrawal";
cout << "\n\t\t\t\t3. Make Deposit";
cout << "\n\t\t\t\t4. Add Interest";
cout << "\n\n\t\t\t\t5. Exit";
}
void open_account()
{
system("CLS");
cout << "\n\n\t\t\t\tOPEN ACCOUNT";
cout << "\n\t\t\t\t============\n\n";
cout << "\tPlease enter your name\n\n\t";
cin >> customer_name;
cout << "\n\n\tPlease enter initial despoit\n\n\t";
cin >> deposit;
balance = balance + deposit;
account_number = acc_no + 1;
cout << "\n\n\tYour new account number\n\n\t" << setfill('0') << setw(8) << account_number;
get_choice();
}
void make_withdrawal()
{
system("CLS");
cout << "\n\n\t\t\t\tMAKE WITHDRAWAL";
cout << "\n\t\t\t\t===============\n\n";
cout << "\tPlease enter Account Number\n\n\t";
cin >> acc_entry;
if (acc_entry == account_number)
{
cout << "\n\n\tPlease enter amount to withdraw\n\n\t";
cin >> withdrawal;
if (withdrawal > balance)
{
cout << "\n\n\tYou are exceeding your limit";
cin.ignore();
cin.get();
}
else
{
balance = balance - withdrawal;
cout << "\n\n\tYour new balance\n\n\t" << fixed << setprecision(2) << (char)156 << balance;
cin.ignore();
cin.get();
}
}
else
{
cout << "\n\n\tAccount number does not exist.";
cin.ignore();
cin.get();
}
get_choice();
}
void make_deposit()
{
system("CLS");
cout << "\n\n\t\t\t\tMAKE DEPOSIT";
cout << "\n\t\t\t\t============\n\n";
cout << "\tPlease enter Account Number\n\n\t";
cin >> acc_entry;
if (acc_entry == account_number)
{
cout << "\n\n\tPlease enter amount to deposit\n\n\t";
cin >> deposit;
balance = balance + deposit;
cout << "\n\n\tYour new balance\n\n\t" << fixed << setprecision(2) << (char)156 << balance;
cin.ignore();
cin.get();
}
else
{
cout << "\n\n\tAccount number does not exist.";
cin.ignore();
cin.get();
}
get_choice();
}
void add_interest()
{
string yn;
system("CLS");
cout << "\n\n\t\t\t\tADD INTEREST";
cout << "\n\t\t\t\t============\n\n";
cout << "\tPlease enter Account Number\n\n\t";
cin >> acc_entry;
if (acc_entry == account_number)
{
cout << "\n\n\tDo you wish to add interest [Y/N]\n\n\t";
getline(cin, yn);
if (yn == "Y" || yn == "y")
{
balance = balance * interest;
cout << "\n\n\tYour new balance\n\n\t" << fixed << setprecision(2) << (char)156 << balance;
cin.ignore();
cin.get();
}
}
else
{
cout << "\n\n\tAccount number does not exist.";
cin.ignore();
cin.get();
}
get_choice();
}
void display_transaction()
{
system("CLS");
cout << "\n\n\t\t\t\tCLOSED";
cout << "\n\t\t\t\t======\n\n";
if (account_number != 112280)
{
cout << "\tCustomer Name : - " << customer_name;
cout << "\n\n\tAccount Number : - " << setfill('0') << setw(8) << account_number;
cout << "\n\n\tBalance : - " << fixed << setprecision(2) << (char)156 << balance << "\n\n";
}
cin.get();
}
void get_choice()
{
display_menu();
do
{
cout << "\n\n\t\t\t\tEnter Number [1-5] : ";
cin >> choice;
menu_selection(choice);
} while
(choice << 1 || choice >> 5);
cin.ignore();
}
void menu_selection(int a)
{
switch (a)
{
case 1:
{
open_account();
break;
}
case 2:
{
make_deposit();
break;
}
case 3:
{
make_withdrawal();
break;
}
case 4:
{
add_interest();
break;
}
case 5:
{
display_transaction();
break;
}
default:
{
cout << "hello";
}
}
}
void get_choice()
{
display_menu();
do
{
cout << "\n\n\t\t\t\tEnter Number [1-5] : ";
cin >> choice;
menu_selection(choice);
} while
(choice < 1 || choice > 5); // << and >> aren't for comparison
cin.ignore();
}
In your get_choice function, you have a do-while loop with the following condition:
while (choice << 1 || choice >> 5); // this is going to run for a bit
<< and >> are not comparison operators; rather, they are bit shift operators.
Because your choice can be shifted either left or right, it goes into an infinite loop. It's a simple fix, really - change them to comparison operators!
As for the customer name, if there are spaces or newlines, std::cin will stop reading at the first one. Be sure to use std::getline for reading strings from stdin.
In open_account, replace this:
cin >> customer_name;
with this:
std::getline(cin, customer_name);
Also, fix this line:
int interest = 1.67; // This should be a double
You shouldn't be using globals really, but that's a whole different story.

C++ my program reads backspace as a character

I am working with a c++ program, but I am stuck with annoying bug. The bug is that when i type the password, it counts backspace as a character so can I fix it? Here is the code.
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
string password, username, lon, nu, np;
char c;
int StarNum = 0, humanproof;
cout << "Do you wanna create a user or login?";
cout << "\nLogin" << endl;
cout << "New" << endl;
cin >> lon;
if(lon=="login"){
goto login;
}
if(lon=="new"){
goto newa;
}
login:
cout << "Username: ";
cin >> username;
lol:
cout << "Password: ";
while (c != 13)
{
c = (char)getch();
if(c == 13) break;
StarNum++;
password += c;
cout << "*";
if (c == 127 || c == 8){
//go here to fix the problem
}
password = "";
goto lol;
}
}
if(username == "user" && password == "pass" || username == nu && password == np){
cout << "\nYou are logged in.";
goto options;
} else {
cout << "\nusername or password is wrong" << endl;
return 0;
}
return 0;
newa:
cout << "Username:";
cin >> nu;
cout << "password:";
cin >> np;
cout << "Type the number fourhoundred and twentie three too proof you are a human: ";
cin >> humanproof;
if(humanproof == 423){
cout << "The username is " << nu << endl;
for(int no = 0; no <= 100; no++){
cout << endl;
}
goto login;
return 0;
}
if(humanproof!=423){
cout << "wrong answer!";
return 0;
}
options:
int op;
cout << "\nwhat do you want to do?" << endl;
cout << "1. Calculator" << endl;
cout << "2. About"<< endl;
cout << "3. Just for fun" << endl;
cout << "4. Exit" << endl;
cin >> op;
if(op==1){
goto calculator;
}
if(op==2){
goto info;
}
if(op==3){
goto fun;
}
if(op==4){
return 0;
}
else{
cout << "you entered a invalid number. " << endl;
return 0;
}
calculator:
double n1, n2, sum;
int opa;
cout << "Choose a operation" << endl;
cout << "1. Addition/+" << endl;
cout << "2. Subscraction/-" << endl;
cout << "3. Multiplication/x" << endl;
cout << "4. Divsion/ /" << endl;
cin >> opa;
if(opa == 1){
cout << "enter number 1" << endl;
cin >> n1;
cout << "enter number 2" << endl;
cin >> n2;
sum = n1 + n2;
cout << "the sum is " << sum;
return 0;
}
if(opa == 2){
cout << "enter number 1" << endl;
cin >> n1;
cout << "enter number 2" << endl;
cin >> n2;
sum = n1 - n2;
cout << "the sum is " << sum;
return 0;
}
if(opa == 3){
cout << "enter number 1" << endl;
cin >> n1;
cout << "enter number 2" << endl;
cin >> n2;
sum = n1 * n2;
cout << "the sum is " << sum;
return 0;
}
if(opa == 4){
cout << "enter number 1" << endl;
cin >> n1;
cout << "enter number 2" << endl;
cin >> n2;
sum = n1 / n2;
cout << "the sum is " << sum;
return 0;
}
if(opa > 4){
cout << "You entered a invalid number";
goto calculator;
}
info:
cout << "Created by Bergur 2013";
return 0;
fun:
cout << "You want an eyepad(ipad)?";
}
Your code already checks:
while (c != 13)
And prevent newline from being handled, do similar to what you need with the backspace character, whose number is 8
To fix your issue, before:
StarNum++;
Add:
if (c == 8 && StarNum > 0) {
StarNum--;
if (password.size () > 0)
password.resize (password.size () - 1);
continue;
}
Please format your code. Also, try to provide a minimal code which reproduce the problem, not the whole code.
Unrelated to your problem
Try not to use goto.
Do not use ASCII values, but use the char literals instead, i.e. use '\n' instead of 13.
Also, you can simply add an iother condition inside your while:
while (c != '\n' && c != ' ')