return value in function

This is a discussion on return value in function within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> int num(); int main () { int i, input, result; printf("enter a number: "); scanf("%d", &input); result = ...

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    Post return value in function

    Code:
    #include<stdio.h>
    
    int num();
    
    int main ()
    {
    	int i, input, result;
    
    	printf("enter a number: ");
    	scanf("%d", &input);
    
    	result = input + num();
    	printf("your number %d + %d gives %d\n", input, num(), result);
    	return 0;
    }
    int num()
    {
    	int i;
    	for(i=0;i<10;i++)
    
    	return i;
    }
    i want to display a number given by the user. the for loop in the function num should increase the number what is being added to the number given by the user. this happens ten times.

    the output should look like this

    your number 1 + 1 gives 2
    your number 1 + 2 gives 3
    your number 1 + 3 gives 4

    i guess its a really simply mistake in the num function but i need help

    threadhead

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >i guess its a really simply mistake in the num function but i need help
    Well, you could start by trying to print something within the loop.

    At the moment your code (function num) simply loops i from 0 to 10) and returns 10. Then it prints out. See the mistake now?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    the problem is you only call num () once or print the results once. think about the logical flow of your code: if you want to do it more than once, than your printf () call must be contained in a loop.

    edit: arrrg hammer you're too fast!
    Last edited by moi; 10-18-2002 at 08:40 PM.
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21