CrazyEngineers
  • /*
    * main.c
    *
    *  Created on: 13 Mar 2014
    *      Author: Aadit
    */
    #include 
    #include 
    #include 
    #include 
    #include 
    #define PRINT_HELP  printf("Help commands \n: start - TO START THE PROGRAM\version - VERSION OF PROGRAM\n")
    #define PROG_NAME(x) (printf("Current program running: %s.exe\n",x))
    #define USAGE   printf("Usage:  For list of commands type  help\n")
    #define PRINT_VERSION  printf("Version : 1.0.0.0\n");
    #define GET_NUMBER1(x) (scanf("%f",&x))
    #define GET_NUMBER2(x) (scanf("%f",&x))
    #define INPUT_NUMBERS  printf("Input two numbers ,one for square root and one for square:\n")
    /* Number system */
    struct Base
    {
        float num1,num2;
    };
    int check(char a);
    void get_data(struct Base *b);
    int sq_root(float a);
    void calculation(float *a,float *b,struct Base *b_test);
    void check_arg(float,float,struct Base,char*[]);
    
    int main(int argc,char *argv[])
    {
        struct Base base;
        float sq_root,sq;
    
        PROG_NAME(argv[0]);
        check_arg(sq,sq_root,base,argv[1]);
        if(argc<=1)
        {
            printf("Bye\n");
            exit(0);
        }
        return 0;
        system("cls");
        }
    
    
    int square(float a)
    {
        return (a*a);
    }
    int sq_root(float a)
    {
        return(sqrt(a));
    }
    void get_data(struct Base *b)
    {
        // Get values
        GET_NUMBER1(b->num1);GET_NUMBER2(b->num2);
    }
    void calculation(float *a,float *b,struct Base *b_test)
    {
        *a  = square(b_test->num1); // calculate square
        *b = sq_root(b_test->num2); // calculate square root
    
        // return multiple values using pointers
    }
    int check(char a)
    {
        if(a == 's')
            return 0; // show square result
        else
            return 2; // show square_root
    }
    
    void check_arg(float a,float b,struct Base b1,char *c[])
    {
        if(!strcmp(c[1],"start"))
        {
            system("cls");
            INPUT_NUMBERS;
            get_data(&b);
            calculation(&a,&b,&b);
        }
        else if(strcmp(c[1],"version"))
        {
            PRINT_VERSION;
        }
        else if(strcmp(c[1],"help"))
        {
            PRINT_HELP;
        }
    
        else if(!strcmp(c[1],"usage"))
            USAGE;
    
    }
    
    What is wrong?
    I think there is an error in command line arguments.
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Aadit Kapoor

    MemberMar 14, 2014

    Aadit Kapoor
    /*
    * main.c
    *
    *  Created on: 13 Mar 2014
    *      Author: Aadit
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <conio.h>
    #define PRINT_HELP  printf("Help commands \n: start - TO START THE PROGRAM\version - VERSION OF PROGRAM\n")
    #define PROG_NAME(x) (printf("Current program running: %s.exe\n",x))
    #define USAGE   printf("Usage: <program name (command)> For list of commands type <program name> help\n")
    #define PRINT_VERSION  printf("Version : 1.0.0.0\n");
    #define GET_NUMBER1(x) (scanf("%f",&x))
    #define GET_NUMBER2(x) (scanf("%f",&x))
    #define INPUT_NUMBERS  printf("Input two numbers ,one for square root and one for square:\n")
    /* Number system */
    struct Base
    {
        float num1,num2;
    };
    int check(char a);
    void get_data(struct Base *b);
    int sq_root(float a);
    void calculation(float *a,float *b,struct Base *b_test);
    void check_arg(float,float,struct Base,char*[]);
    
    int main(int argc,char *argv[])
    {
        struct Base base;
        float sq_root,sq;
    
        PROG_NAME(argv[0]);
        check_arg(sq,sq_root,base,argv[1]);
        if(argc<=1)
        {
            printf("Bye\n");
            exit(0);
        }
        return 0;
        system("cls");
        }
    
    
    int square(float a)
    {
        return (a*a);
    }
    int sq_root(float a)
    {
        return(sqrt(a));
    }
    void get_data(struct Base *b)
    {
        // Get values
        GET_NUMBER1(b->num1);GET_NUMBER2(b->num2);
    }
    void calculation(float *a,float *b,struct Base *b_test)
    {
        *a  = square(b_test->num1); // calculate square
        *b = sq_root(b_test->num2); // calculate square root
    
        // return multiple values using pointers
    }
    int check(char a)
    {
        if(a == 's')
            return 0; // show square result
        else
            return 2; // show square_root
    }
    
    void check_arg(float a,float b,struct Base b1,char *c[])
    {
        if(!strcmp(c[1],"start"))
        {
            system("cls");
            INPUT_NUMBERS;
            get_data(&b);
            calculation(&a,&b,&b);
        }
        else if(strcmp(c[1],"version"))
        {
            PRINT_VERSION;
        }
        else if(strcmp(c[1],"help"))
        {
            PRINT_HELP;
        }
    
        else if(!strcmp(c[1],"usage"))
            USAGE;
    
    }
    
    What is wrong?
    I think there is an error in command line arguments.
    I have resolved the error!😀
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorMar 14, 2014

    Aadit Kapoor
    I have resolved the error!😀
    Please post the fix so that others looking at this discussion will find it useful.
    Are you sure? This action cannot be undone.
    Cancel
  • Nayan Goenka

    MemberMar 14, 2014

    The code is copied. Please avoid plagiarizing content. Or if you want to discuss codes written by others, please don't try to say its yours.
    Are you sure? This action cannot be undone.
    Cancel
  • Aadit Kapoor

    MemberMar 14, 2014

    Nayan Goenka
    The code is copied. Please avoid plagiarizing content. Or if you want to discuss codes written by others, please don't try to say its yours.
    How can you be sure it is copied??
    This is a site where people can share knowledge.I would really appreciate if you keep your false opinions and comments about others to yourself.

    Can i get some proof if this code is copied.
    Are you sure? This action cannot be undone.
    Cancel
  • Aadit Kapoor

    MemberMar 14, 2014

    Kaustubh Katdare
    Please post the fix so that others looking at this discussion will find it useful.
    Hello Sir,The problem was in the check_arg(...) function and the problem was that i forget to put (!) in strcmp() to check whether the value returned is 0.
    Are you sure? This action cannot be undone.
    Cancel
  • Aadit Kapoor

    MemberMar 14, 2014

    Now i present the final code..
    /*
     * main.c
     *
     *  Created on: 13 Mar 2014
     *      Author: Aadit
     */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <conio.h>
    #define PRINT_HELP  printf("Help commands \n: start - TO START THE PROGRAM\version - VERSION OF PROGRAM\n")
    #define PROG_NAME(x) (printf("Current program running: %s\n",x))
    #define USAGE   printf("Usage: <program name (command)> For list of commands type <program name> help\n")
    #define PRINT_VERSION  printf("Version : 1.0.0.0\n");
    #define GET_NUMBER1(x) (scanf("%f",&x))
    #define GET_NUMBER2(x) (scanf("%f",&x))
    #define INPUT_NUMBERS  printf("Input two numbers ,one for square root and one for square:\n")
    #define GET_FLAG(x)    (scanf("%c",&x))
    #define MESSAGE   printf("Enter (s) to see square result and (sq) to see square root:\n");
    /* Number system */
    struct Base
    {
    	float num1,num2;
    };
    int check(char a);
    void get_data(struct Base *b);
    int sq_root(float a);
    void calculation(float *a,float *b,struct Base b_test);
    void check_arg(float,float,struct Base,char*[],char);
    
    int main(int argc,char *argv[])
    {
    	struct Base base;
    	argv[2] = NULL;
    	char flag;
    	float sq_root=0.0,sq=0.0;
    	if(argc == 2)
    	{
    	PROG_NAME(argv[0]);
    	//printf("%s",argv[1]); For testing
    	check_arg(sq,sq_root,base,argv,flag);
    	}
    	else
    		USAGE;
    	return 0;
    	system("cls");
    	}
    
    
    int square(float a)
    {
    	return (a*a);
    }
    int sq_root(float a)
    {
    	return(sqrt(a));
    }
    void get_data(struct Base *b)
    {
    	// Get values
    	GET_NUMBER1(b->num1);GET_NUMBER2(b->num2);
    }
    void calculation(float *a,float *b,struct Base b_test)
    {
    	*a  = square(b_test.num1); // calculate square
    	*b = sq_root(b_test.num2); // calculate square root
    
    	// return multiple values using pointers
    }
    int check(char a)
    {
    	if(a == 's')
    		return 0; // show square result
    	else
    		return 2; // show square_root
    }
    
    void check_arg(float a,float b,struct Base b1,char *c[],char f)
    {
    	if(!strcmp(c[1],"start"))
    	{
    		system("cls");
    		INPUT_NUMBERS;
    		get_data(&b);
    		calculation(&a,&b,b1);
    		fflush(stdin);
    		MESSAGE;
    		GET_FLAG(f);
    		if(!check(f))
    			printf("Calculated square : %f\n",a);
    		else
    			printf("Calculated square root : %f\n",b);
    
    
    	}
    	else if(!strcmp(c[1],"version"))
    	{
    		PRINT_VERSION;
    	}
    	else if(!strcmp(c[1],"help"))
    	{
    		PRINT_HELP;
    	}
    
    	else if(!strcmp(c[1],"usage"))
    		USAGE;
    
    }
    
    
    
    
    Are you sure? This action cannot be undone.
    Cancel
  • Nayan Goenka

    MemberMar 14, 2014

    Ok dude. I have been programming stuff for the past 6 years and I can pretty well easily infer what kind of codes a newborn can write. And yes, I don't wanna argue with you on this but I think you should be more concentrating on learning now rather than showing off your programming skills.

    How far did you reach on php? M sure at this pace you will have your own Xenforo made within 2-3 months.
    Are you sure? This action cannot be undone.
    Cancel
  • Nayan Goenka

    MemberMar 14, 2014

    Tell me what does this mean without surfing for this.

    int main(int argc,char *argv[])
    Are you sure? This action cannot be undone.
    Cancel
  • Aadit Kapoor

    MemberMar 14, 2014

    Nayan Goenka
    Tell me what does this mean without surfing for this.

    int main(int argc,char *argv[])
    First of all,These are known as command line arguments
    int argc controls the number of arguments given
    char *argv[] is a character pointer pointing to argc.
    for example-:
    there is a program
    main.c
    #include <stdio.h>
    main(int argc,char *argv[])
    {
    if(argc == 2)
    printf("Two args supplied");
    else
    printf("1");
    }
    
    Here argv[0] is the name of the program and argv[1] is the value provided.
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorMar 14, 2014

    #-Link-Snipped-# - be nice in your responses. He's in the initial learning phases.
    Are you sure? This action cannot be undone.
    Cancel
  • Nayan Goenka

    MemberMar 14, 2014

    Ok cool. My bad
    Are you sure? This action cannot be undone.
    Cancel
  • Aadit Kapoor

    MemberMar 14, 2014

    Kaustubh Katdare
    #-Link-Snipped-# - be nice in your responses. He's in the initial learning phases.
    😀😀😀😀
    Are you sure? This action cannot be undone.
    Cancel
  • Aadit Kapoor

    MemberMar 15, 2014

    Nayan Goenka
    Ok dude. I have been programming stuff for the past 6 years and I can pretty well easily infer what kind of codes a newborn can write. And yes, I don't wanna argue with you on this but I think you should be more concentrating on learning now rather than showing off your programming skills.

    How far did you reach on php? M sure at this pace you will have your own Xenforo made within 2-3 months.
    Oh! I have written this code and by the way i am learning c,you only told me to learn C.
    I was not showing off,i was just asking for help,and don't call me a newborn programmer.
    Everyone here is a newborn programmer,people come here to learn not argue!
    No one is perfect,and i don't think here that anyone has mastered programming.
    People come here to learn new things,discover,they are not like you who discourage children like me!
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register