Why am I getting Warning: C4244? - c++

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));

Related

Attempt at making a currency converter but it doesn't start at all

This is my final project for the semester. When I run it on replit, the program runs but doesn't work fully after the first step. When I run it on VS Code, it gives me a linker command error. This is due in 3 days, please any help is appreciated. Thank you
Below is my code:
`
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
const int HEAD_SPACE = 23;
const int SECND_HDSPACE = 35;
const int THRD_HDSPACE = 27;
char startKey;
char replayKey;
int userChoice;
int main()
{
float conversion(void);
firstStart:
cout << "=======================================================\n\n";
cout << setw(SECND_HDSPACE) << "\t\t\t ::: CURRENCY CONVERTER :::\n\n";
cout << "=======================================================\n\n";
cout << setw(SECND_HDSPACE) << "\t\t\tPlease select any options below\n\n";
cout << setw(SECND_HDSPACE) << "\t\t\t1) Press [1] to convert from USD to GBP\n";
cout << setw(SECND_HDSPACE) << "\t\t\t2) Press [2] to convert from USD to EUR\n";
cout << setw(SECND_HDSPACE) << "\t\t\t3) Press [3] to convert from USD to NZD\n";
cout << setw(SECND_HDSPACE) << "\t\t\t4) Press [4] to convert from USD to JPY\n";
cout << setw(SECND_HDSPACE) << "\t\t\n\n :: PRESS [S] TO START PROGRAM :: \n\n";
menuOption:
cin >> startKey;
if(startKey == 's' && startKey == 'S')
{
float resultVal = conversion();
cout << "The conversion is: " << resultVal << endl;
cout << "Would you like to run the program again? Press Y or N" << endl;
scndStrt:
cin >> replayKey;
if (replayKey == 'y' && replayKey == 'Y')
{
goto firstStart;
}
else if ((replayKey == 'n' && replayKey == 'N'))
{
cout << "Thanks for using my program!" << endl;
}
else
{
cout << "Wrong Key Entered. Please hit Y or N" << endl;
goto scndStrt;
}
}
else
{
cout << "Wrong Key Entered. Please Hit [S]";
goto menuOption;
}
float convervion(void);
int selection;
int currChoice;
float currency1;
float currency2;
cout << "Please enter the currency name: " << endl;
cin >> selection;
cout << "Please enter the amount you would like to convert: " << endl;
cin >> currency1;
switch (selection)
{
case '1':
formulaInput:
cout << "Please enter what currency you would like to conver in: " << endl;
cin >> currency2;
if (currency2 == '1')
{
currency2 = currency1 * 1;
}
else if (currency2 == '2')
{
currency2 = currency1 * 0.84;
}
else if (currency2 == '3')
{
currency2 = currency1 * 0.94;
}
else if (currency2 == '4')
{
currency2 = currency1 * 1.92;
}
else
{
cout << "Incorrect input, please try again." << endl;
goto formulaInput;
}
}
}
`
I tried to watch some videos but the terminology and syntax seemed too advanced for me. I'm only a first year CS student and this is my first semester.
First of all, this program will not run correctly. But beside of that and to your question:
You have to close the main and open the conversion function(s). Check all your curly braces to be correctly.
Your function declaration is inside the main function. As retired Ninja in the comment section said, it is not incorrect and you can do this. Please think about local and global scope. (Within the main function it will become local scope for main and outside the declaration would be in global scope)
But if you think about it, if you wold define another function beneath the in local scope declared function, the same function will become global scope anyway. So for this reason it would be preferable to declare the function in global scope directly. For now, just from the standpoint, to avoid unnecessary irritation.
You have a misspell in your functions declaration and definition. (conversion / convervion ) please check this.
Your function is declared and defined with a return value of float but you forget the final return statement, which you will need to print out the calculation result.
.
#include <iostream> // Do you realy need all of these includes?
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
const int HEAD_SPACE = 23;
const int SECND_HDSPACE = 35;
const int THRD_HDSPACE = 27;
char startKey;
char replayKey;
int userChoice;
float conversion(void);
int main()
{
firstStart:
cout << "=======================================================\n\n";
cout << setw(SECND_HDSPACE) << "\t\t\t ::: CURRENCY CONVERTER :::\n\n";
cout << "=======================================================\n\n";
cout << setw(SECND_HDSPACE) << "\t\t\tPlease select any options below\n\n";
cout << setw(SECND_HDSPACE) << "\t\t\t1) Press [1] to convert from USD to GBP\n";
cout << setw(SECND_HDSPACE) << "\t\t\t2) Press [2] to convert from USD to EUR\n";
cout << setw(SECND_HDSPACE) << "\t\t\t3) Press [3] to convert from USD to NZD\n";
cout << setw(SECND_HDSPACE) << "\t\t\t4) Press [4] to convert from USD to JPY\n";
cout << setw(SECND_HDSPACE) << "\t\t\n\n :: PRESS [S] TO START PROGRAM :: \n\n";
menuOption:
cin >> startKey;
if (startKey == 's' && startKey == 'S')
{
float resultVal = conversion();
cout << "The conversion is: " << resultVal << endl;
cout << "Would you like to run the program again? Press Y or N" << endl;
scndStrt:
cin >> replayKey;
if (replayKey == 'y' && replayKey == 'Y')
{
goto firstStart;
}
else if ((replayKey == 'n' && replayKey == 'N'))
{
cout << "Thanks for using my program!" << endl;
}
else
{
cout << "Wrong Key Entered. Please hit Y or N" << endl;
goto scndStrt;
}
}
else
{
cout << "Wrong Key Entered. Please Hit [S]";
goto menuOption;
}
}
float conversion()
{
int selection;
int currChoice;
float currency1;
float currency2;
cout << "Please enter the currency name: " << endl;
cin >> selection;
cout << "Please enter the amount you would like to convert: " << endl;
cin >> currency1;
switch (selection) // ??
{
case '1':
formulaInput:
cout << "Please enter what currency you would like to conver in: " << endl;
cin >> currency2; // This is not working.
if (currency2 == '1')
{
currency2 = currency1 * 1;
}
else if (currency2 == '2')
{
currency2 = currency1 * 0.84;
}
else if (currency2 == '3')
{
currency2 = currency1 * 0.94;
}
else if (currency2 == '4')
{
currency2 = currency1 * 1.92;
}
else
{
cout << "Incorrect input, please try again." << endl;
goto formulaInput;
}
}
// Here you have to return something?
return currency2;
}

How I can use CASE in a different way? [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 2 years ago.
Improve this question
Hello I am trying to build a project but the case '2' is not working.
struct student
{
string fname;//for student first name
string lname;//for student last name
string id;//for Registration No number
string course;//for class info
}studentData;//Variable of student type
struct teacher
{
string fname;//first name of teacher
string lname;//last name of teacher
string id;//Bool Group
string course;//Number of serves in School
}teach;//Variable of teacher type
int main()
{
int i = 0, j;//for processing usage
char choice;//for getting choice
string find;//for sorting
string srch;
while (1)//outer loop
{
system("cls");//Clear screen
//Level 1-Display process
cout << "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\";
cout << "\n\n\t\t\tSCHOOL MANAGEMENT PROGRAM\n\n";
cout << "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\";
cout << "\n\n\t\t\tMAIN SCREEN\n\n";
cout << "Enter your choice: " << endl;
cout << "1.Students information" << endl;
cout << "2.Teacher information" << endl;
cout << "3. Course information " << endl;
cout << "4. Display Strudent information " << endl;
cout << "5. Display Teacher Information " << endl;
cout << "6. Display Course Information n" << endl;
cout << "7.Exit program" << endl;
cin >> choice;
system("cls");//Clear screen
switch (choice)//First switch
{
case '1': //Student
{
{ ofstream f1("student.txt", ios::app);
for (i = 0; choice != 'n'; i++)
{
if ((choice == 'y') || (choice == 'Y') || (choice == '1'))
{
cout << "Enter First name: ";
cin >> studentData.fname;
cout << "Enter Last name: ";
cin >> studentData.lname;
cout << "Enter Registration number: ";
cin >> studentData.id;
cout << "Enter class: ";
cin >> studentData.course;
f1 << studentData.fname << endl << studentData.lname << endl << studentData.id << endl << studentData.course << endl;
cout << "Do you want to enter data: ";
cout << "Press Y for Continue and N to Finish: ";
cin >> choice;
}
}
f1.close();
}
continue;//control back to inner loop -1
continue;//Control pass to 1st loop
}
case '2'://Teachers biodata
{
{ ofstream t1("teacher.txt", ios::app);
for (i = 0; choice != 'n'; i++)
{
if ((choice == 'y') || (choice == 'Y') || (choice == '1'))
{
cout << "Enter First name: ";
cin >> teach.fname;
cout << "Enter Last name: ";
cin >> teach.lname;
cout << "Enter Registration number: ";
cin >> teach.id;
cout << "Enter class: ";
cin >> teach.course;
t1 << teach.fname << endl << teach.lname << endl << teach.id << endl << teach.course << endl;
cout << "Do you want to enter data: ";
cout << "Press Y for Continue and N to Finish: ";
cin >> choice;
}
}
t1.close();
continue;//control back to inner loop -1
}
continue;//Control pass to 1st loop
}
case '3':
{
{ ifstream f2("student.txt");
cout << "Enter First name to be displayed: ";
cin >> find;
cout << endl;
int notFound = 0;
for (j = 0; (j < i) || (!f2.eof()); j++)
{
getline(f2, studentData.fname);
if (studentData.fname == find)
{
notFound = 1;
cout << "First Name: " << studentData.fname << endl;
cout << "Last Name: " << studentData.lname << endl;
getline(f2, studentData.id);
cout << "Registration No number: " << studentData.id << endl;
getline(f2, studentData.course);
cout << "Class: " << studentData.course << endl << endl;
}
}
if (notFound == 0) {
cout << "No Record Found" << endl;
}
f2.close();
cout << "Press any key two times to proceed";
_getch();//To hold data on screen
_getch();//To hold data on screen
}
continue;//control back to inner loop -1
}
case '4':
{
{ ifstream t2("teacher.txt");
cout << "Enter First name to be displayed: ";
cin >> find;
cout << endl;
int notFound = 0;
for (j = 0; (j < i) || (!t2.eof()); j++)
{
getline(t2, studentData.fname);
if (studentData.fname == find)
{
notFound = 1;
cout << "First Name: " << studentData.fname << endl;
cout << "Last Name: " << studentData.lname << endl;
getline(t2, teach.id);
cout << "Registration No number: " << studentData.id << endl;
getline(t2, teach.course);
cout << "Class: " << studentData.course << endl << endl;
}
}
if (notFound == 0) {
cout << "No Record Found" << endl;
}
t2.close();
cout << "Press any key two times to proceed";
_getch();//To hold data on screen
_getch();//To hold data on screen
}
continue;//control back to inner loop -1
}
case '5':
{}
case '6':
{}
case '7':
{
break;//outer case 3
}//outer case 3
break;//outer loop
}
}
}
inside your switch statement, inside the case where you are checking if "choice" is '2 (case '2'://Teachers biodata), you have yet another if statement which checks if "choice" is '1', but at that point "choice" will be '2'.
if ((choice == 'y') || (choice == 'Y') || (choice == '1'))
becomes
if ((choice == 'y') || (choice == 'Y') || (choice == '2'))

Undeclared Identifier C2065 "P"

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

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.

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 != ' ')