Thread: Help with main function only allowed to declare variables and functions.

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    3

    Help with main function only allowed to declare variables and functions.

    So I need to make a main function have no if/for/etc. statements so I need to move it to another function and call it in main. The problem is that it's a command line argument function so I'm confused on how it works. Here's an example:



    Code:
    #include <stdio.h>
    
    
    
    int main(int argc, char* argv[])
    {
    printf("The program name %s", argv[0]);
    
    if (argc == 2) {
        printf("Argument supplied is %s", argv[1]); }
    else if (argc > 2) {
        printf("Too many arguments");}
    else {
        printf("One argument");}
    
    }
    How can i make this into two functions with main only declaring variables and calling other functions?



  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should pass argc and argv to a function that checks the command line arguments. You will probably still need an if statement in the main function though, i.e., this second function would return say, a boolean value to indicate if the checks were successful or failed, upon which in main you check the return value from calling this function to determine if the program should terminate or continue.
    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

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by machine392 View Post
    [COLOR=#000][FONT=HelveticaNeue]How can i make this into two functions with main only declaring variables and calling other functions?
    Sounds like you should take a look at Functions in C.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-03-2015, 07:06 PM
  2. (split) yet another how to declare main thread
    By DevoAjit in forum C Programming
    Replies: 1
    Last Post: 10-28-2011, 05:54 AM
  3. Problems passing values from function to main variables
    By ERJuanca in forum C Programming
    Replies: 18
    Last Post: 06-12-2009, 07:13 PM
  4. passing variables from main to another function
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 03-06-2006, 07:30 PM
  5. How to declare a global variable in Main()
    By vnrabbit in forum C Programming
    Replies: 2
    Last Post: 06-20-2002, 12:59 PM

Tags for this Thread