Thread: String parameter errors

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    String parameter errors

    I'm trying to make a program that checks if a file is there, if not make it with some writting in it. I've done my best make sure everything is correct.

    Code:
    Code:
    /* Section: header */
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    
    /* Section: constant */
    const std::string NEWLINE = "\n"
    const std::string NEWLINE_FLUSH = "std::endl"
    
    /* Section: prototype */
    void nfound( const std::string &v_file );
    
    int main( int argc, char *argv[] ) {
      using namespace std;
      string the_file = "test_file.txt";
      if ( "the_file" ) {
        cout << "File found!";
      } else {
        cout << "File not found!";
        nfound( "the_file" );
      }
    
      cin.get();
      return 0;
    }
    
    void nfound( const std::string &v_file ) {
      using namespace std;
      ofstream a_file ( "v_file" );
      a_file << "This text will go inside!NEWLINE";
      a_file.close();
    }
    Errors:
    |9|error: expected ',' or ';' before 'const'|
    ||In function 'int main(int, char**)':|
    |21|error: 'nfound' was not declared in this scope|

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You need semi-colons after the definitions of NEWLINE and NEWLINE_FLUSH.

    I suspect, once you fix that, your code will not behave as you expect, as std::string's do not participate in concatenation of string literals. But you can experience that problem for yourself.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    Colon Cancer

    The darn semi-colons! Thanks for pointing that out! Can you explain your comment here:
    Quote Originally Posted by grumpy View Post
    I suspect, once you fix that, your code will not behave as you expect, as std::string's do not participate in concatenation of string literals. But you can experience that problem for yourself.
    I've just began programming and the furthest explaination of strings was C-style vs std::string.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The code
    Code:
       a_file << "This text will go inside!NEWLINE";
    will put the text "NEWLINE" into your output file, whereas I suspect you expect there to be an actual newline.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Wouldn't this code here replace anywhere that I put NEWLINE with \n? Kinda like an alias, that is the intended affect anyway...
    Code:
    const std::string NEWLINE = "\n"

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That is my point. Look at the output file. You will find it does not.

    Aliasing of variable names does not work inside string literals.

    Think about it: would you really want the output of "some_stream << "Hello Peter"; to change if you subsequently modified the code to introduce a variable or macro in your code named Peter?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Oh I see now! So something to the tune of:
    Code:
    a_file << "This text will go inside!" << NEWLINE;

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    What benefit that gives you over
    Code:
    a_file << "This text will go inside!" << "\n";
    is questionable.

    Note that it will not work with your definition of NEWLINE_FLUSH, as std::endl is not a character string.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    The benefit is psychological, it might change later on when I advance through the tutorials and start actually programming. As for NEWLINE_FLUSH, I don't know how to make that one an alias, but I did it to highlight to myself that there is an alternative version to doing a newline.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string as parameter
    By wschuiteman in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 08:27 AM
  2. Replies: 6
    Last Post: 01-08-2008, 10:25 AM
  3. string and function parameter
    By aker_y3k in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2005, 10:11 AM
  4. modify has function from string parameter to templates...
    By rusty0412 in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2005, 08:02 PM
  5. Replies: 2
    Last Post: 05-23-2003, 02:46 PM