Newbie Question (easy i think)

This is a discussion on Newbie Question (easy i think) within the C++ Programming forums, part of the General Programming Boards category; HI, i've posted only once before, but i have a question. I would like to know if there is a ...

  1. #1
    Registered User pyrotherm's Avatar
    Join Date
    Sep 2003
    Posts
    14

    Newbie Question (easy i think)

    HI, i've posted only once before, but i have a question. I would like to know if there is a way in C++ to enter a string of text as a variable as in var="your name", that's it

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    like:
    Code:
    string var = "your name";
    or:
    Code:
    char var[] = "your name";
    ?

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    and
    char *str = "my string";

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Well, don't use the last syntax you were given, it is better to use an array than a pointer to declare a string.

  5. #5
    Registered User pyrotherm's Avatar
    Join Date
    Sep 2003
    Posts
    14
    ok, what i want to do is to have the user imput thier name, and it outputs it in all the right places, so that i could make my program more personalized. I want the value to be like a float or int,where the user could imput (cin) thier name, and i twould output the value later on.

    thx. for your time,
    Pyrotherm

  6. #6
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Then, if you are using C++, use string.

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by Lynux-Penguin
    and
    char *str = "my string";

    -LC
    Or, much more correctly:

    const char *str = "my string";

    It's bad karma to take advantage of the single instance of an implicit conversion from const to non-const.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    22

    try this

    when you accept the value from the user use any of the following

    Code:
    cin.get(aString, 50 ) // accept a string up to 50 or '\n'
    cin.get(astring,50,'$') // accept to 40 or '$' is found
    this will allow for any white space between the first and last name

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 11:13 AM
  2. Another Embarassingly Easy Question
    By almo89 in forum C Programming
    Replies: 2
    Last Post: 02-11-2006, 03:59 PM
  3. very newbie question: how to copy files
    By webwesen in forum C Programming
    Replies: 26
    Last Post: 04-25-2002, 03:01 PM
  4. Newbie question about registries/files
    By SirDavidGuy in forum C++ Programming
    Replies: 1
    Last Post: 02-20-2002, 08:58 PM
  5. newbie question, should be easy
    By keeb in forum C++ Programming
    Replies: 3
    Last Post: 09-14-2001, 05:29 PM

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