What is wrong in this code?

/*
* 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

  • Aadit Kapoor
    Aadit Kapoor
    Aadit Kapoor
    /*
    * 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.
    I have resolved the error!๐Ÿ˜€
  • Kaustubh Katdare
    Kaustubh Katdare
    Aadit Kapoor
    I have resolved the error!๐Ÿ˜€
    Please post the fix so that others looking at this discussion will find it useful.
  • Nayan Goenka
    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.
  • Aadit Kapoor
    Aadit Kapoor
    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.
  • Aadit Kapoor
    Aadit Kapoor
    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.
  • Aadit Kapoor
    Aadit Kapoor
    Now i present the final code..
    /*
     * 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\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")
    #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;
    
    }
    
    
    
    
  • Nayan Goenka
    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.
  • Nayan Goenka
    Nayan Goenka
    Tell me what does this mean without surfing for this.

    int main(int argc,char *argv[])
  • Aadit Kapoor
    Aadit Kapoor
    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 
    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.
  • Kaustubh Katdare
    Kaustubh Katdare
    #-Link-Snipped-# - be nice in your responses. He's in the initial learning phases.
  • Nayan Goenka
    Nayan Goenka
    Ok cool. My bad
  • Aadit Kapoor
    Aadit Kapoor
    Kaustubh Katdare
    #-Link-Snipped-# - be nice in your responses. He's in the initial learning phases.
    ๐Ÿ˜€๐Ÿ˜€๐Ÿ˜€๐Ÿ˜€
  • Aadit Kapoor
    Aadit Kapoor
    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!

You are reading an archived discussion.

Related Posts

After a long wait, VLC - one of the most popular media player - has now been deployed on the Windows Store for Windows 8 users. The latest version of...
Galen Church submitted a new project: bndr - cloud tool for creating, managing, and sharing hardware-projects bndr is a new tool for the maker, engineer, and manufacturer. It allows the...
Using a computer-aided design tool, researchers at Virginia Tech and Massachusetts Institute of Technology have created genetic languages to handle the design of biological systems. GenoCAD is an open-source software...
The Taiwan based smartphone chip manufacturer โ€“ MediaTek announced its latest technology that helps connect devices wirelessly, with the help of newly invented proximity technology. The new invention called as...
how to create a new website in free and some tips to promote