Page 3 of 4 FirstFirst 1234 LastLast
Results 21 to 30 of 32

Thread: C++

  1. #21
    Senior Member
    Join Date
    May 2002
    Posts
    344

    c++

    Not quite... but close. the userName[21] creates a username with up to 21 characters (which are located in positions 0-20 of the character array) Declaring a variable as just a char only allows a character, but the [] make the variable into an array of characters instead. Also, if you want, you can include string.h and use the string variable type instead. This header file will also give you more string manipulation functions/procedures.
    so you mean if my program looks like this it wil work:

    #include <iostream>
    using namespace std;

    int main void
    {
    char userName[100]//this allows the user to type a 100 character name

    cout << "insert your name" << end;
    cin >> userName

    cout << "hello" <<userName<< "you are very cool"
    }
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  2. #22
    Banned
    Join Date
    Oct 2001
    Posts
    263
    yes but the part thats after the // is a one line comment and dosnt need to be there really so you dont need to update it to be 100 characters......

  3. #23
    Junior Member
    Join Date
    Jul 2002
    Posts
    10
    For the trouble it causes, either keep comments up to date or get rid of them

    Otherwise, people just end up ignoring them because they're too cynical to think they'll be of any use.

    PS: White_Eskimo: You're still limiting the user to 100 characters, and a name with no spaces in. If I entered "White Eskimo", then it would take the name as "White".
    Byte Me

  4. #24
    Banned
    Join Date
    Oct 2001
    Posts
    263
    ok this string that youve declared........ how long can it be...... is it unlimeted length like im hoping? and it can take any input type? if these are right i think im in love with them then

  5. #25
    Junior Member
    Join Date
    Jul 2002
    Posts
    10
    Probably no limits (on a 32-bit compiler) up to around a couple of gigs, might hit problems with more than a few tens of megabytes due to memory fragmentation - after about a megabyte you're better off with a rope class (basically they split a string into lots of little ones that the memory system can handle).

    What do you mean by "take any input type"?
    Byte Me

  6. #26
    The Iceman Cometh
    Join Date
    Aug 2001
    Posts
    1,209
    and it can take any input type?
    I assume you're refferring to whether it can be a different variable type (such as an integer, etc.). The answer is no. Strings will be string's of characters. Granted, you can enter numbers, but they will be characters of the numbers, not the integer values. You will have to change it to an integer later if you need to do integer manipulations.

    If you wish to have them of any type, you can create your own class which will allow you to declare an array dynamically or a linked-list which will allow you to create an array of any length of any type.

    AJ

  7. #27
    Senior Member
    Join Date
    Aug 2001
    Posts
    356

    Re: c++

    Originally posted here by White_Eskimo

    #include &lt;iostream&gt;
    using namespace std;

    int main void
    {
    char userName[100]//this allows the user to type a 100 character name

    cout << &quot;insert your name&quot; << end;
    cin >> userName

    cout << &quot;hello&quot; << userName << &quot;you are very cool&quot;
    }
    that will work except for the lack of semi-colons, and you would want to use a different input method. it should look like this:
    #include &lt;iostream&gt;
    using namespace std;

    #define MAXNAMELENGTH 100

    int main void
    {
    char userName[MAXNAMELENGTH];//this allows the user to type a 99 character name --1 char is needed for the terminating NULL char.

    cout << &quot;insert your name&quot; &lt;&lt; endl;//endl, not end
    cin.getline(userName, MAXNAMELENGTH); //you wont want return chars in a name, and this will stop a buffer overflow because it will only read upto 100 chars.
    userName[MAXNAMELENGTH-1] = NULL; //make sure there is a nullchar at the end.


    cout << &quot;hello&quot; <<userName<< &quot;you are very cool&quot;;
    }


    Originally posted here by LoggOff
    yes but the part thats after the // is a one line comment and dosnt need to be there really so you dont need to update it to be 100 characters......
    well comments are almost always a good thing. saying they are un-needed just starts people off not using them and gives headaches to anybody who winds up having to read and modify their code.
    -8-

    There are 10 types of people in this world: those who understand binary, and those who dont.

  8. #28
    Banned
    Join Date
    Oct 2001
    Posts
    263
    /me falls in love with string...... if only i could figure out how to use them......?

  9. #29
    The Iceman Cometh
    Join Date
    Aug 2001
    Posts
    1,209
    Check out the AO FAQ for a bunch of links to help you use C++ and strings.

    AJ

  10. #30
    Banned
    Join Date
    Oct 2001
    Posts
    263
    good idea, thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •