Variable question I can't find answer to

This is a discussion on Variable question I can't find answer to within the C++ Programming forums, part of the General Programming Boards category; Hello, I am sure this is simple, It must be too late, I am going through tutorials, I simply cannot ...

  1. #1
    joelmon
    Guest

    Variable question I can't find answer to

    Hello, I am sure this is simple, It must be too late, I am going through tutorials, I simply cannot find how to define a variable that is text?

    is it char?

    char[100] for example?

    I know int is for integer, all examples I see have integer variables, I simply want to do the basic

    "hello, your name is (variable)" where
    (variable) is data from input from user

    I just can't find how to define a text variable

    I know, silly, I can do if/loops with int variables, but I can't find text variables, ok, being laughing

    Ps. It's 5 am, my apologies if I missed this in the faq, it is not to be rude, I really am digging to find this, not asking for code to be written for me (gulp)

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> char[100] for example?

    That is one way. In C/C++ there is a category of strings, (text), called "Null terminated strings". These are basically arrays of characters with a NULL character to mark the end of the string. Your array above could hold a text string 99 characters long, (the hundreth character would be the NULL to terminate it).

    Code:
    #include <stdio.h> 
    int main() 
    {
    	char abc[6]; 
    	sprintf(abc,"Hello");
    	printf("%s\n",abc);
    	return 0;
    }
    There are other types of string as well. What to use depends on what you are doing, with what, why, and with what goal.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    64
    there are two basic ways, one is char[50] or whatever, but if you include the string library you can decalre a string directly:

    #include <iostream>
    #include <string>
    using namespace std;

    int main(void)
    {
    string name = "Herbert";

    cout << "hello " << name ;
    }

    hope this helps.
    if you use the standard namespace you can also do direct comparisons, i think, like;

    if (name == othername)

    without using strcmp etc.

  4. #4
    joelmon
    Guest
    Thank you so much, this was nice of you (and damn fast)

    I will practice this

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 11-23-2008, 07:12 PM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 04:33 PM
  3. Simple Calendar question for find day of the week.
    By Extro in forum C Programming
    Replies: 3
    Last Post: 02-25-2003, 09:26 AM
  4. global variable and header question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-06-2002, 12:38 AM
  5. Variable Arguments Question
    By moonwalker in forum C Programming
    Replies: 8
    Last Post: 08-04-2002, 10:08 AM

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