so this is a ship's database and my problem is that I have to check if the user enter an integer, but when the program saves registry to the array and I show the result the registry variable where the user puts the number its an scramble of numbers
int tester1 = 0;
cout << "What is the registry of the ship?: ";
cout << " >>";
if (cin >> tester1)
{
tester1 >> array[counter].registry;
cin.clear();
cin.ignore(1000, '\n');
}
else
{
while (!(cin >> tester1))
{
cout << "Please enter an integer!!" << endl;
cout << " >>";
cin >> tester1;
cin.clear();
cin.ignore(1000, '\n');
}
}
cin>>tester1;
while(!cin) {
cin.clear();
cin.sync();
cout << "Please enter an integer!!" << endl;
cout << " >>";
cin>>tester1;
}
array[counter].registry=tester1;
Related
void addItem(struct InventoryItem inventory[], int& size){
cout << "\nEnter the item name: ";
cin.getline(inventory[size].itemName, 100, '\n');
while (strlen(inventory[size].itemName) == 0){
cout << "Invalid item name!" << endl;
cout << "Enter item name: ";
cin.getline(inventory[size].itemName, MAX_CHAR, '\n');
}
cout << "Enter the item price: ";
cin >> inventory[size].itemPrice;
while(!cin || inventory[size].itemPrice < 0){
cout << "Please enter a valid number: ";
cin >> inventory[size].itemPrice;
clear();
}
cout << endl;
size++;
}
My !cin keeps accepting letters as numbers ...
any ideas why?
note: I'm new to programming.
If operator>> fails to extract a value, it puts the input stream into an error state. You are not clearing that state, or ignoring the bad input that caused it to fail, so operator>> keeps failing.
Try this instead:
void addItem(struct InventoryItem inventory[], int& size){
do {
cout << "Enter the item name: ";
if (cin.getline(inventory[size].itemName, MAX_CHAR, '\n')) {
if (strlen(inventory[size].itemName) > 0) {
break;
}
}
else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Invalid item name!" << endl;
}
while (true);
do {
cout << "Enter the item price: ";
if (cin >> inventory[size].itemPrice) {
if (inventory[size].itemPrice >= 0) {
break;
}
}
else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Invalid item price!" << endl;
}
while (true);
cout << endl;
size++;
}
I have a problem with my code, it skips 6 lines of code, and I don't know what the problem could be. I'm just practicing C++, making a bank app, and in the registration menu it skips 6 lines of code for some reason. I would appreciate any help or suggestion! The code can look a little dirty. I don't understand why the code skips the input for the cityAddress, stateAddress, zipAddress after I type the houseAddress input.
// Registration menu code
void registerMenu() {
bool registerSuccess = false;
bool usernameSuccess = false;
string saveInfo;
system("CLS"); // Clear Console
cout << "Please create your account.\n";
cout << "First Name: ";
cin >> firstName;
cout << "Last Name: ";
cin >> lastName;
cout << "Phone Number: ";
cin >> phoneNumber;
cout << "Address: ";
cin >> houseAddress;
cout << "City: ";
cin >> cityAddress;
cout << "State: ";
cin >> stateAddress;
cout << "Zip code: ";
cin >> zipAddress;
cout << "\n\n";
cout << "Save information?\nY/N\n";
cin >> saveInfo;
if (saveInfo == "Y") {
cout << "-----------INFORMATION SAVED!-----------\n";
}
else if (saveInfo == "N") {
registerMenu();
}
else {
registerMenu();
}
cout << "\n\n";
do {
cout << "Username: ";
cin >> username;
ifstream usernameCheck("user_" + username + ".txt");
if (usernameCheck.is_open()) {
cout << "This username already exists. Create a different username.\n\n";
Sleep(1000);
}
else {
cout << "\n\t! USERNAME AVAILABLE !\n";
usernameSuccess = true;
}
} while (!usernameSuccess);
do {
cout << "Password: ";
cin >> password;
if (password.length() >= 8) {
cout << "Initial deposit to your account: $";
cin >> balance;
system("CLS"); // Clear Console
cout << "Registration complete!\n";
// [START] Create Account file
ofstream registration;
registration.open("user_" + username + ".txt");
registration << username << endl << password << endl << balance;
registration.close();
// [FINISH] Create Username file
registerSuccess = true;
password = password;
Sleep(1000);
system("CLS"); // Clear Console
cout << "--------------------------------" << endl;
cout << " Account Information\n";
cout << "Username: " << username << endl;
cout << "Password: " << password << endl;
cout << "Balance: $" << balance << endl;
cout << "--------------------------------" << endl;
cout << "Forwarding you in 5 seconds..." << endl;
Sleep(5000);
mainMenu();
}
else {
cout << "\n\nPassword must contain at least 8 characters. (You entered " << password.length() << " characters)\nPlease try again.\n";
}
} while (!registerSuccess);
}
Home addresses usually have spaces in them. Cin reads up to the first delimiter which is a space. Instead, try
std::getline(cin, houseAddress)
So after I enter my input in option 1, then I chose to continue. My input information are gone when choosing the option 2.
I just started my programming course so please give me a lot of advice.
This is the output enter image description here
struct Human
{
string name;
string foods;
string date;
};
char option;
int main()
{
do
{
int user;
cout << "You" <<endl;
cout << "\nChoose one from the menu below" <<endl;
cout << "1 Enter information" <<endl;
cout << "2 See information stored" <<endl;
cout << "\nEnter your option" <<endl;
cin >> user;
Human me;
switch(user)
{
case 1:
{
cin.ignore(100, '\n');
cout << "\nEnter your name: ";
getline(cin, me.name, '\n');
cout << "Enter your favorite foods: ";
getline(cin, me.foods, '\n');
cout << "Enter your birthday: ";
getline(cin, me.date, '\n');
break;
}
case 2:
{
cin.ignore(100, '\n');
cout << "Your name is: " << me.name<<endl;
cout << "Your favorite foods are: " <<me.foods<<endl;
cout << "Your birthday is: " <<me.date<<endl;
break;
}
}
cout << "\nDo you wish to continue?" <<endl;
cout << "Enter 'y' to continue and 'n' to exit" <<endl;
cout << "Your choice:";
cin >> option;
}while(option == 'y');
return 0;
}
You create a new Human me; in every iteration of the do-while loop. When you read input for that me in one iteration, the me in the next iteration won't know about that.
Read about "scope" and moving the declaration Human me; outside of the loop will probably do what you expect.
#include<iostream>
using namespace std;
struct Human
{
string name;
string foods;
string date;
};
char option;
int main()
{
Human me;
do
{
int user;
cout << "You" <<endl;
cout << "\nChoose one from the menu below" <<endl;
cout << "1 Enter information" <<endl;
cout << "2 See information stored" <<endl;
cout << "\nEnter your option" <<endl;
cin >> user;
switch(user)
{
case 1:
{
cin.ignore(100, '\n');
cout << "\nEnter your name: ";
getline(cin, me.name, '\n');
cout << "Enter your favorite foods: ";
getline(cin, me.foods, '\n');
cout << "Enter your birthday: ";
getline(cin, me.date, '\n');
break;
}
case 2:
{
cin.ignore(100, '\n');
cout << "Your name is: " << me.name<<endl;
cout << "Your favorite foods are: " <<me.foods<<endl;
cout << "Your birthday is: " <<me.date<<endl;
break;
}
}
cout << "\nDo you wish to continue?" <<endl;
cout << "Enter 'y' to continue and 'n' to exit" <<endl;
cout << "Your choice:";
cin >> option;
}while(option == 'y');
return 0;
}
So, Here is The Working Code problem is that you declare struct variable in the loop so every time loop runs it gets again initialized and previous lost
enter image description here
The following code doesn't give the second prompt to "enter message". How do I fix it?
cout << "Enter shifts:" << endl;
cin >> shifts;
cout << "Enter message:" << endl;
getline(cin, msg);
try this one
cout << "Enter shifts:" << endl;
cin >> shifts;
cout << "Enter message:" << endl;
cin.ignore();
getline(cin, msg);
use cin.ignore(); before using getline anywhere.
when you enter shifts there is a newline which read by geline funtion. So You need to ignore that newline.
write :
cout << "Enter shifts:" << endl;
cin >> shifts;
getchar();
cout << "Enter message:" << endl;
getline(cin, msg);
#include <iostream>
#include <limits>
#include <math.h>
using namespace std;
int main()
{
float startTemperature;
float endTemperature;
float loopTemperature;
float stepSize;
float i;
float numberOne;
cout << "Please enter a start temperature: " << endl;;
cin >> startTemperature;
while(!(cin >> startTemperature)){
cin.clear();
cout << "Invalid input. Try again: ";
}
cout << "Please enter an end temperature: ";
cin >> endTemperature;
while(!(cin >> endTemperature)) {
cin.clear();
cin.ignore(256, '\n');
cout << "Invalid temperature. Please try again: ";
}
cout << "Please enter a step size: ";
cin >> stepSize;
while(!(cin >> stepSize)) {
cin.clear();
cin.ignore(256, '\n');
}
for(i = startTemperature; i < endTemperature; i += stepSize) {
if(i == startTemperature) {
cout << "Celsius" << endl;
cout << "-------" << endl;
cout << startTemperature << endl;
loopTemperature = startTemperature + stepSize;
}
loopTemperature += stepSize;
if(loopTemperature > 20) {
break;
}
cout << loopTemperature << endl;
}
}
Hi, The problem with this code is that I have to input the value of the temperature twice. I have looked at other answers and I think it is something to do with the cin buffer but I don't know exactly what is wrong.
In the line
cin >> startTemperature; // <---problem here
while(!(cin >> startTemperature)){
cin.clear();
cout << "Invalid input. Try again: ";
}
You are taking input once, then again in the loop. That's is why you had to give input twice.
Just remove first input line, same for endTemparature and stepSize.
You're asking for input before the while loop, then again in the loop condition statement. Change the condition in your while statement to
while(!cin){
//error handling you already have
cin>>startTemperature; //endTemperature respectively
}
It's not only for the temperature but rather for every input. Change your code to the one below:
cout << "Please enter a start temperature: " << endl;;
while (!(cin >> startTemperature)){
cin.clear();
cin.ignore(std::numeric_limits<int>::max(), '\n');
cout << "Invalid input. Try again: ";
}
cout << "Please enter an end temperature: ";
while (!(cin >> endTemperature)) {
cin.clear();
cin.ignore(std::numeric_limits<int>::max(), '\n');
cout << "Invalid temperature. Please try again: ";
}
cout << "Please enter a step size: ";
while (!(cin >> stepSize)) {
cin.clear();
cin.ignore(std::numeric_limits<int>::max(), '\n');
cout << "Invalid step size. Please try again: ";
}
Reason:
You had redundant cin calls. Also use std::cin.ignore(std::numeric_limits<int>::max(), '\n'); instead of arbitrary number 256.