Thread: Kind of a little problem (HW)

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    14

    Unhappy Kind of a little problem (HW)

    I have a little problem getting my code to work because when I run the program it allows me to enter in the numbers, but after doing so it immediately quits.

    Here's the problem: Write two functions:
    A function named sum which takes two integer values as arguments, and returns their sum.
    A main function which asks the user for two integer values, passes the two values to the sum function, then prints the two values and their sum.
    The main function then again asks the user for two integer values, passes the two values to the sum function, then prints the two values and their sum.

    Code:
    #include <stdio.h>
    
    void getData (int* a, int* b);
    int add (int a, int b);
    void printRes (int a, int b, int sum);
    
    int main (void)
    {
       int a;
       int b;
       int sum = 0;
       
       getData (&a, &b);
       sum = add (a, b);
       printRes(a, b, sum);
       return 0;
    }
    
    void getData (int* a, int* b)
    {
         printf("\nPlease enter the first integer number: ");
         scanf("%d", a);
         printf("\nPlease enter the second integer number: ");
         scanf("%d", b);
         return;
         }
         
         int add (int a, int b)
    
    {
         int sum;
         
         sum = a + b;    
         return sum;
    }
    void printRes (int a, int b, int sum)
    {
       printf("%d + %d = %d\n", a, b, sum);
       return;
    }
    If anyone could help me dissect the problem that would be greatly appreciated since the assignment is due today...

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Works fine for me, but I'm using a terminal on my Linux box. Is this your problem: Cprogramming.com FAQ > Stop my Windows Console from disappearing everytime I run my program?

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    14
    Yeah that was the problem, but I managed a work-around.

    I just put:

    Code:
    int temp;
    
    printf("Enter an integer and press Enter to exit the program: ");
    
    scanf("%d", &temp);
    before return 0;

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with passing structs to a function.
    By darsh1120 in forum C Programming
    Replies: 7
    Last Post: 03-11-2008, 04:36 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM