Thread: Whats wrong with my C program?

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    156

    Whats wrong with my C program?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    
    int main(int argc, char *argv[])
    {
    char name[]="";
    int length=0, i=0;
    
    printf("Input your name: ");
    scanf("%c", name);
    length= strlen(name);
    
    printf("Your name is: ");
    while (i < length) {
    printf("%c", name[i]);
    i++; }
    
    printf("\n\n");
    system("pause");
    return EXIT_SUCCESS;
    }
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: [email protected]

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    scanf(%c", &name) will only give you a single character.

    char name[80];

    gets(name);

    printf("The name was %s", name);

    or scanf("%s", name);
    Last edited by ronin; 06-30-2002 at 09:49 PM.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    156
    I know about the strings. I was trying to make the loop for.
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: [email protected]

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    "I know about the strings..."
    Apparently not!

    char name[]="";

    The compiler is obligated to allocate precisely ONE character for this string! And that is for the null-terminator!! So pay attention:

    char name[] = "Joe"; //...compiler allocates 4 bytes - No More!

    char name[100]; //...excellent...100 bytes alloc'd

    change that one line of code and your for() loop will work.

    P.S.: If you want to see for yourself, just print the value of strlen...

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by ronin
    scanf(%c", &name) will only give you a single character.

    char name[80];

    gets(name);

    printf("The name was %s", name);

    or scanf("%s", name);
    Please, never recommend the use of gets(), it's a function that should've been got rid of a long time ago....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284

    Smile

    A stack smasher who doesnt like gets )

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by pinko_liberal
    A stack smasher who doesnt like gets )
    Well, you use it, and I'll abuse it
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Unregistered
    Guest
    Shouldn't you be including <string.h>, not simply <string>?

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    7

    where is ...

    EXIT_SUCCESS defined???

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >EXIT_SUCCESS defined???
    Along with EXIT_FAILURE, EXIT_SUCCESS is defined in stdlib.h.

    -Prelude
    My best code is written with the delete key.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Prelude
    >EXIT_SUCCESS defined???
    Along with EXIT_FAILURE, EXIT_SUCCESS is defined in stdlib.h.

    -Prelude
    Hey, you better watch it. You don't want to be too helpful. Someone might get angry for you enlighten--er--bad mouthing them...

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Unregistered
    Guest

    why not gets()

    what's the problem with gets()??

  13. #13
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    char name[80];

    gets(name);

    printf("The name was %s", name);

    or scanf("%s", name);
    >what's the problem with gets()??
    Absolutely nothing, unless the user entered a string longer than 79 characters. That's because you have allocated only 80 bytes for the variable 'name', and gets() would read from the stdin and copy the buffer to this variable. If the length of the copy exceeded the limit allocated to this variable, there would be a risk of writing the data to some other memory location, which might be used by some other application/function, probably corrupting the data there and crashing the program using it. Or, your own program might terminate with an error due to to access of memory not allocated to/by your program. So, it's always safer to use fgets(), with the FILE * argument of it set to stdin, where you can limit the number of characters to be read to some limit of your choice.

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    156
    Hey guys, I took your advice but my program still doesn't work. I have it, along with the .exe file in a zip. So here it is to download..........
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: [email protected]

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  15. #15
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    >Hey guys, I took your advice but my program still doesn't work. I have it, along with the .exe file in a zip. So here it is to download..........
    How about pasting the code here instead of attaching it as an attachment. I'm sure there are many more like me who cannot download it for various number of reasons. And about the .exe, it's not at all safe to download anything like that on the internet, so I doubt if anybody would really download your attachment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 01:31 PM
  2. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  4. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 09:57 AM
  5. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 05:09 PM