-
Question on for loop
Hi all, My question may sound silly but please bear with me , I am new at this. I am having trouble making a for loop output what I would like it to output.
In my code, the age of the user is passed as the second argument If the user is over 20 years old, I want to output "boy are you old!" once for year old over 20 that the user is.
Here is my code, the for loop I have only outputs the message once if the user enters a number 20 or over.
#include <stdio.h>
#include <string.h>
main (int argc, char **argv)
{
int n, age;
n = strlen(argv[1]);
if (n < 5)
printf("Your name is short\n");
printf("Your name is long\n");
age = atoi(argv[2]);
for (age = 20; age < 100; ++age);
{
printf("boy are you OLD!\n");
}
}
Thanks a lot.
-
#include <stdio.h>
#include <string.h> //for strlen()
#include <stdlib.h> //for atoi()
#include <conio.h> //for getch()
#line 1 /* makes the first line: 'void main(...)' help Debuging process. good programming pratice */
void main (int argc, char **argv){
unsigned short n, age;
n = strlen(argv[1]);
age = atoi(argv[2]);
if (n < 5) {
printf("Your name is short\n");
}
if(n > 5){
printf("Your name is long\n");
}
if(age > 20){
for (; age <= 100; ++age){
printf("boy are you OLD!\n");
}
}else{
printf("boy you're YOUNG\n");
}
getch();
}[FONT=arial]
-
> 'void main(...)'
God, don't use that.
My compiler screams at me for even having it in there.
[edit]
Pleaase use code tags.
Use int main(void)
return 0; at the end of main.
...and if you think void main is good, please don't help anybody.
[/edit]
-
anatazi, possibly you could refer to this for command line stuff:
Code:
/*
Program:
CmdLine.c
Purpose:
Demonstrates the use of command line parameters.
*/
#include <stdio.h>
void default_message(void);
int main( int argc, char * argv[] )
{
if (argc == 2)
{
if(argv[1][0] == '?')
{
printf("Help stuff would go here.\n");
}
else
{
printf("%s \n", argv[1]);
}
}
else if ( argc >= 3 )
{
printf("Invalid input:\n");
printf("Only one command-line parameter allowed.\n");
}
else
{
default_message();
}
return 0;
}
void default_message(void)
{
printf("CmdLine.exe:\n");
printf("The purpose of this program is to demonstrate command line arguments.\n");
printf("This program will print whatever you type on the screen.\n\n");
printf("Example:\n");
printf("C:\\>CmdLine.exe Hmmm\n");
printf("Hmmm\n");
}
This just demonstrates command line arguments, i'll leave the age stuff to you.
-
Got it!
Thanks for the help, I finally got it to work!