Just started coding C++ [duplicate] - c++

This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 3 years ago.
What seems to be the problem in my code?
When I hit Enter after entering my age, the prompt already finishes without asking for my address. I know I can use getline() for the age, but what if the user enters a non-integer answer?
Sorry, I just started coding yesterday and I want to learn the basics.
#include <iostream>
using namespace std;
int main()
{
int age;
string name, address;
cout << "Please enter the name of the user:" << endl;
getline(cin, name);
cout << "Please enter age of the user:" << endl;
cin >> age;
cout << "Please enter the address of the user:" << endl;
getline(cin, address);
cout << "Your name is " << name << " and you are " << age
<< " years old " << " who lives in " << address << endl;
cout << "Thank you for using my program!";
return 0;
}

Just add 'cin.ignore()' after 'cin>>age' like this:
cout << "Please enter age of the user:" << endl;
cin >> age;
cin.ignore();
cout << "Please enter the address of the user:" << endl;
getline(cin, address);
When getline() reads input, then a newline character is left in input stream, due to which it dosen't reads the string(address) in your program.
And if a user enters a 'float' or 'double' etc. instead of 'int' in age then it will simply extract out integer from it ,
for example:
if user enters 39.29 or 39.00005 or 39.00 or then age=39
To know more about getline check the following link:

Related

I'm writing simple struct program and the getline returns empty string [duplicate]

This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
getline not working properly ? What could be the reasons? [duplicate]
(3 answers)
getline not asking for input? [duplicate]
(3 answers)
cin and getline skipping input [duplicate]
(4 answers)
Closed 4 months ago.
I'm collage student and I'm trying to do simple struct program
I wrote this code then the second getline returns empty string.
#include <iostream>
#include <string>
using namespace std;
struct person
{
string name;
string phone;
int age;
float salary;
};
int main()
{
person p1;
cout << "Enter your name : ";
getline(cin, p1.name);
cout << "Enter your age : ";
cin >> p1.age;
cout << "Enter your phone number : ";
getline(cin, p1.phone);
cout << "Enter your salary : ";
cin >> p1.salary;
cout << "------------------------------------------------------------\n";
cout << "Your personal information : \n";
cout << "Name : " << p1.name << endl;
cout << "Age : " << p1.age << endl;
cout << "Phone number : " << p1.phone << endl;
cout << "Salary : " << p1.salary << endl;
return 0;
}
I though it was from the IDE but when I tried it on deferent IDE on deferent device I got the same result

C++ is skipping the first cin.getline method [duplicate]

This question already has answers here:
C++ Getline after Cin
(3 answers)
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 1 year ago.
I am developing a program where I am calling cin.getline() to get values from the user. My program is logically correct and program runs but, it somehow skips the first cin.getline() method and calls the second one and onwards.
Here's my code:
public:
Library()
{
myFile.open("testFile.txt", ios::app);
myFile2.open("testFile.txt", ios::in);
cout<<"Constructor calling"<<endl;
}
void addBookData()
{
cout<<"Enter a Book Name: "; // this gets printed
cin.getline(bookName, 50); // skips this, doesn't take input from user
cout<<"Enter the Author's Name: "; // this gets printed
cin.getline(authorName, 50); // takes value from the user here
cout<<"Enter the Genre: ";
cin.getline(genre, 30);
cout<<"Enter number of pages: ";
cin >> noOfPages;
cout<<"Enter rating: ";
cin >> rating;
cout<<"Enter language: "; // again this gets printed
cin.getline(language, 20); // this gets skipped as well
cout<<"Issued to (Username): ";
cin.getline(issuedTo, 30); // takes value from here
cout<<"Issued date: ";
cin.getline(issuedDate, 30);
myFile <<"Book Name: " << bookName << endl << "Author's Name: " << authorName << endl <<
"Genre: " << genre << endl << "Number of pages: " << noOfPages << endl << "Rating: " <<
rating << endl << "Language: " << language << endl << "Issued to (Username): " << issuedTo
<< endl << "Issued date: " << issuedDate << endl;
}
Can anybody explain to me why is this happening? What am I doing wrong? Any help would be appreciated. Thanks.

How do I get the program to request for input for the string "course" before they print out the while/if statement

How do i do that without switching the sequence of asking the questions in my program? I would like to get their age, before asking about the course. However, once the age is keyed in, the program automatically proceeds to print the while/if statement.
#include<iostream>
#include<string>
using namespace std;
int main()
{
int age; //Declares the variable "age"
string name;
string course;
cout << "Hi\n"; //Prints out "Hello World"
cout << "What is your name? ";
getline(cin, name);
cout << "\nHow old are you ? "; //Ask the user their age
cin >> age; //Records the value entered by user
cout << "What course are you picking up in university ? ";
getline(cin, course);
//If number entered is smaller than 0, print the following message
while (age < 0 ) {
cout << "\nYou can't be younger than 0 years old.";
cout << "\nPlease try again";
cout << "\n\nHow old are you ? ";
cin >> age;
}
//If number entered is larger than 100, print the following message
if (age > 100) {
cout << "\nYou are such a blessed person !\n\n";
}
//If number entered is between 1 and 99, print the following message
if (age > 1 && age < 99) {
cout << "Your name is " << name << " ,and you are " << age << " years old.";
cout << "\n You are planning on taking " << course << " in university.";
}
return 0;
}
If getline() is used after cin >>, the getline() sees this newline character as leading whitespace, and it just stops reading any further.
Solution:
Call cin.ignore() before calling getline()
Or
Make a dummy call getline() to consume the trailing newline character from the cin>>
#include<iostream>
#include<string>
using namespace std;
int main()
{
int age; //Declares the variable "age"
string name;
string course;
cout << "Hi\n"; //Prints out "Hello World"
cout << "What is your name? ";
getline(cin, name);
while(true) {
cout << "\nHow old are you ? "; //Ask the user their age
cin >> age; //Records the value entered by user
if(age>=0)
break;
cout << "\nYou can't be younger than 0 years old.\nPlease try again.";
}
cout << "What course are you picking up in university ? ";
getline(cin, course);
//If number entered is larger than 100, print the following message
if (age > 100) {
cout << "\nYou are such a blessed person !\n\n";
}
//If number entered is between 1 and 99, print the following message
if (age > 1 && age < 99) {
cout << "Your name is " << name << " ,and you are " << age << " years old.";
cout << "\n You are planning on taking " << course << " in university.";
}
return 0;
}

Code not allowing user input in strings after first string

I have looked around and can't seem to find an answer to this. I am new to C++ and am attempting to write a program for a class that asks the user for the first and last names of 4 students and their ages. The program will then display the input names and ages and also display the average of the ages.
The issue I am having is that the program allows for input of the first name and age but then skips over the remaining three name input fields and only allows for the remaining three ages to be input.
I apologize if this ends up being a dumb question but I really am at a loss. Any help will be greatly appreciated.
Here is the code I have thus far:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string studentname1;
cout << "Please enter the first student's full name:" << endl;
getline(cin, studentname1);
int age1;
cout << "Please enter the first student's age:" << endl;
cin >> age1;
string studentname2;
cout << "Please enter the second student's full name:" << endl;
getline(cin, studentname2);
int age2;
cout << "Please enter the second student's age:" << endl;
cin >> age2;
string studentname3;
cout << "Please enter the third student's full name:" << endl;
getline(cin, studentname2);
int age3;
cout << "Please enter the third student's age:" << endl;
cin >> age3;
string studentname4;
cout << "Please enter the fourth student's full name:" << endl;
getline(cin, studentname2);
int age4;
cout << "Please enter the fourth student's age:" << endl;
cin >> age4;
cout << "Hello from our group." << endl;
cout << "NAME AGE" << endl;
cout << studentname1 << " " << age1 << endl;
cout << studentname2 << " " << age2 << endl;
cout << studentname3 << " " << age3 << endl;
cout << studentname4 << " " << age4 << endl;
cout << "The average of all our ages is: " << (age1 + age2 + age3 + age4) / 4.00 << endl;
return 0;
}
Since the age variables are int, the cin >> age1; will leave the newline character in the input stream. When next you call getline(), you will get the remainder of that line - which is empty, and so on.
Also, you have a copy-paste bug in your code. getline(cint, studentname2); is run for students 2, 3 and 4.
You can either solve the problem by using getline() for all input:
string agestring;
getline(cin, agestring)
stringstream(agestring) >> age1;
or clear cin when you're done reading the age:
cin >> age1;
cin.ignore();

Problems with getline() function? [duplicate]

This question already has answers here:
getline not asking for input? [duplicate]
(3 answers)
Closed 8 years ago.
I have a problem with the getline function in the code below. In the "get" info section I want to be able to store a whole sentence, but I can't make it work.
When I open up my program it just skips the input for the info.
p.s: I'm new to C++
Here is the section of the code where i have the problem (Enter the information):
void add() {
string name;
string faction;
string classe;
string race;
string info;
ofstream wowdatabase("wowdatabase.txt", ios::app);
cout << "Add a race or class" << endl;
cout << "---------------------------------------------------------" << endl;
cout << "" << endl;
cout << "Enter the name of the race or class (only small letters!):" << endl;
cin >> name;
cout << "Enter Race (Type -, if writen in name section):" << endl;
cin >> race;
cout << "Enter Class (Type -, if writen in name section):" << endl;
cin >> classe;
cout << "Enter faction (Alliance/Horde):" << endl;
cin >> faction;
cout << "Enter the information:" << endl;
getline(cin, info);
wowdatabase << name << ' ' << faction << ' ' << classe << ' ' << race << ' ' << info << endl;
wowdatabase.close();
system("CLS");
main();
}
Now it works fine =) but, when i want to output the info again it only shows the first word in the sentence?
Before this statement
getline(cin, info);
make the following call
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
To use this call you must include header <limits>.
The problem is that after executing statement
cin >> faction;
the new line character that corresponds to enetered key ENTER is in the input buffer and the next getline call reads this character.
After you read fraction an empty line character will be left over. The consequent call to getline will read it. To avoid that add a call to cin.ignore before the getline call.