problem with the number

Jan 31, 2013 at 2:17am
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int a,b,c,d,n,m;
cout<<"a,b,c,d:";
cin>>a,b,c,d;
n=a*1000+b*100+c*10+d;
cin>>n;
m=d*1000+c*100+b*10+a;
cout<<"n="<<n<<endl;
cout<<"m="<<m<<endl;


system("PAUSE");
return EXIT_SUCCESS;
}
//I need to print the two number, but indeed it shows me that n=230580640,m=2295783, why?
Jan 31, 2013 at 2:19am
I type the 1223.
Jan 31, 2013 at 12:14pm
cin>>a,b,c,d;
should be
cin >> a >> b >> c >> d;

The reason for the incorrect result is the other variables were not initialised and so had whatever random value happened to be in that memory location.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int a,b,c,d,n,m;

    cout << "a,b,c,d: ";
    cin >> a >> b >> c >> d;

    n = a*1000 + b*100 + c*10 + d;

    m = d*1000 + c*100 + b*10 + a;

    cout << "n=" << n << endl;
    cout << "m=" << m << endl;

    return 0;
}
Last edited on Jan 31, 2013 at 12:17pm
Jan 31, 2013 at 12:49pm
Apart from the previous comment I think that you was going to enter digits not some numbers. In this case it would be better to use an object of type char to store an entered digit. Something as for example

1
2
3
4
5
6
char c;

do
{
   std::cin >> digit;
} while ( isdigit( c ) );

Last edited on Jan 31, 2013 at 12:49pm
Topic archived. No new replies allowed.