Thread: New to Programming

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    4

    New to Programming

    Help -- I am teaching myself C through a book and one of the exercises calls for you to write a program that has a function which calls two other functions. I wrote the following code and compiled it with DevC++, Pelles C, and Quincy 99. None of them gave me any warnings or errors but the compiled program (from any of the above 3 compilers) will not run on Windows XP or Windows 98. The code is as follows:
    Code:
    /* This program asks whether or not you want to multipy or divide then */
    /* performs the calculation */
    
    #include <stdio.h>
    
    char g;
    int a, b, c, t, s;
    int x,y,w,z;
    
    int calculate(int x, int y );
    int product(int w,int z );
    int divide(int w,int z);
    
    int main( void )
    {
        printf("This program will multiply two numbers or\n");
        printf("divide the first number by the second.\n");
        printf("Press 1 if you want to multiply\n");
        printf("Press 2 if you want to divide: ");
        scanf("%d", g);
        printf("\nEnter in the first number: ");
        scanf("%d", a);
        printf("\nEnter in the second number: ");
        scanf("%d", b);
        c = calculate( a, b );
        
        if (g == 1)
        {
            printf("Their product is: %d\n", c);
        }
        else
        {
            printf("%d divided by %d is %d.\n\n", a, b, c);
        }
        
        system("PAUSE");
        return 0;
     }
    
    int calculate( x, y )
    {   
        if (g == 1)
        {
             t = product( x, y );      
        }
        else
        {    
            t = divide( x, y);
        }
        return t;
    }
    
    int product( w, z )
    {
        s = ( w * z);
        return s;
    }
    
    int divide( w, z)
    {
        s = ( w * z);
        return s;
    }
    When I run the program I encounter the error after I enter a value when prompted to enter 1 or 2.
    Any help would be very welcome. And please forgive my programming ignorance.

  2. #2
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >scanf("%d", g);
    >scanf("%d", a);
    >scanf("%d", b);
    scanf expects an address, so if the object isn't a pointer you need to prefix it with &.

    >system("PAUSE");
    This is okay for now, but keep in mind that this use of system is not recommended for several good reasons. You also need to include <stdlib.h> because that's where system is declared.

    >int calculate( x, y )
    >int product( w, z )
    >int divide( w, z )
    You need to specify the types, just like in your prototypes.

    And your divide function actually multiplies. This works:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char g;
    int a, b, c, t, s;
    int x,y,w,z;
    
    int calculate(int x, int y );
    int product(int w,int z );
    int divide(int w,int z);
    
    int main( void )
    {
        printf("This program will multiply two numbers or\n");
        printf("divide the first number by the second.\n");
        printf("Press 1 if you want to multiply\n");
        printf("Press 2 if you want to divide: ");
        scanf("%d", &g);
        printf("\nEnter in the first number: ");
        scanf("%d", &a);
        printf("\nEnter in the second number: ");
        scanf("%d", &b);
        c = calculate( a, b );
        
        if (g == 1)
        {
            printf("Their product is: %d\n", c);
        }
        else
        {
            printf("%d divided by %d is %d.\n\n", a, b, c);
        }
        
        system("PAUSE");
        return 0;
     }
    
    int calculate( int x, int y )
    {   
        if (g == 1)
        {
             t = product( x, y );      
        }
        else
        {    
            t = divide( x, y);
        }
        return t;
    }
    
    int product( int w, int z )
    {
        s = ( w * z);
        return s;
    }
    
    int divide( int w, int z )
    {
        s = ( w / z);
        return s;
    }

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    4
    This is a correction in the code I posted:

    "int divide ( w, z )" should read:
    Code:
    int divide ( w, z )
    {
          s = ( w / z );
          return s;
    }
    although the original performs the wrong function at "int divide"- I still don't see why the program won't work at all past the first prompt. Thank you for your patience.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    4

    Thank you

    Thank you for the clarification!
    and -- Wow that was quick!

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    4
    I've been getting away with a lot of messy code (until now!) it sounds like.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Originally posted by Traves
    Code:
    char g;
    int a, b, c, t, s;
    int x,y,w,z;
    
    int calculate(int x, int y );
    int product(int w,int z );
    int divide(int w,int z);
    
    int main( void )
    {
        printf("This program will multiply two numbers or\n");
        printf("divide the first number by the second.\n");
        printf("Press 1 if you want to multiply\n");
        printf("Press 2 if you want to divide: ");
        scanf("%d", g);
        printf("\nEnter in the first number: ");
        scanf("%d", a);
        printf("\nEnter in the second number: ");
        scanf("%d", b);
        c = calculate( a, b );
    You might want to change your scanf() format specifier from %d to %c to reflect the fact that the variable g is a char as opposed to an int.

    edit:: Actually, you want to change the variable g to an int - if its a char and you press 1 to select multiply, then you are putting the ascii value of one into g, which is 49 I believe - not what you want. Either way, you want your scanf() format specifiers to match the type of variable you are looking to store the data in.

    edit::

    One other thing I forgot to add - this is not something that is wrong, but it is another way to do things. Your code first, then my modification:

    Code:
    int divide ( w, z )
    {
          s = ( w / z );
          return s;
    }
    You could do this instead:

    Code:
    int divide ( w, z )
    {
          return ( w / z );
    }
    A little simpler I think.

    ~/
    Last edited by kermit; 07-16-2004 at 03:31 PM.

  7. #7
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >You might want to change your scanf() format specifier from %d to %c to reflect the fact that the variable g is a char as opposed to an int.
    That would break the program though. The conditional test is for 1, not '1'.

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    One step ahead of you. Well, maybe half a step.

  9. #9
    Quote Originally Posted by Traves
    I wrote the following code and compiled it with DevC++, Pelles C, and Quincy 99. None of them gave me any warnings or errors
    You are kidding, aren't you ?
    Code:
    Compilateur: Default compiler
    Building Makefile: "D:\Dev-Cpp\Makefile.win"
    Exécution de  make...
    make.exe -f "D:\Dev-Cpp\Makefile.win" all
    gcc.exe -D__DEBUG__ -c main.c -o main.o -I"C:/DEV-CPP/include" -W -Wall -O2   -g3
    
    main.c: In function `main':
    main.c:20: warning: format argument is not a pointer (arg 2)
    main.c:22: warning: format argument is not a pointer (arg 2)
    main.c:24: warning: format argument is not a pointer (arg 2)
    
    main.c:36: warning: implicit declaration of function `system'
    
    main.c: In function `calculate':
    main.c:41: warning: type of `x' defaults to `int'
    main.c:41: warning: type of `y' defaults to `int'
    
    main.c: In function `product':
    
    main.c:54: warning: type of `w' defaults to `int'
    main.c:54: warning: type of `z' defaults to `int'
    
    main.c: In function `divide':
    main.c:60: warning: type of `w' defaults to `int'
    main.c:60: warning: type of `z' defaults to `int'
    
    gcc.exe -D__DEBUG__ main.o  -o "Projet1.exe" -L"C:/DEV-CPP/lib" C:/Dev-Cpp/lib/libws2_32.a -g3 
    
    Exécution terminée
    Compilation OK
    Of course, you must learn how to configure your compiler correctly.
    Last edited by Emmanuel Delaha; 07-16-2004 at 04:45 PM. Reason: Quote trimming
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed