Strings

This is a discussion on Strings within the C++ Programming forums, part of the General Programming Boards category; I am trying to assign the array of char s the string read from a file, but I get "initializer ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    34

    Strings

    I am trying to assign the array of char s the string read from a file, but I get "initializer fails to determine size of s", "invalid initializer" I found an earlier post on the forum where someone suggested a loop to assign the char array the string one character at a time but that did not work for me. If I manually give it a string like "Hello World" however, it works fine. Any suggestions? Thanks.

    Code:
    while(INPUTFILE && INPUTFILE.peek() != EOF)
     {
      getline(INPUTFILE, theTerms);
      
      for(int i=0; i < theTerms.length(); ++i)
      {
       char s[] = theTerms ;
      } 
    
     ...

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    try the strcpy( ) function.. it should copy the string to the char array all at once.

    Code:
    while(INPUTFILE && INPUTFILE.peek() != EOF)
    {
      getline(INPUTFILE, theTerms);
      strcpy(s, theTerms.c_str());
    }
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Why do you need a character array in the first place? The string class should meet all your needs.

    If for some reason you really do need a C style string, there is a version of getline that works for those.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    34
    Thanks, it works to copy the string from theTerms to s, but will it also work with the strtok() function, which is the primary reason why I am trying to do this?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    There are alternatives for tokenizing a C++ string. Which one is best depends on what tokenizing you're going to do.

    If you stick with strtok, then you might as well use cin.getline. Don't forget that if you use a C style string you need to allocate space for it. You shouldn't copy into s unless s is declared to have enough characters to hold the line.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    34
    I am now trying to use a sstream object instead to tokenize the string since the input file is delimited by whitespace. I am putting the tokens into a buffer and then pushing them onto a vector of strings. But I am having trouble figuring out how to get only one line of the file into the vector at a time. Thanks again for any suggestions.

    Code:
    while(INPUTFILE && INPUTFILE.peek() != EOF)
     {
      getline(INPUTFILE, theTerms);
      
      stringstream ss(theTerms); // Insert the string into a stream
      vector<string> tokens; 
      string buffer; // buffer string for theTerms
      
      while (ss >> buffer)
      {
       tokens.push_back(buffer);
      }
    }

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You seem to be getting only one line at a time. Do you want the data to be available outside the loop?

    If you only want a vector of tokens from the file (don't care where the lines end):
    Code:
    std::vector<std::string> tokens;
    std::ifstream fin(...);
    std::string input;
    while (fin >> input) {
        tokens.push_back(input);
    }
    If you care about where each line ends, you might use a vector of vectors:
    Code:
    std::vector<std::vector<std::string> > tokenLines;
    ...
    while (std::getline(fin, line)) { //preferable way of reading lines
         //tokenise
         //push resulting vector<string> onto tokenLines
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    34
    I have spent some time reading about string stream but I have not been able to figure out how to use it to remove internal whitespace within a string. Can it indeed remove internal whitespace? Thanks.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Yes. You could put the string into the stream, then extract strings out with operator>> and append those together. The operator>> skips whitespace whether you use it with cin or a stringstream, so that could work.

    I'm a little confused about what you're doing though. Can you give a small example of a couple lines of input and how you want to store it?

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    34
    Well this is just the file processing part of a larger program. I have gone through several different options for extracting data from the input file and have settled with using string streams since they seem to be more efficient.

    So the input file would have lines like the following, where there could be any amount of whitespace within the terms or outside each line of the polynomials represented by:

    2 : 3 1 2 1 4 2

    4 : 5 2
    2 = 2 * 4
    display 2

    Where the first integer followed by a colon denotes the location of the poly and the rest denote coefficient and exponent terms.

    But I think that I found the cause of my problems. I read somewhere that stringstream can do both input and output. But when I changed it to an istringstream, things started to work as I intended them to work.
    Last edited by Bnchs400; 10-01-2007 at 07:48 AM.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    34
    I have a question about istringstream. My inputfile has a line like:

    show 2

    and I have a string s and I assign it the string "show". I also have an int poly.

    shouldn't this statement work?

    Code:
    if(iss >> show >> poly) 
    {
      //do something
    }
    Last edited by Bnchs400; 10-03-2007 at 10:09 AM.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It ought to read the string into the variable show (which would then have the value "show") and the integer into poly (which would then have the value 2).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    34
    I am confused now because I have a line like:

    2 : 3 2 1 0

    and a char object called colon assigned with ':'

    when I test the following block of code, everything works as I think and intend it to work.

    Code:
    if (iss >> poly >> colon)
    {
      aInt = poly;
      
      while(iss >> coeff >> exp)
      { 
        ....
    So is it is possible to check if an istringstream reads something in particular, as I am attempting to do? or, I am doing it incorrectly? Because it apparently works in one case and doesn't in another.
    Last edited by Bnchs400; 10-03-2007 at 12:02 PM.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Well, what does it do in the case where it doesn't work, and what do you expect it to do?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    34
    Quote Originally Posted by CornedBee View Post
    Well, what does it do in the case where it doesn't work, and what do you expect it to do?
    Assuming the line is :

    show 0

    I expect the program to enter this block because the istringstream did indeed contain a line containing show and some integer. But it does not enter that block of code.

    Code:
    if(iss >> show >> poly) 
    {
      //do something
    }
    This is one of the blocks inside a while loop that assigns one line of the file to the istringstream. I have ruled out a problem with the while loop or the assignment of the line to the istringstream because I checked to see what istringstream contained each time around.

    Here is the loop:
    Code:
     int poly, argument, coefficient, exponent;
     char colon = ':';
     char assignment = '=';
     char multiply = '*';
    
     while(INPUTFILE && INPUTFILE.peek() != EOF)
     {
       getline(INPUTFILE, theTerms);
       iss.str(theTerms);
         
    if(iss >> poly >> colon)
    {
     locationOfPoly = poly;                   
       
       while(iss >> coefficient >> exponent)
       {
        tPoly.insert(exponent, coefficient);
       }
    
       //postcondition: all info has been extracted from iss so assign tmpPoly
       //to Poly[locationOfPoly] 
       Poly[locationOfPoly] = tPoly; 
    }  
      
        
      if(iss >> poly >> assignment >> poly >> multiply >> poly)
       {
        Poly[poly] = Poly[poly] * Poly[poly];
       }
       
       if(iss >> show >> poly)
       {         
        locationOfPoly = poly;
        cout <<"Poly[" << locationOfPoly << "] = " << Poly[locationOfPoly] << "\n";
       }
     
     }//end while INPUTFILE loop
    Last edited by Bnchs400; 10-03-2007 at 12:25 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 03:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 12:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21