Thread: Whats Wrong With This Code

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    2

    Exclamation Whats Wrong With This Code

    #include <iostream.h>
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    int mult(int x, int y);

    int main()
    {
    int x, y;
    cout<<"Please input two numbers to be multiplied: ";
    cin>>x>>y;
    cout<<"The product of your two numbers is"<<mult(x, y);

    system("pause");
    return 0;
    }
    int mult(int x, int y)
    {

    return x*y;
    }

    thats my code
    it works fine but it sayd The product Of your 2 numers are <num>Press any button to continue straight after without aany spaces or a new line
    whats wrong with it ?

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    It's because i/o is buffered by default, which means unless you flush the output buffer yourself, it isn't flushed until there is further input or as in your case your program ends. As your program doesn't end until after your call to system("pause"); you won't see the output until after this has returned. One method of preventing this would be to flush the output imeadiately -

    cout<<"The product of your two numbers is"<<mult(x, y)<<flush;

    However as you probably want a newline you could replace flush with endl (which will also flush the buffer).
    zen

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Try it this way.

    cout<<"The product of your two numbers is "<<mult(x, y) << endl;

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Actually, try this:

    cout<<endl << "The product of your two numbers is "<<mult(x, y) << endl;

    You need to explicitly tell it to use new lines. The above will put the line breaks you want in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM