why this program dont work?

This is a discussion on why this program dont work? within the C++ Programming forums, part of the General Programming Boards category; Code: ofstream out; string dado; char q[18]; cout<<"Digite o nome da pessoa que voce quer adicionar:\n"; cin.getline( q,[18](q) ); out.open( ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220

    why this program dont work?

    Code:
                             ofstream out;
        string dado;
        char q[18];
        cout<<"Digite o nome da pessoa que voce quer adicionar:\n";
        cin.getline( q,[18](q) );
        out.open( q,ios::app );
        cout<<"Digite os dados que voce queira adicionar:\n";
        getline(cin,dado);
        cin.ignore();
        out<< dado <<"\n";

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Because it doesn't have a main() function.

    This line
    Code:
    cin.getline( q,[18](q) );
    is wrong.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    cin.getline( q,[18](q) );
    That is why

  4. #4
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    so what i have to place instead of that line?

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Maybe something that is syntactically correct?

    http://www.cppreference.com/cppio/getline.html

  6. #6
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220

    i cant make this work

    Code:
    ofstream out;
        string nome;
        char q[18];
        cout<<"Digite o nome da pessoa que voce quer adicionar:\n";
        cout<<"O nome em letras minusculas e nome e sobrenome separadas por um ponto!\n";
        getline(cin,q);
        out.open( q,ios::app );
        cout<<"Digite os dados que voce queira adicionar:\n";
        cout<<"Lembre-se sempre usando espacos como se fosse um ponto: \n";
        getline(cin,nome);
        cin.ignore();
        out<< nome <<"\n";
    whats wrong with this and what should i do to correct it?

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Threads merged. In the future please keep all posts related to the same problem in the same thread.

    Ok first thing is you should post an errors you recieve. They are a lot more helpful then a bunch of code with prompts in a language outside the board's standard language.

    However the problem is that you are switching styles of getline() without actually looking at what they require.

    Code:
    getline(cin,nome);
    This line is fine, the first parameter expects an istream and the second parameter expects a std::string, both of which you provide.

    Code:
    getline(cin,q);
    Here you are providing an array of char instead of a std::string.

    You want the getline I linked to you which wants:
    a pointer to a char and the size of the buffer, and optionially a delimiting character. So it should look like:
    Code:
    cin.getline(q, 18);
    or maybe even
    Code:
    cin.getline(q, sizeof q/sizeof q[0]);
    if you are fimilar with the details of sizeof()

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I also strongly and highly suggest that you read the forum guildlines found at:
    http://cboard.cprogramming.com/annou...ouncementid=51

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. threaded program doesn't work?
    By OcTO_VIII in forum Linux Programming
    Replies: 1
    Last Post: 12-11-2003, 12:37 PM
  4. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 11:05 AM
  5. how does this program work
    By ssjnamek in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2002, 01:48 PM

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