Print name "x" times using command line arguments

This is a discussion on Print name "x" times using command line arguments within the C Programming forums, part of the General Programming Boards category; I am sure that I am way off on this but I could use some help. I need to make ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    1

    Print name "x" times using command line arguments

    I am sure that I am way off on this but I could use some help. I need to make a program that will print my name "x" amount of times passed from the command line. But if a number greater than 10 is passed to display an error. Here is what I have, probably no where near what I need.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main (int argc, char* argv[])
    {
            int i;
    
            if(argc > 10)
            {
                    printf("Usage: error can not print more than 10 times.\n");
                    pritnf("Exiting program.\n");
                    exit(0);
            }
            else
            {
                    for(i=0;i<argv[1];++i)
                    {
                            printf("zekiegypt\n");
                    }
            }
            return(0);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    Your check against argc is wrong. You should check argc to determine if you should access argv[1]. But you also should convert argv[1] into an integer appropriate for your check against 10 and to control your loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Take a look at our tutorial on command line arguments. Google around for some more examples and tutorials.

    In short, argv is an array of strings. Each string is one "word" from the command line. argc is the number of command line parameters (number of words in argv). Your loop is also incorrect because argv[1] is a string, not a number. You need to use something like atoi, or better yet, strtol to convert it to a number before coparing against i.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing arguments using "Command Line Arguments"
    By Madshan in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2006, 03:46 PM
  2. Multiple arguments in the "if" command
    By |Wiz| in forum C++ Programming
    Replies: 11
    Last Post: 09-28-2005, 12:01 PM
  3. Replies: 3
    Last Post: 06-01-2004, 04:29 PM
  4. Replies: 2
    Last Post: 06-27-2003, 01:45 PM
  5. command line "shell"
    By mart_man00 in forum C Programming
    Replies: 3
    Last Post: 09-29-2002, 01:29 AM

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