Oct 2, 2013 at 1:07am UTC
You just did it on line 15, why not try the same thing on line 19?
Oct 2, 2013 at 1:32am UTC
I tried that but it didn't like it and said there was an error. What is the proper way to write it? I was writing:
inputFile >> getline(cin,stockName);
and
inputFile >> (getline(cin,stockName));
and under line 19, but it got all the text, I just wanted the first line
getline(inputFile,stockName);
Oct 2, 2013 at 2:10am UTC
The third version in your post is correct.
Oct 2, 2013 at 2:12am UTC
how do I make it so not the entire text is read, but instead just the first line?
Oct 2, 2013 at 2:15am UTC
That is the default behavior - are you sure it's reading all lines?
Oct 2, 2013 at 2:35am UTC
yup and it doesn't read the first word. here it is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
string fileName;
ifstream inputFile;
string stockName;
cout << "Enter file name: " ;
getline(cin,fileName);
inputFile.open(fileName.c_str());
inputFile >> stockName;
getline(inputFile,stockName);
cout << stockName << endl;
inputFile.close();
return 0;
}
Last edited on Oct 2, 2013 at 2:37am UTC
Oct 2, 2013 at 2:39am UTC
but you said that was the way to do it...
I took line 19 out and now it has the first word :)
Last edited on Oct 2, 2013 at 2:40am UTC
Oct 2, 2013 at 2:39am UTC
Oh, you forgot to remove line 19.
Oct 2, 2013 at 2:43am UTC
ok so my text looks like this:
FedEx Corp (FDX)
112.57 .53 100
113.77 1.5 75
Gamestop Corp (GME)
49.21 2.2 100
49.65 1.45 75
Time Warner Inc (TWX)
65.78 -.4 100
66.2 -.6 75
I just want the first line (FedEx Corp (FDX)) any way to do that instead of the whole thing? or is there a way to add the white spaces in or maybe a way to string the next word with the first?
Last edited on Oct 2, 2013 at 2:45am UTC
Oct 2, 2013 at 2:45am UTC
I tested your code on my machine and it worked correctly - it only grabs the first line. Are you sure nothing's wrong with your input file?
Oct 2, 2013 at 2:47am UTC
well it's just a .txt file as simple as it gets. It looks exactly like how I posted above, so i'm not sure whats wrong :(
Oct 2, 2013 at 3:08am UTC
I don't have notepad, I have textedit. I tried to find how to do what you posted in textedit but couldn't.
Oct 2, 2013 at 3:28am UTC
wow that worked... what is \r anyway?
Oct 2, 2013 at 3:30am UTC
Carriage Return. You probably have a really ancient mac if it uses \r but not \n.
Oct 2, 2013 at 3:31am UTC
it's only 3 years old
and I do use \n
The more I learn the more I don't like macs...
Last edited on Oct 2, 2013 at 3:33am UTC