Thread: Help: Really strange output for simple calculation.

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    2

    Question Help: Really strange output for simple calculation.

    Hey everyone,

    I have been reading up on the C language for a couple days and am trying to just play around with it a little bit and get the feel for using functions and whatnot. Well I tried to create a simple program that greets you and then asks you to enter two numbers (which another function in the program would be called on to multiply it and then output it. However, when i build and run the program, it outputs "-1115707584" for 2*2.

    If someone could point out maybe what I'm doing wrong that would be super helpful.

    Thanks,

    gpix13

    Code:
    #include <stdio.h>
    
    int multiply();
    int pause();
    
    int main () {
    	int x;
    	int y;
    	printf("Hello there!\n");
    	pause();
    	printf("Please enter a number:");
    	scanf("%i", &x);
    	printf("Please enter a second number:");
    	scanf("%i", &y);
    	multiply();
    }
    
    int multiply (int x, int y) {
    	int a = x * y;
    	printf("The product of your numbers is: %i", a);
    	return 0;
    }
    
    int pause() {
    	int move_on;
    	printf("Press 'Enter' to continue");
    	move_on=getchar();
    	return(0);
    }

  2. #2
    Registered User
    Join Date
    Jan 2010
    Location
    Ca, US
    Posts
    29
    You are protyping your multiply function without any arguments.
    Code:
    int multiply();
    When you call multiply you don't provide any arguments.
    Code:
    multiply();
    But your multiply function is expecting two arguments, so your mutilply is working with two undefined variables.
    Code:
    int multiply (int x, int y) {
    So fix your prototype, and pass your function x and y.

    While we are at it, you want to add a return 0 at the end of your main function.

    Dylan

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    C++ would not allow you to compile such a mistake, but C just ignores the extra parameters unless you declare the function as multiply(void).

    Make sure you're compiling at a very high warning level.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum Gpix13!

    First post, and you already are using *code tags* and *good indentation*.

    Well done! What a pleasure to see.
    Last edited by Adak; 01-16-2010 at 09:03 AM.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    2
    @Dylan

    Thanks so much. I think I understand what I did wrong now. Here is the new code that actually multiplies 'correctly'.

    Code:
    #include <stdio.h>
    
    int multiply(int x, int y);
    int pause();
    
    int main () {
    	int x;
    	int y;
    	printf("Hello there!\n");
    	pause();
    	printf("Please enter a number:");
    	scanf("%i", &x);
    	printf("Please enter a second number:");
    	scanf("%i", &y);
    	multiply (x, y);
    	return 0;
    }
    
    int multiply (int x, int y) {
    	int a = x * y;
    	printf("The product of your numbers is: %i", a);
    	return 0;
    }
    
    int pause() {
    	int move_on;
    	printf("Press 'Enter' to continue");
    	move_on=getchar();
    	return(0);
    }
    @iMalc
    Completely understandable, I'll be sure to check my warning settings. Thank you

    @Adak
    Thank you very much, I really appreciate it. Hopefully I keep with it to where I can actually do something with a program I make.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working through H&K... Strange Output
    By Ghiofish in forum C Programming
    Replies: 12
    Last Post: 10-07-2009, 07:46 AM
  2. Help me please.
    By yann in forum C Programming
    Replies: 15
    Last Post: 09-29-2009, 09:04 PM
  3. Binary Search - Strange Output
    By mike_g in forum C Programming
    Replies: 13
    Last Post: 06-16-2007, 02:55 PM
  4. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 02:18 AM
  5. Really strange, unexpected values from allocated variables
    By Jaken Veina in forum Windows Programming
    Replies: 6
    Last Post: 04-16-2005, 05:40 PM