char variable type

This is a discussion on char variable type within the C++ Programming forums, part of the General Programming Boards category; Hello! I'm writing a program right now that takes your name and some others names and puts them in a ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    42

    char variable type

    Hello!
    I'm writing a program right now that takes your name and some others names and puts them in a story that makes fun of you...

    anyway,
    Code:
    .......
    char name;
    ........
    cout << "Please enter your first name:" << endl;
    cin >> name;
    .........
    ........
    ........
    When I enter a name, it only takes the first letter of it, how can I make it take the whole name ?

    thanks!
    I have a code which stops life!

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    char is only a 1-character data type.

    You can use std::string from <string> (recommended):

    std::string name;
    cin >> name;

    or you can use character arrays:

    char name[32];
    cin.get(name, 32); // Prevents buffer overflow.
    cin.sync();
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    42

    Thumbs up ok!

    Thanks man, you've been helpful!
    I have a code which stops life!

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    148
    >>cin.get(name, 32); // Prevents buffer overflow.
    >>cin.sync();
    A word on sync with istream.
    I never get it to work correct with the cygwin port of gcc,so it seems not to be portable.
    And this thread on comp.lang.c++ agrees

    The sole purpose of 'sync()' is to bring the
    internal and the external view of a stream into synchronization. Typically
    this means to flush an output buffer. Often, this does not touch an input
    buffer at all although a stream buffer might bring the internal input
    buffer into synchronization with the external view if this has changed,
    eg. due to another program modifying a file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 05:17 AM

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