Thread: Friends please review the code (help with the erro)

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    28

    Friends please review the code (help with the erro)

    Here is my code
    Code:
    /* A simple C program that takes
    * two values and invoke a function with
    * pass by value */
    
    
    # include <stdio.h>
    # include <stdlib.h>
    
    
    int main() {
    system("cls");
    
    
    //Prompting user to enter two numbers
    int num1, num2, sum;
    printf("User enter first number to be added: ");
    
    
    //Storing first number in num1
    scanf("%d", num1);
    
    
    
    
    printf("User enter second number to be added: ");
    scanf("%d", num2);
    
    
    sum  = 0;
    
    
    sum = add_Func(num1, num2, sum);
    
    
    printf("User added answer is %d", sum);
    
    
    return 0;
    }
    
    
    int add_Func(int x, int y, int &sum) {
    printf("User adding %d and %d", x, y);
    
    
    sum = x + y;
    
    
    return sum;
    }
    Error is shown in Line 31


    ||=== Build: Release in eg01 (compiler: GNU GCC Compiler) ===|
    C:\Users\Dushyant\cProjects\eg01\main.c||In function 'main':|
    C:\Users\Dushyant\cProjects\eg01\main.c|16|warning : format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat]|
    C:\Users\Dushyant\cProjects\eg01\main.c|20|warning : format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat]|
    C:\Users\Dushyant\cProjects\eg01\main.c|24|warning : implicit declaration of function 'add_Func' [-Wimplicit-function-declaration]|
    C:\Users\Dushyant\cProjects\eg01\main.c|31|error: expected ';', ',' or ')' before '&' token|
    ||=== Build failed: 1 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|



    Please reply soon. That's necessary

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,759
    Yes, it means you forgot the &var in all your scanf calls
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    You also have to add declaration of add_Func before you call it
    Code:
    /* A simple C program that takes
    * two values and invoke a function with
    * pass by value */
    
    
    # include <stdio.h>
    # include <stdlib.h>
    
    //Function declaration
    int add_Func(int x, int y, int &sum);
        
    
    int main() {
    
        //main code skipped
        return 0;
    }
    
    //function definition
    int add_Func(int x, int y, int &sum) {
        //body skipped
    }
    Thus compiler will be able to detect that you are calling your function with wrong parameters - reference is C++ feature not available in C (instead of fixing problem with sum parameter - you can completely remove it since the value will be returned)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    707
    I don't think the OP even knows about a C++ reference. I think he used a '&' when he meant to use a '*'.

    His code suggests he is very much a beginner, as most of the posters here.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    Trying to delete
    Last edited by vart; 09-02-2017 at 08:47 AM. Reason: missed compiler output
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please review my code
    By ari01 in forum C++ Programming
    Replies: 2
    Last Post: 02-20-2014, 01:01 PM
  2. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 09:00 PM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. review this code
    By KIBO in forum C Programming
    Replies: 12
    Last Post: 08-14-2007, 02:28 PM
  5. review code
    By absenta in forum C++ Programming
    Replies: 3
    Last Post: 04-09-2002, 02:13 PM

Tags for this Thread