Thread: variable "char" problem

  1. #1
    C++ Master
    Join Date
    Oct 2005
    Location
    ProgramLand
    Posts
    15

    variable "char" problem

    I cant get this to work:


    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      char var_name;
    
      cout<<"Please enter in your name...\n";
      cin>> var_name;
      cin.ignore();
      cout<<"Your name is "<< var_name <<"\n";
      cin.get();
    }
    it compiles without any errors but when i enter in a name it bails. if i sent in one character it'll work correctly. How do i get it so that it could with with multiple characters too?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Declare var_name to accept multiple characters.
    Code:
    char var_name[100];
    Next question: how do I get it to accept text with spaces in between? Use getline.

    And this.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    A char type can only store one char, so use a "string" type instead, which can store an unlimited number of chars:
    Code:
    #include <string>
    ...
    ...
    string name;
    cin>>name;
    Last edited by 7stud; 12-03-2005 at 11:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. problem with variable passed as reference
    By crash88 in forum C Programming
    Replies: 1
    Last Post: 05-16-2006, 07:16 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. problem with the const variable
    By ssharish in forum C Programming
    Replies: 2
    Last Post: 01-28-2005, 09:53 AM
  5. Peculiar Problem with char variable in C Language
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-31-2001, 04:06 PM