For Loop Disaster

This is a discussion on For Loop Disaster within the C Programming forums, part of the General Programming Boards category; Currently trying how to use the For loop but I知 not having much luck. Just trying to print a name ...

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

    Angry For Loop Disaster

    Currently trying how to use the For loop but I知 not having much luck. Just trying to print a name ten times but I reckon I知 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=1; <10; name+1)
        printf("Your name is %s/n",name);
    }
    
    int main()
    {
        input_name();
        process();
        getch();
        return 1;
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    13
    sorry didn't mean to create two. If possible could the admin delete this sorry to make a mess.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Do you know about while loops? I think you should learn about those first. Then, all you really need to know is that:
    Code:
    for(A; B; C) {
      D;
    }
    Is identical to:
    Code:
    A;
    while(B) {
      D;
      C;
    }
    So in for, you specify three... let's refer to them as arguments, even though they aren't, really. The first is executed before the for loop is executed, the second is the while condition, and the third is executed whenever the loop's body is executed (so at the end of the body, after every iteration).

    So, now, how do you think you should do it?

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    13
    I’ve done a little on while loops but I’m not that experienced in C programming as a whole. This is my first year at college.

    I understand what you have wrote there.

    I’ve been taught for the for loop that –

    For(initialisation; test condition; Incrementation or decrementation)

    Which I kinda understand but what is initialisation? I put name=1 cause I thought you had to tell the computer that name was one thing and the rest told the computer add to one till you reach ten.

    I understand the while loop is definitely the most logical way of solving this problem but I’m just trying to understand the for loop so I can incorporate it in another program.

    I understand the theory behind the language but can put it into code. If that makes sense?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For Loop Disaster
    By Chalmers86 in forum C Programming
    Replies: 4
    Last Post: 03-23-2009, 01:58 AM
  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, 03: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, 09:12 PM

Tags for this Thread


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