PDA

View Full Version : Qtcreator won't calculate correctly



thefatladysingsopera
3rd June 2011, 15:18
I have the following console program on qtcreator 4.7.


#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
int a;
int b;
int result = a * b;

cout<<"Enter a";
cin>>a;

cout<<"Enter b";
cin>>b;

cout<<"You entered"<<a<<"and you entered"<<b<<"Their product is"<<result<<endl;
cout << "Press any key to exit..." << endl;
system ("pause");
return 0;
}

When i input 5 and 9,i get You entered5and you entered9Their product is-2009147472

why am i not getting 45?.

Lykurg
3rd June 2011, 15:25
Hm, that's just a guess, but maybe you should calculate the result after the user has entered the numbers...

thefatladysingsopera
3rd June 2011, 16:19
Thanks,that was the problem.


cout<<"Enter a";
cin>>a;

cout<<"Enter b";
cin>>b;

int result = a * b;

worked.

ChrisW67
4th June 2011, 00:43
This question has precisely nothing to do with either Qt or Qt creator.

For future reference, the language you are writing code in is called C++.