when i run this code the 2nd & 3rd files' names the first character disappear
for example if i named the 2nd file set2.txt it will be et2.txt
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream set1,set2,set3;
string name;
cout<<"Enter set1 name: ";
cin.ignore();
getline(cin , name);
set1.open(name,ios::out);
cout<<"Enter set2 name: ";
cin.ignore();
getline(cin , name);
set2.open(name,ios::out);
cout<<"Enter set3 name: ";
cin.ignore();
getline(cin , name);
set3.open(name,ios::out);
}
the first character disappear
That is because you ignore the first character with the line
cin.ignore();
before you read the a line of text with
getline(cin , name);
These two lines are in the wrong order. Instead they should be
getline(cin , name);
cin.ignore();
The purpose of cin.ignore() here is to remove the newline character from the input stream after you read a line of text with getline().
Related
I am beginner to the C++ and was trying to build first codes. Right now I'm trying to learn cin command but there is a problem. When I use this code:
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
string surname;
cout<<"Please enter your name: ";
cin>>name;
cout<<"Please enter your surname: ";
cin>>surname;
cout<<"Your name: "<<name <<endl;
cout<<"Your surname: "<<surname;
}
The output is:
Please enter your name: aaa bbb
Please enter your surname: Your name: aaa
Your surname: bbb
So when I use 2 names in the 'name' variable, this happens. But when I just write 'aaa' in the name and 'bbb' in the surname, it works perfectly fine:
Please enter your name: aaa
Please enter your surname: bbb
Your name: aaa
Your surname: bbb
What do I have to do? Thanks.
Use the getline function:
#include <iostream>
#include <string>
using namespace std;
int main() {
string name, surname;
cout << "Please enter your name: ";
getline(cin, name);
cout << "Please enter your surname: ";
getline(cin, surname);
cout << "Your name: " << name << endl;
cout << "Your surname: " << surname;
}
cin reads just till the first space, but getline reads till the first new line. That means that cin only takes the first word, and the next word "rests" unpicked on the console. When the next cin comes, it takes that "resting" word.
You have to include the #include<string> header when using function getline. You specify the input type as first argument (that's what you put in the parentheses – in our case cin), and as second argument is the string where you want to save that value. For example, getline(cin, input);. If you don't use namespace std;, you must also specify the namespace, so like this: std::getline(std::cin, input).
If you are still learning C++, you won't understand this, but maybe a little later: there are actually two forms ("overloads") of getline function: one is like I said before (istream& getline (istream& is, string& str, char delim);), but another also uses the 3rd parameter (istream& getline (istream& is, string& str);), like this: getline(cin, input, ';');. With that code, the input stream ends the input at the first semicolon. Note the ' sign, not " sign!
I have the following simple program, but the last line of code getline(cin, topicClass) is never excuted. However, if I use normal cin>>topicClass that is executed. Could you please help me out of this? Thanks
#include <iostream>
#include <string>
using namespace std;
void InfoInput()
{
string classID;
string teacherName;
int totalStudent;
string topicClass;
cout<<"Enter class ID"<<endl;
getline(cin, classID);
cout<<"Enter teacher's name"<<endl;
getline(cin, teacherName);
cout<<"Enter total number of students"<<endl;
cin>>totalStudent;
cout<<"Enter topic of class"<<endl;
getline(cin, topicClass);
//cin>>topicClass;
}
int main ()
{
InfoInput();
}
Your problem is above, with this line:
cin>>totalStudent;
This does not read a line. You enter your input and (I assume) you press ENTER. The \n remains in the buffer of std::cin and is read as an empty line with your next instruction:
getline(cin, topicClass);
To fix, use this code:
cin>>totalStudent;
while(std::isspace(cin.peek())
cin.ignore();
cout<<"Enter topic of class"<<endl;
getline(cin, topicClass);
There is \n remained in cin after reading totalStudent as integer so you need to get that \n first out of the system and then read the next string
#include <iostream>
#include <string>
using namespace std;
void InfoInput()
{
string classID;
string teacherName;
int totalStudent;
string topicClass;
cout<<"Enter class ID"<<endl;
getline(cin, classID);
cout<<"Enter teacher's name"<<endl;
getline(cin, teacherName);
cout<<"Enter total number of students"<<endl;
cin>>totalStudent;
cout<<"Enter topic of class"<<endl;
getline(cin,topicClass);
getline(cin, topicClass);
//cin>>topicClass;
}
int main ()
{
InfoInput();
}
Your classID and teacherName are local variables and disappear after execution leaves the function. You'll have to figure out some way, like passing parameters by reference, to share the variables between functions.
I'm working on a project of splicing an inputted string of a name, and for some reason it's not working. Part of it is code copied from my book that supposedly works, so I'm stuck. Am I doing something wrong?
#include <iostream>
#include <string>
using namespace std;
void main()
{
string name;
int index;
cout<<"Please enter your full name. ";
cin>>name;
cout<<"\n"<<endl;
index = name.find(' ');
cout<<"First Name: "<<name.substr(0, index)<<" "<<name.substr(0, index).length()<<endl;
name = name.substr(index+1, name.length()-1);
index = name.find(' ');
cout<<"Middle Name: "<<name.substr(0, index)<<" "<<name.substr(0, index).length()<<endl;
name = name.substr(index+1, name.length()-1);
cout<<"Last Name: "<<name<<" "<<name.length()<<endl;
}
Most peoples' names consist of at least two words. This will only get one of them:
cout<<"Please enter your full name. ";
cin>>name;
istream operator>> is whitespace delimited. Use getline instead:
std::getline(std::cin, name);
For your purposes, you could probably do this, which is simpler:
std::string first, middle, last;
std::cin >> first >> middle >> last;
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
getline not asking for input?
There is some unique thing happening in my program.
Here are some set of commands :
cout << "Enter the full name of student: "; // cin name
getline( cin , fullName );
cout << "\nAge: "; // cin age
int age;
cin >> age ;
cout << "\nFather's Name: "; // cin father name
getline( cin , fatherName );
cout << "\nPermanent Address: "; // cin permanent address
getline( cin , permanentAddress );
When i try to run this snippet along with the whole code.The output program works like :
output:
Enter the full name of student:
Age: 20
Father's Name:
Permanent Address: xyz
If you notice ,the program didn't ask me the full name and went on directly to ask me the age.Then it skips the father's name also and asks the permanent address.
What could be the reason for this ?
It is difficult for me to post the whole code because it is too large.
Since you have not posted any code. I am going to take a guess.
A common problem while using getline with cin is getline does not ignore leading whitespace characters.
If getline is used after cin >>, the getline() sees this newline character as leading whitespace, and it just stops reading any further.
How to resolve it?
Call cin.ignore() before calling getline()
Or
make a dummy call getline() to consume the trailing newline character from the cin >>
The problem is that you are mixing getline with cin >> input.
When you do cin >> age;, that gets the age from the input stream, but it leaves whitespace on the stream. Specifically, it will leave a newline on the input stream, which then gets read by the next getline call as an empty line.
The solution is to only use getline for getting input, and then parsing the line for the information you need.
Or to fix your code, you could do the following eg. (you'll still have to add error checking code yourself) :
cout << "Enter the full name of student: "; // cin name
getline( cin , fullName );
cout << "\nAge: "; // cin age
int age;
{
std::string line;
getline(cin, line);
std::istringstream ss(line);
ss >> age;
}
cout << "\nFather's Name: "; // cin father name
getline( cin , fatherName );
cout << "\nPermanent Address: "; // cin permanent address
getline( cin , permanentAddress );
after the line cin >> age ; there is still the newline character \n (because you pressed enter to input the value) in the input buffer, to fix this you add a line with cin.ignore(); after reading the int.
Why does cin.getline start working for the second line on the body input but break on the first?
Example Program run:
Enter name: Will
Enter body: hello world
hello again <= It accepts this one
char* name = new char[100];
char* body = new char[500];
std::cout << "Enter name: ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.getline(name, 100);
std::cout << "Enter body: ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.getline(body, 500');
std::cin >> body;
As JoshD says, but additionally, you can save a lot of work & pain by using std::string and std::getline from the <string> header.
Like ...
#include <string>
#include <iostream>
int main()
{
using namespace std;
std::string name;
cout << "Enter name: "; getline( cin, name );
}
Cheers & hth.,
– Alf
Because you're ignoring the first line with the cin.ignore statement.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
That will ignore a whole line.
Remove that and you'll get what you want.
You may also want to flush the cout stream to assure your output prints to the screen right away. Add a cout.flush(); before your getline.