Thread: For Loop Disaster

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    13

    Angry For Loop Disaster

    Currently trying how to use the For loop but I’m not having much luck. Just trying to print a name ten times but I reckon I’m doing something wrong? Your help would be much appreciate as i am getting very frustrated!

    Code:
    #include<conio.h>
    #include<stdio.h>
    #include<stdlib.h>
    
    char name[25];
    void input_name()
    {
         printf("please enter your name.");
         scanf("s%",&name);
    }
    void process()
    {
    for (name; <10; name+1)
        printf("Your name is %s/n",name);
    }
    
    int main()
    {
        input_name();
        process();
        getch();
        return 1;
    }

  2. #2
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Code:
    void process()
    {
    for (name; <10; name+1)
        printf("Your name is %s/n",name);
    }
    That section has a few bugs in it . . .

    First off . . . for (name; <10; name+1) is incorrect. Try for (; name<10; name++) instead.

    Now, printf("Your name is %s/n",name); again, has a bit of a problem. I'll let you find it, but one tip: /.

    Also, what's that return 1; doing there?

    Remember that bash is the opposite of C: 0 is true, successful, and !0 is false, unsuccessful. Just to confuse you some more. :P

    EDIT: A-whoops-a-dasies. Didn't notice the double post. My apologies.
    Last edited by Nightowl; 03-22-2009 at 09:03 PM.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    scanf("s%",&name); - & should be removed.

    Using global variables is not recomanded - pass parameters by pointer

    to prevent buffer overrun - add width modifier to the format

    Code:
    char name[25];
    scanf("%24s", name);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    Code:
    void process()
    {
        int i;
        for (i = 0; i<10; i++)
            printf("Your name is %s\n",name);
    }
    Okay, so what did I do here? First of all you shouldn't be checking the name variable against an integer condition. You will most likely get a compilation error as it is a type mismatch. So we replace that condition with a different one, we create an integer value and test that value.

    Another tip: refrain from using scanf(), it is particularly unsafe, try entering in a string that is more then 25 characters long and you will see. A safer alternative is fgets() (which I believe is deprecated now) or even perhaps getline(&buffer, &length, stdin); Don't use fixed size arrays on the second one because it will spit out an error if you enter a string more then the array size.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by hawaiian robots View Post
    Another tip: refrain from using scanf(), it is particularly unsafe,
    you should learn to use it correctly

    fgets() (which I believe is deprecated now)
    it is not

    or even perhaps getline(&buffer, &length, stdin);
    I do not think there is standard function like this
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For Loop Disaster
    By Chalmers86 in forum C Programming
    Replies: 3
    Last Post: 03-22-2009, 05:20 PM
  2. Tsunami disaster.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 01-09-2005, 09:56 AM
  3. NTFS disaster recovery
    By -=SoKrA=- in forum Tech Board
    Replies: 20
    Last Post: 04-17-2004, 02:16 AM
  4. Class + Union + Struct = Disaster??
    By Inquirer in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2002, 03:55 PM
  5. disaster relief
    By iain in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-13-2001, 08:12 PM

Tags for this Thread