By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 898 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Strings

P: 6
my input is in the following format:

<year><TAB><winner><tab><score><tab><runner-up><tab><score><newline>

where year and scores are all intergers and the Winner and Runner-UP are just strings. The problem is both strings may or may not contain spaces.
I need to write the programming in C++ to intake this data, and then place it into either 4 arrays or 2 parralel two dimensional arrays (year,winner/loser). I'm having trouble reading the input correctly.
Nov 6 '06 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,510
you could use strtok() in <string.h> see
http://www.cplusplus.com/ref/cstring/strtok.html

with the tab character "\t" as the seperator
Nov 6 '06 #2

vninja
P: 40
also you could use the getline cmd. be sure to use the cin.ignore(25, '\n') between the cin.

with your ifstream (i believe srry really tired here), lets say infile, use the getline with the tab delimiter to get the info, followed by a cin.ignore, then infile>> to get the int. getline only works for strings so be careful;

basic code
getline(infile, stringVARIABLEname, delimiter);
cin.ignore(intOFhowMANYcharsTOignore, delimiter);
infile >> variable //will stop at next space
getline(infile, stringVARIABLEname, delimiter);
...
...
..
the cin.ignore or it might be infile.ignore since your not using cin. (again sorry but don't have access to my compiler to check it out right now)
might be after the infile>>variable and before the getline or just how it is now.
the delimiter for the getlines will be a sigle char that separates your data \t( '\t' ) in your case
and for the cin/infile.ignore will just be a \n ( '\n' ).
hope this helps
Nov 6 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.