#include <iostream>
#include <string>
#include <fstream>
using namespace std;
main ()
{
string empty = "empty";
string line;
string filename;
cout << "enter file name\n";
cin >> filename;
cin >> line;
ofstream myfile;
myfile.open (filename.c_str());
getline (cin,line);
myfile << line;
return 0;
}
2 Replies - 328 Views - Last Post: 08 October 2012 - 07:57 AM
#1
why does getline delete the first word of my string? does c++ hate me?
Posted 08 October 2012 - 05:01 AM
why the hell does getline get the line barring the first word? i tried bodging the problem by trying to pop another word in before it but it just uses that word and then...deletes the first word of the line, i know cin only applies to the first word but thats why i was using getline in the first place...cin on its own gets me just the first word...GRAAAAH *crushes polystyrene coffee cup, slaps wife*
Replies To: why does getline delete the first word of my string? does c++ hate me?
#2
Re: why does getline delete the first word of my string? does c++ hate me?
Posted 08 October 2012 - 05:42 AM
Yes, C++ hates you for beating your wife 
Also, you first input a word in "line" on line 17 using cin, then overwrite it by calling getline, with "line" as the string. So, the first word is stored in "line", then overwritten by getline, and hence you don't see the first word. So, remove the extra cin on line 17.
Also, instead of main(), write main() and return 0. In C++, there is no default return type for functions.
Also, you first input a word in "line" on line 17 using cin, then overwrite it by calling getline, with "line" as the string. So, the first word is stored in "line", then overwritten by getline, and hence you don't see the first word. So, remove the extra cin on line 17.
Also, instead of main(), write main() and return 0. In C++, there is no default return type for functions.
This post has been edited by aresh: 08 October 2012 - 05:43 AM
#3
Re: why does getline delete the first word of my string? does c++ hate me?
Posted 08 October 2012 - 07:57 AM
You should also always check to make sure that the file opened correctly before attempting to write to it.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|