Thread: return value in function

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

    Post return value in function

    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;
    	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


  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    >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
    Registered User moi's Avatar
    Join Date
    Jul 2002
    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 07: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, 02: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, 06:39 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM