I am having an issue with the getline function [duplicate] - c++

This question already has answers here:
cin and getline skipping input [duplicate]
(4 answers)
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 21 days ago.
#include <iostream>
using namespace std;
int main(){
//set variables
string name1;string name2;string name3;string n1;string n2;string n3;
int age1; int age2; int age3;
//get names and ages
cout << "Who Is The First Student?"<< endl;
getline(cin, name1); //name 1
cout << "What is " << name1<< "'s Age?"<< endl;
cin >> age1; // age 1
cout << "Who Is The Second Student?"<< endl;
getline(cin, name2); //name 2
cout << "What is " << name2<< "'s Age?"<< endl;
cin >> age2; //age 2
cout << "Who Is The Third Student?"<< endl;
getline(cin, name3); // name 3
cout << "What is " << name3<< "'s Age?"<< endl;
cin >> age3; //age 3
// gets modified names
n1 = name1.substr(2, name1.size() -3);
n2 = name2.substr(2, name2.size() -3);
n3 = name3.substr(2, name3.size()-3);
// Output formatting
cout << "Name Age Modified"<<endl;
cout << name1<< " "<<age1<<" "<<n1<<endl;
cout << name2<< " "<<age2<<" "<<n2<<endl;
cout << name3<< " "<<age3<<" "<<n3<<endl;
return 0;
}
The output asks the first question which is for the name of the first student but it outputs as this:
Who Is The First Student?-
John Doe-
What is John's Age?-
19-
Who Is The Second Student?-
What is 's Age?-
It is skipping the user input of the second student's name and instantly asking for the age but I don't know why this is happening, is there something wrong with my code or do I have the formatting incorrect? I believe that I used the getline function correctly but I may be incorrect and unaware of it being skipped over by a more important function.

The std::string::substr() man page says this about the exception you are seeing:
Parameters
pos - position of the first character to include
count - length of the substring
Exceptions
[throws] std::out_of_range if pos > size()

The main problem of the program is that the operator >> reads only one word.
That is for example in these statements
cout << "Who Is The First Student?"<< endl;
cin >> name1; //name 1
You entered a string that contains two words "John Doe". But the operator >> reads only the first word "Jphn" in the variable name1.
So in the next input statement
cout << "What is " << name1<< "'s Age?"<< endl;
cin >> age1; // age 1
an error occurred because instead of a number the input buffer contains the word "Doe".
So as a result the variables name1, name2, and name3 do not contain what you are expecting. And these statements
n1 = name1.substr(2, name1.size()-3);
n2 = name2.substr(2, name2.size()-3);
n3 = name3.substr(2, name3.size()-3);
produce the run-time error.
Instead of the operator >> you should use standard function std::getline.
Here is a demonstration program based on the code of your program that shows what is the reason of the error
#include <iostream>
#include <string>
int main()
{
std::string name1, name2;
int age1;
//get names and ages
std::cout << "Input 3 Names Below:" << std::endl;
std::cout << "Who Is The First Student?" << std::endl;
std::cin >> name1; //name 1
std::cout << "What is " << name1 << "'s Age?" << std::endl;
std::cin >> age1; // age 1
std::cout << "Who Is The Second Student?" << std::endl;
std::cin >> name2; //name 2
std::cout << "name1 = " << name1 << '\n';
std::cout << "name2 = " << name2 << '\n';
}
The program output is
Input 3 Names Below:
Who Is The First Student?
John Doe
What is John's Age?
Who Is The Second Student?
name1 = John
name2 =
As you can see due to the error of reading a data in the variable age1 (the buffer contains the string "Doe") the object name2 is empty. So if you will use it in this statement
n2 = name2.substr(2, name2.size()-3);
then the runtime error will occur.
Here is another demonstration program that shows how the standard function std::getline can be used in your program.
#include <iostream>
#include <string>
#include <limits>
int main()
{
std::string name1, name2, name3;
int age1, age2, age3;
//get names and ages
std::cout << "Input 3 Names Below:" << std::endl;
std::cout << "Who Is The First Student?" << std::endl;
std::getline( std::cin, name1 ); //name 1
std::cout << "What is " << name1 << "'s Age?" << std::endl;
std::cin >> age1; // age 1
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
std::cout << "Who Is The Second Student?" << std::endl;
std::getline( std::cin, name2 ); //name 2
std::cout << "What is " << name2 << "'s Age?" << std::endl;
std::cin >> age2; //age 2
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
std::cout << "Who Is The Third Student?" << std::endl;
std::getline( std::cin, name3 ); // name 3
std::cout << "What is " << name3 << "'s Age?" << std::endl;
std::cin >> age3;
std::cout << "name1 = " << name1 << '\n';
std::cout << "name2 = " << name2 << '\n';
std::cout << "name3 = " << name3 << '\n';
}
The program output is
Input 3 Names Below:
Who Is The First Student?
John Dow
What is John Dow's Age?
20
Who Is The Second Student?
Mary Poppins
What is Mary Poppins's Age?
21
Who Is The Third Student?
Bob Fisher
What is Bob Fisher's Age?
25
name1 = John Dow
name2 = Mary Poppins
name3 = Bob Fisher

Related

How to create a function to read/write data to struct members?

I'm trying to create a program that read and print students' data with c++. for that, I've created a struct Student, a function to read data from the user and assign it to a struct instance s1 and a function to print students' data on the screen, and I think the problem is with the function that read/write data.
Here is the my code:
#include<iostream>
#include<string>
using namespace std;
struct Student
{
char name[30];
int age;
double gpa;
string department;
};
Student read_data(Student x)
{
cout << "Name (30 characters maximum): ";
cin.get(x.name, 30);
cout << "Age: ";
cin >> x.age;
cout << "Department: ";
cin >> x.department;
cout << "GPA: ";
cin >> x.gpa;
return x;
}
void print_data(Student x)
{
cout <<
"\n***************************************************************" << endl;
cout << "Name: " << x.name << endl;
cout << "Age: " << x.age << endl;
cout << "Department: " << x.department << endl;
cout << "GPA: " << x.gpa << endl;
}
int main()
{
Student s1, s2, s3;
cout << "This program stores -Temporarily- data of three students\n" << endl;
cout << "Enter 1st student's data" << endl;
read_data(s1);
print_data(read_data(s1));
system("pause");
return 0;
}
The output of this code is:
This program stores data of three students
Enter 1st student's data
Name (30 characters maximum): Ahmed Maysara
Age: 22
Department: CS
GPA: 3.5
Name (30 characters maximum): Age: Department: GPA:
***************************************************************
Name:
Age: -858993460
Department:
GPA: -9.25596e+61
Press any key to continue . . .
As you see, the output is out of my expectations :) ..
Any help ?!
Both CinCout and David are correct.
There are a couple of problems with your code as it now stands.
The first problem is that while you successfully call the function read_data(s1), s1 is a just a copy. So, when the function sets all of the values for the student using cin, it is really just setting a copy's values. You can either make it so that you are passing in the original, or you can return the student (which you are doing) and set s1 equal to the result (which you are not).
To make sure that you pass in the original, you can go to where you declared read_data. Instead of saying Student read_data(Student x), you should place an ampersand after the parameter that you don't want to copy Student read_data(Student &x). This is called passing by reference (you reference the original instead of referencing by copy)
Alternatively, you could con just set s1 to the result where you call it in main. You could say s1 = read_data(s1); and that would work fine, though a bit more inefficiently.
Lastly, the other glaring error in the code is that you accidentally call read_data again when you say print_data(read_data(s1)). Instead, say print_data(s1).
Instead of passing and returning the structure object each time on call of read_data and print_data we could add those inside the structure itself, We could create object of Student and call the functions read and print within the same.
struct Student
{
char name[30];
int age;
double gpa;
string department;
Student(): age(0), gpa(0)
{
memset( name, 0, 30 );
}
void read()
{
cout << "\nName (30 characters maximum): ";
cin.get(name, 30);
cout << "\nAge: ";
cin >> age;
cout << "\nDepartment: ";
cin >> department;
cout << "\nGPA: ";
cin >> gpa;
}
void print()
{
cout << "\n***************************************************************" << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Department: " << department << endl;
cout << "GPA: " << gpa << endl;
}
};
int main()
{
Student s1;
s1.read();
s1.print();
return 0;
}
You are passing copy of s1 into the read_data function, but not bothering to update the value based on the return arg. i.e. something like this should work.
s1 = read_data(s1);
print_data(s1);
Alternatively, pass by reference instead of value:
void read_data(Student& x)
{
cout << "Name (29 characters maximum): "; // requires null terminator
cin >> x.name; // just read into the buffer directly
cout << "Age: ";
cin >> x.age;
cout << "Department: ";
cin >> x.department;
cout << "GPA: ";
cin >> x.gpa;
}
And then later:
read_data(s1);
print_data(s1);
change you read_data with something like this
void read_data(Student& x)
{
cout << "Name (30 characters maximum): ";
///cin.get(x.name, 30);
cin.getline(x.name, 30);
cout << "Age: ";
cin >> x.age;
cin.ignore();
cout << "Department: ";
std::getline(cin, x.department);
///cin >> x.department;
cout << "GPA: ";
cin >> x.gpa;
cin.ignore();
// return x; can't return a value from a void function
}
and in main function or where you are calling the read_data function use
Student s1, s2, s3;
cout << "This program stores -Temporarily- data of three students\n" << endl;
cout << "Enter 1st student's data" << endl;
read_data(s1);
read_data(s2);
read_data(s3);
the reason you are getting weird values in return is that you capture buffer with cin >> instead getline
see
description of getline function
description of cin.ignore function

Run time error of cin.get() [duplicate]

This question already has answers here:
Using getline(cin, s) after cin [duplicate]
(13 answers)
Closed 5 years ago.
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
void cpp_string();
void cstyle_string();
int main()
{
cpp_string();
cstyle_string();
system("pause");
return 0;
}
void cpp_string()
{
string fName, lName;
char grade;
int age;
cout << "What is your first name?";
getline(cin, fName);
cout << "What is your last name?";
getline(cin, lName);
cout << "What letter grade do you deserve?";
cin >> grade;
cout << "What is your age?";
cin >> age;
cout << "Name: " << fName << ", " << lName << endl;
cout << "Grade: " << grade << endl;
cout << "Age: " << age << endl;
return;
}
void cstyle_string()
{
char fNm[20], lNm[20];
char grade;
int age;
cout << "What is your first name?";
cin.get(fNm, 20).get();
cin.clear();
cout << "What is your last name?";
cin.get(lNm, 20).get();
cout << "What letter grade do you deserve?";
cin >> grade;
cout << "What is your age?";
cin >> age;
cout << "Name: " << fNm << ", " << lNm << endl;
cout << "Grade: " << grade << endl;
cout << "Age: " << age << endl;
return;
}
I'm getting output as
What is your first name?demiurge conon
What is your last name?no
What letter grade do you deserve?a
What is your age?22
Name: demiurge conon, no
Grade: a
Age: 22
What is your first name?What is your last name?What letter grade do you deserve?What is your age?Name: ,
Grade: ╠
Age: -858993460
Press any key to continue . . .
but if I run cstyle_string() in different file then I'm not getting any errors code works perfectly.
I want to know why this is happening?
There are two question.
Redundant \n
the state of cin
The last cin in cpp_string is cin >> age.
It will leave a \n not extracted.
In first of cstyle_string is cin.get(fNm, 20).get();
The delimiting character is not extracted from the input sequence if found, and remains there as the next character to be extracted from the stream
the cin.get(FNm, 20) will parse empty input before \n, and no characters are available in the stream in actually. In this case, the failbit flag will be set and next all cin >> operator will fail.
You can only call cstyle_string and press enter directly, the same thing will happen.

is there a better way? new to c++

I am new to c++ but do have a basic knowledge in coding. This program works fine and well but I'm wondering if there is a better way to do this.
The program makes a star wars name by taking the first three letters of your last name and the first 2 of your first name to make your first name of your star wars name. Then for your star wars surname it takes the first two letters of your mother's maiden name and the first three letters of the city you were born in.
// starWarsName.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string firstName;
string surname;
string maidenName;
string city;
cout << "This program is designed to make you a star wars name, it takes some information and concatinates parts of the information to make your NEW name" <<endl << endl;
cout << "please enter your first name" << endl;
cin >> firstName;
cout << "please enter your surname" <<endl;
cin >> surname;
cout << "what is your mothers maiden name?" << endl;
cin >> maidenName;
cout << "please tel me which city you were born in" << endl;
cin >> city;
cout << firstName << " " << surname << endl;
cout << firstName[0] << " " << surname << endl;
int size = firstName.length();
//cout << size;
cout << surname[0] << surname[1] << surname[2] << firstName[0] << firstName[1];
cout << " " << maidenName[0] << maidenName[1] << city[0] << city[1] << city[2];
cin.get();
cin.ignore();
return 0;
}
You can use string::substr here to store character sequence instead of writing surname[0]..surname[2] again and again.
Here is an example of string::substr
#include <iostream>
#include <string>
int main ()
{
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (3,5); // "think"
std::size_t pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos); // get from "live" to the end
std::cout << str2 << ' ' << str3 << '\n';
return 0;
}
Output:
think live in details.

"getline" prompt gets skipped, not working as intended

Here are my codes:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int age1;
int age2;
string name1;
string name2;
cout << "Please enter the name for one people: " << "\n";
getline (cin, name1);
cout << "Please enter the age for this people: " << "\n";
cin >> age1;
cout << "Please enter the name for another people: " << "\n";
getline (cin, name2);
cout << "Please enter the age for this people too: " << "\n";
cin >> age2;
if ( (age1 <= 100 || age2 <= 100) && (age1 < age2) )
{
cout << name1 << " is younger!" << "\n";
}
else if ( (age1 <= 100 || age2 <= 100) && (age1 > age2) )
{
cout << name2 << " is younder!" << "\n";
}
else if ( (age1 <= 100 || age2 <= 100) && (age1 = age2) )
{
cout << name1 << " and " << name2 << " are of the same age!" << "\n";
}
else
{
cout << "You've got some really old people that are well older than 100!";
}
}
The first getline and cin works fine. I am able to be prompted to input.
However, the second getline and cin are prompted at once, thus I can only input for cin. (The second getline is skipped!)
If I use four cins, the program will work properly.
cin >> age1; does not read the newline character following the number. The newline remains in the input buffer, then prematurely stops the second getline.
So, your program already works as long as you enter the first age and the second name on the same line.
One solution would be to skip whitespace after the numbers:
cin >> age1 >> ws;
Live demo.
first: cin>>age; It takes the number and stores into age but at the same
time it leaves the newline character in the buffer itself. so when there is prompt for next name cin finds that left over newline character in the buffer and takes it as the input. that it why it escapes the name2 prompt.
cout << "Please enter the name for one people: " << "\n";
cin>>name1;
cout << "Please enter the age for this people: " << "\n";
cin >> age1;<<--**this left the new line character in input buffer**
cin.get();<<-- **get that newline charachter out of there first**
cout << "Please enter the name for another people: " << "\n";
getline (cin, name2);
cout << "Please enter the age for this people too: " << "\n";
cin >> age2;
now i give name1-> shishir age1->28
name2->ccr age-> 22 it prints ccr is younder!<-- the spelling is wrong too :D
for more info on getline and get() read c++ primer plus listing 4.3, 4.4, 4.5
Happy coding
You need a ; after getline (cin, name);
hope this helps
I would suggest using cin.ignore(100,'\n'). It ignores the amount of characters you specify when you call it(100 in the example above), up to the char you specify as a breakpoint. For example:
cout << "Please enter the name for one people: " << "\n";
getline (cin, name1);
cout << "Please enter the age for this people: " << "\n";
cin >> age1;
cin.ignore(100, '\n');
cout << "Please enter the name for another people: " << "\n";
getline (cin, name2);
cout << "Please enter the age for this people too: " << "\n";
cin >> age2;
cin.ignore(100, '\n');

Multiplying double type values gives random/garbage answer in output

#include <iostream>
#include <string>
using namespace std;
int main() {
double age;
double months;
string name;
months = age*12.0;
cout << "Enter your name and age: ";
cin >> name >> age;
cout << "Hello " << name << " age " << age << " (" << months << " months)\n";
return(0);
}
The program asks for name and age, and should out put the name and age in a sentence with the age in months in parentheses.
Output gives something like:
Hello Bob age 20 (1.82561e-313 months), but should be Hello Bob age 20 (240 months). I did not use int because I wanted to be able to input non int values for age.
I have tried 12 instead of 12.0 and tried declaring a variable and doing months = age*m where m = 12.0 but is result is the same. By the way, the random value is about the same regardless of what variable age is. Why is this happening? Also, would this be a link-time error or run-time error?
You're doing the multiplication using age before it's initialized. What value would you expect it to give you?
Change your code to first get the age, and then do the multiplication:
cout << "Enter your name and age: ";
cin >> name >> age;
months = age*12.0;
cout << "Hello " << name << " age " << age << " (" << months << " months)\n";
Move the calculation of months to after the point age is entered (age was uninitialised in the original code):
cout << "Enter your name and age: ";
cin >> name >> age;
months = age*12.0;
cout << "Hello " << name << " age " << age << " (" << months << " months)\n";