Thread: Pls help in writing the program

  1. #1
    Registered User
    Join Date
    Nov 2001

    Pls help in writing the program


    I wrote a simple program that accepts name ,age from user.
    i declared name as char and age as int.Somehow when it prompts the statement to ask the value from user something is happening.Meaning if i enter jacob as name it immediately displays the next statement as enter ur age and the coming out of the program.

    can anyone psl tell why is this happening ?

    int main(){

    char name;
    int age;
    cout<<"enter ur name"<<endl;
    cout<<"enter ur age"<<endl;
    return 0;

    somhow it is not taking long name as input to name field.Pls suggest some solution.


  2. #2
    Registered User f0ul's Avatar
    Join Date
    Nov 2001

    A raw newbie replies!

    Im a bit new at this -- But .. looking at the code I would assume that the problem is the lack of array after the char name


    char name[];

    I have no idea if this is correct .. but this is what I would do!
    I don't want to belong to any club that'll accept me as a member!

  3. #3
    Registered User
    Join Date
    Aug 2001
    almost right, fOul. It is not possible to assign a string of char like Jacob to a single char like name as bsb77 wrote. You assign a string of char to a char array (which is often called a c_style string) or to an instance of a string class. However, you need to be sure the char array/string you are assigning to has been allocated an adequate of memory to hold the string firs. The format fOul uses will only work when the char array is being declared, in which case the compiler determines the amount of memory you need.

    char array1[] = "Happy Holidays";

    It is probably better to allocate memory yourself so you get into the habit of it though. There are several ways to do it. First is like this:

    char array2[80];

    the value I placed in [] is arbitrary and can be any value you want/need as long as you have enough memory in your stack. If you want to go further afield you can even use char * to work with strings, if you are careful. For example:

    char * array3;

    is valid. As declared array3 is a pointer to a char. Theoretically that char may be a single char or the first char in a string, you can't tell by just the declaration alone, however. Also, since array3 has not had any memory assigned to it yet it may be pointing anywhere, so DON'T use it until you have assigned it some memory. To assign it some memory you could do this:

    char * array3 = "Freuliche Weinachten";

    or this:

    char ch ='$';
    array3 = &ch;

    or this:

    int x = 80;
    array3 = new char[x];//declare a char array using dynamic memory.

    or whatever, but it MUST be assigned memory somehow before you use it or your compiler will generate an error if you are lucky and your program will crash if you aren't.

    PS there are other issues involving use of the new operator, too, but now I am getting too far afield and will shut up.
    Last edited by guest; 11-29-2001 at 04:01 PM.

  4. #4
    Registered User
    Join Date
    Nov 2001

    Thank you !

    foul and guest thank you so much for your help !


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  2. Writing a checklist program for MTGO?
    By CrYpTiC in forum Game Programming
    Replies: 2
    Last Post: 04-08-2005, 09:46 PM
  3. Help on writing program
    By robasc in forum C Programming
    Replies: 18
    Last Post: 03-07-2005, 12:19 PM
  4. writing a program called tail like the one in unix
    By fanaonc in forum C Programming
    Replies: 2
    Last Post: 11-04-2001, 02:20 AM