Strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jetboy555
    New Member
    • Nov 2006
    • 6

    Strings

    my input is in the following format:

    <year><TAB><win ner><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.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you could use strtok() in <string.h> see
    http://www.cplusplus.c om/ref/cstring/strtok.html

    with the tab character "\t" as the seperator

    Comment

    • vninja
      New Member
      • Oct 2006
      • 40

      #3
      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, stringVARIABLEn ame, delimiter);
      cin.ignore(intO FhowMANYcharsTO ignore, delimiter);
      infile >> variable //will stop at next space
      getline(infile, stringVARIABLEn ame, 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>>variabl e 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

      Comment

      Working...