Thread: Print name "x" times using command line arguments

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,909
    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