-
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=1; <10; name+1)
printf("Your name is %s/n",name);
}
int main()
{
input_name();
process();
getch();
return 1;
}
-
sorry didn't mean to create two. If possible could the admin delete this sorry to make a mess.
-
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?
-
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?