C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-22-2009, 04:54 PM   #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; <10; name+1)
    printf("Your name is %s/n",name);
}

int main()
{
    input_name();
    process();
    getch();
    return 1;
}
Chalmers86 is offline   Reply With Quote
Old 03-22-2009, 08:51 PM   #2
Resu Deretsiger
 
Nightowl's Avatar
 
Join Date: Nov 2008
Location: /dev/null
Posts: 185
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.
__________________
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.

Last edited by Nightowl; 03-22-2009 at 09:03 PM.
Nightowl is offline   Reply With Quote
Old 03-23-2009, 12:51 AM   #3
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
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);
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-23-2009, 12:55 AM   #4
Registered User
 
Join Date: Mar 2009
Posts: 8
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.
hawaiian robots is offline   Reply With Quote
Old 03-23-2009, 12:58 AM   #5
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
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

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

Quote:
or even perhaps getline(&buffer, &length, stdin);
I do not think there is standard function like this
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Reply

Tags
problem

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:38 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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