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!
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.
int main()
{
constint 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;
}
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?
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!
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>
usingnamespace 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;
}