Thread: Question on for loop

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    6

    Question 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.

  2. #2
    Registered User bljonk's Avatar
    Join Date
    Oct 2001
    Posts
    70
    #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]
    Last edited by bljonk; 05-24-2002 at 11:11 AM.
    Ünicode¬>world = 10.0£

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > '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]
    Last edited by Shadow; 05-24-2002 at 11:14 AM.
    The world is waiting. I must leave you now.

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    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.
    The world is waiting. I must leave you now.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    6

    Got it!

    Thanks for the help, I finally got it to work!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM