Thread: add three integers function

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87

    add three integers function

    Hello.

    I am trying to write a program that adds three integers. It does not run. Where is the mistake?

    Code:
    #include <stdio.h>	
    #include <stdlib.h>	
    	
    int addthreeintegers(int i,int j,int k);	
    	
    int main(){	
    	
    int a,b,c;	
    printf("This program adds three integer values.\nPlease enter three integers separated by a space:\n");	
    scanf("%d %d %d\n",&a,&b,&c);	
    printf("The result of %d+%d+%d is %d\n",a,b,c,addthreeintegers(a,b,c));	
    	
    return 0;	
    	
    }	
    	
    	
    int addthreeintegers(int i,int j,int k){	
    	return (i+j+k);
    }
    Thank you.

    nerio

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,068
    Does it compile?
    Does it run?
    Does the console window disappear when you press enter?
    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
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    Quote Originally Posted by Salem View Post
    Does it compile?
    Does it run?
    Does the console window disappear when you press enter?
    It compiles. It runs. But when I enter the three values and then press Enter, it doesn't do anything.

    EDIT:

    I have a similar program for adding two integers that works, but I don't see why the one for three variables does not work:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int addti(int i, int j);
    
    
    main(){
    	int a,b;
    	
    	   printf("This program adds two integers. Enter two integer values separated by a space:\n");
    	   scanf("%d %d",&a,&b);
    	   printf("The addition of %d and %d is %d.",a,b,addti(a,b));
    	   
    	   return 0;
    	
    }
    
    
    int addti(int i, int j){
    
    return (i+j);
    
    }
    Last edited by nerio; 03-16-2018 at 06:46 AM.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    945
    The difference is that the one that doesn't work has a newline at the end of the format. That's what's making it hang. Get rid of the newline in the scanf format.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  5. #5
    Registered User
    Join Date
    Mar 2018
    Posts
    2
    hello ,This code cant be run .because you did not give the correct prototype of function. Thats why is not compile.
    correct code ....
    Code:
    • #include<stdio.h>
    • #include<conio.h>
    • sum(int,int,int);
    • void main()
    • {
    • int a,b,c,d;
    • clrscr();
    • printf("\nACCEPT VALUE FOR a,b,c:\n");
    • scanf("%d %d %d",&a,&b,&c);
    • d=sum(a,b,c);
    • printf("\nSUM OF %d,%d and %d IS %d",a,b,c,d);
    • getch();
    • }
    • sum(int x,int y,int z)
    • {
    • int temp;
    • temp=x+y+z;
    • return(temp);
    • }
    .

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,068
    Well the prototype in the OP was good, they didn't use void main, and they didn't use the obsolete conio.h.

    Nor did they fill their code with bullet points.
    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.

  7. #7
    Registered User
    Join Date
    Oct 2017
    Posts
    36
    The code is clear and easy to read.

  8. #8
    Registered User
    Join Date
    Mar 2018
    Posts
    5
    Nerio
    Your first program,
    I must \n remove out the scanf line.
    Your program works on two ways, enter an integer and enter, 3x, or enter an integer and the spacebar, 3X.
    IDE Newbie.

  9. #9
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Code:
    #include <stdio.h>
    static int add_two_integers(int i, int j) { return (i + j); }
    int main(void) {
      int sum = 0;
      int a;
      printf("This program adds integers.  "
             "Enter integer values separated by spaces\n"
             "End with a 0\n:");
    
      do {
        if (1 == scanf("%d", &a))
          sum = add_two_integers(sum, a);
        else break; // scanf error
      } while (a != 0);
    
      if (0 == a)
        printf("The sum is %d.\n", sum);
      else
        printf("Input error or end of file.\n");
    }
    I could have put sum += a; but I wanted to use your addti() function (renamed to add_two_integers()) to add three integers.
    Code:
    ./sum
    This program adds integers.  Enter integer values separated by spaces
    End with a 0
    :200 3000 45 0
    The sum is 3245.
    Last edited by christophergray; 04-01-2018 at 05:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 06-07-2014, 01:06 AM
  2. Replies: 2
    Last Post: 09-18-2013, 08:54 AM
  3. non-integers from created function?
    By minty33 in forum C Programming
    Replies: 4
    Last Post: 10-27-2012, 04:04 AM
  4. Integers Sums & Average Through Function
    By snayyer in forum C Programming
    Replies: 1
    Last Post: 03-09-2011, 06:01 AM
  5. integers in reverse with a function call
    By dwilson in forum C Programming
    Replies: 3
    Last Post: 04-06-2003, 08:50 AM

Tags for this Thread