Thread: cin.getline?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    replace
    Code:
    char filename[80];/
    by
    Code:
    string filename;

    Code:
    ifstream fin(filename);
    by
    Code:
    ifstream fin(filename.c_str());

    Code:
    ofstream fout(filename,ios::trunc);
    by
    Code:
    ofstream fout(filename.c_str(),ios::trunc);

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    Quote Originally Posted by h_howee
    replace
    Code:
    char filename[80];/
    by
    Code:
    string filename;

    Code:
    ifstream fin(filename);
    by
    Code:
    ifstream fin(filename.c_str());

    Code:
    ofstream fout(filename,ios::trunc);
    by
    Code:
    ofstream fout(filename.c_str(),ios::trunc);
    no idea what that is...
    i dont think i covered any of that in my book.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    char filename[80] is a C-string

    #include <string>
    string abc;

    is a C++ string, this is a class it handles for you memory allocations (you need not to worry about the unsuffitient buffer size) and provide some useful interface like
    if(abc == "Hello, World!")

    So when you are programming in C++ there is no reason not to use it...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> dont think i covered any of that in my book.
    Many C++ books don't teach C++ strings in the beginning (primarily because they were introduced more recently than C style strings). You should be learning C++ strings first, so either switch books or supplement your book with looking things up online.

    BTW, when using getline with a C++ string, there is a slightly different format:
    Code:
    getline(cin, filename);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.getline help
    By Cool Dude 2k in forum C Programming
    Replies: 2
    Last Post: 07-27-2005, 06:55 PM
  2. cin.getline and msgrcv
    By osal in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2005, 12:01 PM
  3. difference between getline and cin.getline
    By bartinla in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 09:47 AM
  4. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 05:53 PM