C++ Help

Discuss any other topics here
Post Reply
MakerOfGames
Posts: 866
Joined: Fri Jul 08, 2005 4:27 am
Location: PA, USA

C++ Help

Post by MakerOfGames »

I am working on a coding project for my c++ class, but I just can't get this right. I need to read a text file which data is seperated by commas until the end of the line there is a new line and a repeat of data.
So, how can I do a double disect of the data stream?

Code: Select all

in.getline(temp, 200, '\n');
That gets me the whole line to a temp string variable, but how do I then look at the string and pick out the data seperated by commas?
Ex data set:
23,53,52,543,63,12,2,523,98
32,53,43,43,65,3,646,2,34
-all rows have the same number of sets of data, its just the length of the individual pieces of data in the string that vary.

Any help would be greatly appreciated!
Think outside the box.
To go on an adventure, one must discard the comforts and safety of the known and trusted.
Allanon
Posts: 493
Joined: Mon Aug 29, 2005 8:23 am

Re: C++ Help

Post by Allanon »

Try using the strtok function.
MakerOfGames
Posts: 866
Joined: Fri Jul 08, 2005 4:27 am
Location: PA, USA

Re: C++ Help

Post by MakerOfGames »

Thanks. I think that will help me out.
Think outside the box.
To go on an adventure, one must discard the comforts and safety of the known and trusted.
User avatar
paradoxnj
RF2 Dev Team
Posts: 1328
Joined: Wed Mar 01, 2006 7:37 pm
Location: Brick, NJ
Contact:

Re: C++ Help

Post by paradoxnj »

You can also use the Standard Template Library's stringtokenizer lib or Boost::Spirit.
Many Bothans died to bring you this signature....
MakerOfGames
Posts: 866
Joined: Fri Jul 08, 2005 4:27 am
Location: PA, USA

Re: C++ Help

Post by MakerOfGames »

paradoxnj wrote:You can also use the Standard Template Library's stringtokenizer lib or Boost::Spirit.
I will look that up. I have only had 2 C++ classes to date. Much of how my course is oriented is that more Java will carry over, so very basic input output was covered so far in C++, which its actually A LOT different from Java. Hopefully my next class will clear things up for me, along with looking that up and more independent research :roll:. Thanks for the help.
Think outside the box.
To go on an adventure, one must discard the comforts and safety of the known and trusted.
User avatar
SithMaster
Posts: 306
Joined: Mon Jul 04, 2005 11:45 pm

Re: C++ Help

Post by SithMaster »

I should know this. I just took a semester of C++ and i really cant remember how to do this one. When is it due? I can try and go over my notes and figure it out based on labs ive done. Or i can just send you my code files so you can try and decipher my code.
The brightest light casts the darkest shadow.
Burn the heretic. Kill the mutant. Purge the unclean.
MakerOfGames
Posts: 866
Joined: Fri Jul 08, 2005 4:27 am
Location: PA, USA

Re: C++ Help

Post by MakerOfGames »

I have until Monday to get this done. I am going to give it another shot tomorrow, because tonight I am out of time(I have other work thats due by Friday that needs done first). I might be able to get this on my own with the help already posted. Thank you very much though. I will let you know if I still can't get this down and then we can go from there. Thanks again!
Think outside the box.
To go on an adventure, one must discard the comforts and safety of the known and trusted.
Allanon
Posts: 493
Joined: Mon Aug 29, 2005 8:23 am

Re: C++ Help

Post by Allanon »

A little help

Code: Select all

char *Result = NULL;

in.getline(temp, 200, '\n');

Result = strtok( temp, ",");  // Get the first number
while (Result)  // loop thru all numbers in string
{
    int num = atoi(Result); // convert string to an integer
    printf("%i",num);        // print the integer
    Result = strtok(NULL,",");  // Get the next number 
}

MakerOfGames
Posts: 866
Joined: Fri Jul 08, 2005 4:27 am
Location: PA, USA

Re: C++ Help

Post by MakerOfGames »

Thanks! That will help a lot!
Think outside the box.
To go on an adventure, one must discard the comforts and safety of the known and trusted.
Post Reply