c++ char name;

This is a discussion on c++ char name; within the C++ Programming forums, part of the General Programming Boards category; how do I set it that I am able to enter more than one char ex:hello, right now all I ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    8

    Unhappy c++ char name;

    how do I set it that I am able to enter more than one char ex:hello, right now all I can enter is ex: a, or b ect...

    I just started programming.

  2. #2
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    Post code showing how you are reading-in single characters and then I can show you how to read in strings.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    8
    thanks for the quick response I'm not used to that.

    here is the code

    Code:
    # include <iostream.h>
    int main ()
    {
    int number;
    char letter;
    cout <<"please enter one letter."
    cin >> letter;
    cout << "please enter your age."
    cin >> number;
    cout << "this is the name you have entered." << letter;
    cout << this is the age you have entered." << number;
    return 0;
    }


    I was told to include <string.h> what is that supposed to do?

  4. #4
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    string letter
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    8
    will that solve my problem?

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    char name[50];

    cout<<"Enter your name: ";

    cin.get(name, 50);

  7. #7
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    Or

    char name[50];

    cout << "Enter a name:";
    cin >> name;
    cout << "You entered:" << name << endl;

  8. #8
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    You should learn about strings, either as an array of char's or using the string library <cstring>
    none...

  9. #9
    Neoseeker's master master2000's Avatar
    Join Date
    Dec 2002
    Posts
    101

    Lightbulb

    you can also do

    scanf("%s",&name)
    with char[30]


    [30]-can be changed to any number but that might cause a glich when you enter more than your number in [].hope it helps
    Last edited by master2000; 12-15-2002 at 09:15 AM.

  10. #10
    Registered User
    Join Date
    Dec 2002
    Posts
    8

    Question

    is this c++
    Code:
     char name;
               getline name;
    and this regular c
    Code:
     
            char name[50];
         cin name;
    ?????????????????????????????????????????????

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Wrong. cin is an istream object. In other words it is a c++ class. Since this is C++ you should use a string. However, if speed and memory consumption is an issue (i doubt that it is here) then use a array. You can't hold a string in a single char. A char only holds 1 byte of data (one letter).

  12. #12
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by ammar
    You should learn about strings, either as an array of char's or using the string library <cstring>
    No, the string type is located in <string>
    <cstring> is functionaly equivalent to <string.h>

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I think ammar did mean cstring...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 08:36 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 10:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM

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