So I just started learning C++, and am already stuck on my first simple program I have been assigned in my class.
The program is a simple program meant to allow the suer to insert an integer number between 1-9999. The program is then to reverse the number and display it.
i.e. User types 1527, and 7251 is displayed. Or if 456 is entered, 6540 is displayed.
I thought I put it all together correctly, but I get one error which I have been trying to read up on and figure out but to no avail.
The error is:
error C2062: type 'int' unexpected
This is my short code:
#include <iostream>
#include <vector>
using namespace std;
int main(){
{
int x, y;
cout << "Enter a four number integer between 1-9999: ";
cin >> x;
y = int.reverse()
cout << "Reverse of integer: " << y;
}
}
I'm slightly embarrassed having to ask help for what I am assuming is a silly error, but I have been trying to figure this out for a couple hours now and cannot figure it out. Thank you!
OK that does make sense, not sure what I was doing there. However I am still stuck with what to do here. If you could just point me in the general direction of where I should be looking I would greatly apprecate it.
You would use a type of container like a vector to store the numbers and then use iterators to reverse the values.
I would recommend reading Bjarne Stroustrup's Programming Principles and Practice Using C++
Thank you! I hada feeling vectors were involved, but since my instructor has that lesson a few weeks away I didn't think he would require them on this lab. Thanks again!
#include <iostream>
#include <math.h>
usingnamespace std;
int reverse ( double x )
{
double xx[4] = { 0 ,0 ,0 ,0 } , p;
for ( int n = 3; n > 0; n-- )
{
p = pow(10 , n);
if ( x >= p )
{
xx[n] = x; // set xx[n] to value of x
x = (int)x % (int)p; // mod x , x = leftover
xx[n] -= x; // xx[n] - leftover
xx[n] /= p; // divide it down to a single number
}
}
xx[0] = x;
return (xx[0]*1000 + xx[1]*100 + xx[2]*10 + xx[3]);
}
int main()
{
int num;
num = reverse ( 456 );
cout << num << " " << reverse ( 12 ) << "\n";
return 0;
}