 |
 |
Stupid Basic C++ Question
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2002
Location: over yonder
Status:
Offline
|
|
I can't figure this out...and it's really frustrating that this damn thing won't work.
Text file:
65 42 19
Hello World
Program code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
int var1, var2, var3;
string inputString;
ifstream inStream;
inStream.open("test.txt");
inStream >> var1 >> var2 >> var3;
getline(inStream, inputString);
cout << var1 << endl;
cout << var2 << endl;
cout << var3 << endl;
cout << inputString;
return 0;
}
The program will read in the 3 numbers and ouput them correctly, but won't take in the string. Why the hell not!?!?!?!
Any help would be GREATLY appreciated...I think I'm having a major brain fart and it's driving me nuts!
|
|
chown -R us:us yourbase
Dissent is not un-American.
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Aug 2000
Location: Haltom City, TX
Status:
Offline
|
|
where is your getline getting the line from? the text file?
if so, you may need to change your syntax to something like
inStream.getline(inputString, "/n");
that way it knows to look for "Hello World" in the input file and not from the console
- Taz
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2002
Location: over yonder
Status:
Offline
|
|
The syntax you're suggesting is for use with C-Strings. With a C++ String, the getline() function takes arguments like getline(stream, variable). I specified the filestream.
|
|
chown -R us:us yourbase
Dissent is not un-American.
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status:
Offline
|
|
Caveat: I'm very much a C++ newbie also.
But I have learned that it's very often hazardous to mix this:
stream >> var;
with this:
getline(stream,var2);
because the stream >> operator *ignores* whitespace, while getline() accepts it. That often means that a trailing newline or whitespace is left on the stream after the >> operation.
Two suggestions:
1. issue a stream.clear() and/or stream.ignore() to flush out any leftover bits.
2. Do the whole thing with getline(), and use a stringstream to convert strings to ints.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: May 2001
Location: Up north
Status:
Offline
|
|
I haven't coded in C++ for many months.
Um, shouldn't you do a get line after you get those integers, and then do a getLine for the Hello World? ie, because you have to advance the current file position past that end of line on the integer line, else it will read whatever is inbetween the last integer and the end of line, which is nothing or "".
---------------------------------
This is a working version of your program
---------------------------------
Text file:
65 42 19
Hello World
Program code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
int var1, var2, var3;
string inputString;
string garbage;
ifstream inStream;
inStream.open("test.txt");
inStream >> var1 >> var2 >> var3;
// go to end of line
getline(inStream, garbage); // you can us inputString here because it is set again later, but for the sake of clarity I didn't
// read line
getline(inStream, inputString);
cout << var1 << endl;
cout << var2 << endl;
cout << var3 << endl;
cout << inputString;
return 0;
}
(Last edited by 11011001; Apr 3, 2003 at 01:14 AM.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Aug 2000
Location: Haltom City, TX
Status:
Offline
|
|
i've never coded in regular C... ever.
what i suggested is common C++
are you sure your parameters are correct for the getline?
parm1 = where to store data
parm2 = how much data
parm3(optional) = i think you can also specify a delimiter, otherwise by default its "\n"
- Taz
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Feb 2003
Location: Portland, Oregon
Status:
Offline
|
|
Originally posted by fromthecloud:
I can't figure this out...and it's really frustrating that this damn thing won't work.
Text file:
65 42 19
Hello World
Program code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
int var1, var2, var3;
string inputString;
ifstream inStream;
inStream.open("test.txt");
inStream >> var1 >> var2 >> var3;
getline(inStream, inputString);
cout << var1 << endl;
cout << var2 << endl;
cout << var3 << endl;
cout << inputString;
return 0;
}
The program will read in the 3 numbers and ouput them correctly, but won't take in the string. Why the hell not!?!?!?!
Any help would be GREATLY appreciated...I think I'm having a major brain fart and it's driving me nuts!
#include <iostream.h>
#include <fstream.h>
void Get();
int main()
{
Get();
return 0;
}
void Get()
{
//Character arrays. Strings are retarded.
char x[100];
ifstream inFile; //File variable
//Open her up
inFile.open("test.txt");
//Check for file
if (inFile){
//Get each character while not end-of-file
while ( !(inFile == EOF) ){
inFile.get(x, 100); //Build character array
}
}
//If file doesn't open than error out
else
cout << "No File  n";
//Print contents of character array
cout << x;
}
There's a quick and dirty. I haven't compiled it or anything.
jesse ;-)
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: May 2001
Location: Up north
Status:
Offline
|
|
his code was just missing a readline
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status:
Offline
|
|
The amount of disagreement over a simple question like this has made me yet again appreciate what a wonderful language C++ is
Anyway, yeah, I think he's just missing a readline... Honestly I guess that would be a problem in any language, can't just forget about those newline characters.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status:
Offline
|
|
Originally posted by itai195:
The amount of disagreement over a simple question like this has made me yet again appreciate what a wonderful language C++ is
Precisely why I don't write in C or C++ anymore. Mmmm, java.
|
|
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Dec 2002
Status:
Offline
|
|
It was a simple question. I think its nice that people that are still learning it can help each other like that. Just because they don't know *everything* about C++ doesn't make it a bad language. Java is nice, as well.
|
|
Travis Sanderson
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|