Don't know how to solve.

I was looking through a series of C++ 'questions' and chose one of the hardest apparently. This is just a part of the whole thing, but it's the most important. I'm only a beginner and have been doing the tutorials on this website so please explain in detail if you can.
I'm trying to get a number, and turn it around, so for example: I cin x=730, the program should send back the number 37, skipping the 0's in the end of the original number; I also don't know the number of digits in x.

I tried to keep it simple but am not sure what is wrong:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{int x, y=0;
cout<<"Enter number";
cin>>x;
while (x>0)
{int a, v[100], i=0, z=0;
a=x%10;
if (a==0)
	{continue;}
else 
	{v[i]=a;
	i++;
	x=x/10;}
for (i;i>=0;i--)
	{y+=v[z]*10^i;
	z++;
}}cout<<y;
getch();}


I'm especially not sure if this operation is valid in C++: 10^i, as in 10 to the i. Also am pretty sure that if the number is 703, it would skip the 0 inside so I'd have to change that too :/ Any idea of how it could be re-written? Thanks!
Last edited on
Code tags exist for a reason. If you could edit your post, by putting them around your code you'll likely get more help. And using variables with helpful names. Single letters doesn't really say much as to what you're trying to do with them. But yea, edit post, with code tags. After your else statement, it all becomes a mess.
The ^ operator is valid, but it's not what you think. It's the bitwise XOR operator in C++. http://en.wikipedia.org/wiki/Bitwise_operation#XOR

There's a ton of ways you could do it, but it's whatever works best for you.

I'd be tempted to have the number input as a string, then I could examine the last number easily to see if it's a zero and needs dropped and swap the numbers around accordingly.

As I said, though, one of many ways. All personal preference.
Thanks for the quick answers! Sorry about the code tags, I honestly didn't know about them. Edited!

@iHutch105 I'm curious, how can you use strings for numbers in this case? I mean how do you make the program recognize that the string is a number and then use that to turn around the number? Or the program looks at it as a word, so you could use the same code to turn around a word? e.g from turn to nrut?

I'll also think about the entire program again and will post my progress, hopefully by next morning.
You could do it either way. You could either check to make sure all of the characters are digits or not bother checking. If you don't bother checking, you can reverse words too.

Here's an example:

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
48
49
50
51
52
53
54
55
56
57
#include <iostream>  // Used for basic I/O
#include <string>    // Used for string class
#include <cctype>    // Used for isdigit function

using namespace std;

int main(int argc, char* argv[])
{
   string input,            // String from user
          output;           // Output will store reversed string

   bool all_digits = false; // Flag showing whether string is all digits or not

   // Loops if not all characters are digits
   while(!all_digits)
   {
      // Get input from user
      cout << "Please enter a number (digits only): ";
      cin >> input;

      all_digits = true;

      // Check to make sure all characters are digits
      for(int i=0; i < input.length(); i++)
      {
         if(!isdigit(input[i]))
         {
            all_digits = false;
         }		
      }
   }

   // At this point, we know our string contains only digits
   // Let's check for that zero

   if(input[input.length()-1] == '0')
   {
      // If we find a zero, use substr to chop it off
      cout << "Last character is a zero\n";
      input = input.substr(0, input.length() - 1);
      cout << "New number to reverse is: " << input << endl;
   }

   int lc = input.length(); // Loop counter

   // Loop backwards through the input and append each
   // character to the output
   while(lc--)
   {
      output.append(1, input[lc]);
   }

   // Output the reversed number
   cout << "The reversed number is: " << output << endl;

   return 0;
}


If you wanted to allow text in there that could be reversed, you'd just remove anything to do with the digits check.

As I said, there's other ways to do this. Many people will prefer using integers. This is my preferred method.

Hope this helps.

P.S. - Sorry about the late reply. First time on the computer since I got home from work.
Last edited on
If this can help you...

1
2
3
4
5
int Number = 720;
while(Number && ((Number % 10) == 0))
{
    Number /= 10;
}


That will simplify 720 to 72.

EDIT: I probably missed the point, you already did know that. A suggestion then is to stringize the value, remove all the 0's with the subsequent value and terminate with a null character, then numerize it back.

You can use _itoa and atoi, but strtol and ltostr are safer.
Last edited on
I managed to do it "my way":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <conio.h>
using namespace std;

void printvector (int a[], int b)
{for (int y=0;y<b;y++)
{cout<<a[y];}}

int main()
{int x, y=0, a, v[100], i=0;
cout<<"Enter number: ";
cin>>x;

while (x>0)
{a=x%10;
if (a==0)
	{x=x/10;}
else 
	{v[i]=a;
	i++;
	x=x/10;}}
printvector (v,i);
getch();}


The only issue is that if there's a 0 inside the number, it skips it (from 703 it goes to 37) so I have to change that somehow. Is there a way for the program to check if the else statement has been executed once, and then it should skip the if statement before it? Hope you understand what I mean, so that after it reaches the 3 in 703 and the else is executed, it won't check for 0 anymore.

I'm aware that the way I did the program is pretty basic and not as advanced as any of you might do it but I can't think of any other way atm.
Random question too: in what year do you do programming at this level in university? Does it get a lot harder?

Thanks a lot for the replies, I will certainly review the string method later on.
@EssGeEich At the moment i have no idea what _itoa and those are, sorry for that!
Last edited on
Take a look at EssGeEich's algorithm. It'll do what you want. That is, chop the trailing zeroes off the number and ignore any embedded zero values.

Slight bug in my string version that I overlooked last night:
This...
1
2
3
4
5
6
7
   if(input[input.length()-1] == '0')
   {
      // If we find a zero, use substr to chop it off
      cout << "Last character is a zero\n";
      input = input.substr(0, input.length() - 1);
      cout << "New number to reverse is: " << input << endl;
   }


Should be this...
1
2
3
4
5
6
7
   while(input[input.length()-1] == '0')
   {
      // If we find a zero, use substr to chop it off
      cout << "Last character is a zero\n";
      input = input.substr(0, input.length() - 1);
   }
   cout << "New number to reverse is: " << input << endl;


The first way would only work if there was one zero on the end. The second will work for multiple.

As for your question, this level programming was probably first year stuff. The first semester of first year was all console application based. By semester two we were onto writing 2D games in a game shell.
Last edited on
Did it!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <conio.h>
using namespace std;

void printvector (int a[], int b)
{for (int y=0;y<b;y++)
{cout<<a[y];}}

int main()
{int x, y=0, a, v[100], i=0;
cout<<"Enter number: ";
cin>>x;

{while (x && (x%10==0))
{x/=10;}}

while (x>0)
	{a=x%10;
	v[i]=a;
	i++;
	x=x/10;}
printvector (v,i);
getch();}


Guess it's as simple as it gets. Thanks for the help and info!
Topic archived. No new replies allowed.