Is there any way to count numbers

I have to make a program that displays any number that the user enters backwards. So far I have only come up with a way to display a predetermined amount of characters backwards( 5 in this program ). If I could count the characters the user enters, I think I'd be able to display any amount of characters backwards. Is there a way to do this? I have my program below. Thanks to anyone for help!
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
40
41
int main()
{
    char num1, num2, num3, num4, num5;
    int counter = 5;

    cout << "Enter a number with only 5 characters: ";


        while(counter > 4)
        {
            cin.get(num1);
            counter--;
        }

        while(counter > 3)
        {
            cin.get(num2);
            counter--;
        }

        while(counter > 2)
        {
            cin.get(num3);
            counter--;
        }

        while(counter > 1)
        {
            cin.get(num4);
            counter--;
        }

        while(counter > 0)
        {
            cin.get(num5);
            counter--;
        }

        cout << endl << "Your number backwards is: " << num5 << num4 << num3 << num2 << num1 << endl;
    return 0;
}



Input: 12345
Output: Your number backwards is: 54321
You can convert the int to a string and then use the string's length function to determine the number of digits.

EDIT: Oh, it seems that you're getting the user to enter the characters directly. In this case, you could get the input as a string (using std::getline) and then use the string's length function. Although it would be slightly more interesting if you get the input as an int.
Last edited on
You can use an array of characters

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{

    const int counter = 5;
    char nums[counter];

    cout << "Enter a number with only " << counter << " characters: ";


    int i = 0;

    for ( ; i != counter; i++ ) cin.get( nums[i] );

    cout << endl << "Your number backwards is: "

    while ( i )  cout << nums[--i];
    cout << endl;

    return 0;
} 

Last edited on
Thanks guys. Vlad, thanks but I cannot use arrays. Cannot wait to get to them in the book. Shacktar, thanks for the string length function. I included it into my program but I cannot correctly convert a char into a string variable which is all I need it seems. Is there a function that counts the characters of a char variable?


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
40
41
42
43
44
45
46
47
int main()
{
    string getTheLengthNum;
    char num1, num2, num3, num4, num5;
    int counter, intNum;

    cout << "Enter a number: ";
    cin >> num1;
    getTheLengthNum = num1; /*Trying to convert char to string*/
    cout << getTheLengthNum.length(); /*Checking to see if conversion worked but it only outputs "1" regardless of how many characters I enter.*/


    while(counter >= 0)
    {
        while(counter > counter - 1)
        {
            cin.get(num1);
            counter--;
        }

        while(counter > counter - 2)
        {
            cin.get(num2);
            counter--;
        }

        while(counter > counter -3)
        {
            cin.get(num3);
            counter--;
        }

        while(counter > counter - 4)
        {
            cin.get(num4);
            counter--;
        }

        while(counter > counter - 5)
        {
            cin.get(num5);
            counter--;
        }
    }
        cout << endl << "Your number backwards is: " << num5 << num4 << num3 << num2 << num1 << endl;
    return 0;
}

If you're not allowed to use arrays, then you're probably not allowed to use strings either, since they're basically arrays of characters.

You can do something like this instead:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using namespace std;

int main()
{
	int num;
	cout << "Enter a positive integer: ";
	
	while ((cin >> num) && (num > 0))
	{
		cout << "\nYour number backwards is:\n";
		while (num != 0)
		{
			cout << num % 10;
			num /= 10;
		}
		cout << "\n\nEnter a positive integer: ";
	}
	
	cout << "\nGoodbye!" << endl;
	return 0;
}
Wow I just tried out the program and it works! First, how am I allowed to input anything when there isn't a cin operator! I see it in the while loop but is that how I am allowed to input a number?? And what is the this symbol: /=. I'm guessing it outputs things backwards??
I'm just a beginner myself, so I don't know if what I'm saying is true or if I just misunderstand what I'm doing.

The while loop needs to examine the expressions in the parentheses to figure out whether or not the stuff in the loop body gets executed.

In the parentheses, I have (cin >> num) && (num > 0). You can think of (cin >> num) as a type of calculation that returns cin, with some changes. And as one of the steps to performing this calculation, you're required to input something.

The returned cin may have flags checked depending on exactly what you input. In the code, num is an integer, so cin is ok, and (cin >> num) will evaluate as true if you input an integer. If you input something invalid like "asdf", then the returned cin has error flags and (cin >> num) evaluates as false.

num /= 10 is simply another way of saying assign a new value to num, which is equal to the old value of num divided by 10.
This definitely was a major help. I'm still a little cloudy on the /= symbol but I will look into it. Thanks a lot!
This definitely was a major help. I'm still a little cloudy on the /= symbol but I will look into it. Thanks a lot!


Here's one way of looking at it:

1
2
3
 num /= 10;
// can also be written as
num = num / 10;


Take a look at the output for this program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
	int num = 12345;
	cout << "num: " << num << endl;

	num /= 10;
	cout << "new num after num /= 10: " << num << endl;

	num = num / 10;
	cout << "new num after num = num / 10: " << num << endl;

	cin.get();
	return 0;
}


Hope that clears things up.
Last edited on
Topic archived. No new replies allowed.