Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 05-03-2007, 05:51 AM   #1
Beta Member
 
Join Date: May 2007
Posts: 5
Default Why Does This While Loop...

Just by looking, hopefully this can be resolved, because i have about 4 different user-defined classes that i would need to post up if need be. please help, I need to know why this while loop only passes once:

while (getline (cast, name, '\n'))
{
cast >> age;
cast >> junk;
Person* newCharacter = new Person (name, age);
fromFile.push_back(newCharacter);

}


age is int.
junk is string.
name is string.
cast is fstream (input).
fromfile is a vector <*Person> (person is a user defined class).
newCharacter is a Person.


please help. Again, I will post the classes if needed.


edit: oh yes, this is in C++, everyone.
__________________

ToneZ is offline   Reply With Quote
Old 05-03-2007, 07:20 AM   #2
Daemon Poster
 
RewtGuy's Avatar
 
Join Date: Dec 2004
Posts: 595
Send a message via AIM to RewtGuy
Default Re: Why Does This While Loop...

The while loop
Its format is:

while (expression) statement

and its functionality is simply to repeat statement while the condition set in expression is true.

It stops after one cycle because "getline (cast,name, '\n'))" becomes untrue. Usually you see integers as the expression, not functions.
__________________

__________________
Windows: A thirty-two bit extension and GUI shell to a sixteen bit patch to an eight bit operating system originally coded for a four bit microprocessor and sold by a two-bit company that can't stand one bit of competition.
RewtGuy is offline   Reply With Quote
Old 05-03-2007, 07:31 AM   #3
Beta Member
 
Join Date: May 2007
Posts: 5
Default Re: Why Does This While Loop...

alright, the text file looks like this...


1st Name
1st Age
2nd Name
2nd Age


the "while" condition in the parens gets the first name, then goes down and gets the first age. then takes the white space after the input operator and stores that to junk. then takes the first name and age and puts that to a new Person pointer and puts that on the backend of a Vector. The while isn't false rite now because it is here:


First Name
First Age
Second Name <~~ HERE
Second Age


although it is there (or should be there), it ends the loop automatically. The while condition isn't false because there is more text document to input as the condition is true, but it automatically stops. That's my problem, can anyone see an error?
ToneZ is offline   Reply With Quote
Old 05-05-2007, 04:03 PM   #4
Beta Member
 
Join Date: Jan 2007
Posts: 1
Default Re: Why Does This While Loop...

Quote:
Originally Posted by ToneZ View Post
Just by looking, hopefully this can be resolved, because i have about 4 different user-defined classes that i would need to post up if need be. please help, I need to know why this while loop only passes once:

Code:
while (getline (cast, name, '\n'))
        {
                cast >> age;
                cast >> junk;
                Person* newCharacter = new Person (name, age);
                fromFile.push_back(newCharacter);

        }
age is int.
junk is string.
name is string.
cast is fstream (input).
fromfile is a vector <*Person> (person is a user defined class).
newCharacter is a Person.


please help. Again, I will post the classes if needed.


edit: oh yes, this is in C++, everyone.
Try adding
Code:
cast.ignore();
here,
Code:
while (getline (cast, name, '\n'))
        {
                cast >> age;
                cast >> junk;
                Person* newCharacter = new Person (name, age);
                fromFile.push_back(newCharacter);

                cast.ignore();//< -- here

        }
If that helps then use your favorite search engine with the phrase "mixing cin and getline". Then post back with any questions.
Megabytes is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting 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 Off



All times are GMT -5. The time now is 01:16 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0