Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Stupid Basic C++ Question

Stupid Basic C++ Question
Thread Tools
Mac Enthusiast
Join Date: Nov 2002
Location: over yonder
Status: Offline
Reply With Quote
Mar 31, 2003, 10:38 PM
 
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
Reply With Quote
Apr 1, 2003, 04:42 PM
 
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
Reply With Quote
Apr 1, 2003, 05:00 PM
 
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
Reply With Quote
Apr 1, 2003, 07:21 PM
 
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
Reply With Quote
Apr 2, 2003, 12:57 AM
 
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
Reply With Quote
Apr 2, 2003, 01:45 AM
 
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
Reply With Quote
Apr 2, 2003, 01:50 AM
 
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 Filen";

//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
Reply With Quote
Apr 3, 2003, 01:18 AM
 
his code was just missing a readline
     
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status: Offline
Reply With Quote
Apr 3, 2003, 01:01 PM
 
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
Reply With Quote
Apr 3, 2003, 01:48 PM
 
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
Reply With Quote
Apr 4, 2003, 08:24 PM
 
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
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:20 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2