Thread: Variable question I can't find answer to

  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,829
    >>> 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, 03: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-05-2002, 11:38 PM
  5. Variable Arguments Question
    By moonwalker in forum C Programming
    Replies: 8
    Last Post: 08-04-2002, 09:08 AM