Completely new to programming.

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!
y = int.reverse()

int is a built in type. It does not have functions. Why do you think that it has a reverse function? I wish that programming could be that simple.
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.
Last edited on
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++
Last edited on
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!
no vectors just math.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <math.h>

using namespace 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;
}
no vector , swap char .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>

using namespace std;

void reverse ( char* x )
{
	char tmp;
	for ( int n = 0; n < 4; n++ )
	{
		if (x[n] == '\0')
		{
			for ( int m = 4; m > 0; m-- )
			{
				x[m] = x[m-1];
			}
			x[0] = '0';
		}
	}
	tmp = x[0];
	x[0] = x[3];
	x[3] = tmp;
	
	tmp = x[1];
	x[1] = x[2];
	x[2] = tmp;
	
}

int main()
{
	char number[5] = { '0' , '0' , '0' , '0' };
	cout << number << endl;
	cout << "enter number : ";
	cin >> number;
	reverse(number);
	cout << endl << " reverse : " << number << endl;
	
	return 0;
}

Last edited on
Topic archived. No new replies allowed.