Thread: program won't multiply right

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    5

    program won't multiply right

    hey everybody i'm Really new at C++ programming and as sort of a little test for myself i've decided to make a short little program which takes two numbers the user enters and multiplies them and it works with no problems, except that when i put in 2 * 2 it gives me 146 as an answer? i looked at it and i see nothing wrong with it so i was hoping maybe someone here can find whats wrong with it. my code is
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
     int x;
     int y;
    
     cout<<"enter 2 numbers to be multiplied ";
     cin>> x,y;
     cin.ignore();
     cout<<"the answer is "<< x * y;
     system (" pause");
    
    }
    if anyone can help me with this problem i would appreciate it greatlym thanks

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Code:
    #include <iostream>
    #include <windows.h> //NEEDS THIS FOR SYSTEM() CALLS
    
    using namespace std;
    
    int main()
    {
     int x;
     int y;
     int Ans;
    
    
     cout<<"enter 2 numbers to be multiplied ";
     cin>> x,y;
     cin.ignore();
     Ans=x*y;
     cout<<"the answer is "<< Ans;
     Sleep(20000);
    
    }
    That should work (The sleep is just something I perfer.)

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    5
    no, that does not work, i'm still getting 146 when i just enter 2 times 2, i'm confused as to how this happens?

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Try changing x to unsigned int or LONG

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    No, the comma operator doesn't work how you want it to there.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
     int x;
     int y;
    
     cout<<"enter 2 numbers to be multiplied ";
     cin>> x >> y;
     cin.ignore();
     cout<<"the answer is "<< x * y;
    
    }

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    5
    ^^ thanks alot now it's working fine

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM