program won't multiply right

This is a discussion on program won't multiply right within the C++ Programming forums, part of the General Programming Boards category; hey everybody i'm Really new at C++ programming and as sort of a little test for myself i've decided to ...

  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, 12:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 09: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

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