Help me finish this program

Nov 10, 2013 at 4:11pm
So I have a homework
I have to write a program which will test if an integer is a three digit number or not (I obviously have to use IF) and if it is, it should write it in reversed form. (from 123 to 321)
and also
printing should happen in file "sample.txt" ( I don't know what this means O_o)


I know that second part (reversed number) is like this
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
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
	long int num1, num2, rnum=0;
	cout << "enter an integer: " << endl;
	cin >> num1;
	num2=num1;
	do
	{
		rnum=rnum*10;
		int digit = num1%10;
		rnum+=digit;
		num1/=10;
	}
	while(num1);
		cout << " integer you typed is " << num2 << endl;
	    cout << " reversed integer is " << rnum << endl;
		system("pause");
		return 0;
}



but I can't write the whole program without errors :(

help
Nov 10, 2013 at 5:38pm
can you use string? this would be verry simple.

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
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string num;

    cout << "Please enter an integer: ";
    getline(cin, num);

    if(num.size() == 3)
    {
        cout << "The number is 3 digits long " << endl;
        cout << "The number in reverse is ";

        string::reverse_iterator it;
        for (it = num.rbegin(); it < num.rend(); it++)
            cout << *it;

        cout << endl;
    }
    else
        cout << "The number does not contain 3 digits";


    cin.ignore();
    return 0;
}
Nov 10, 2013 at 10:14pm
Yanson
Thank you very much!
Last edited on Nov 10, 2013 at 10:14pm
Topic archived. No new replies allowed.