String is being output backwards...

Hi! So I have a program that will convert a string of numbers into a barcode that consists of :'s and |'s. But the only issue I have is that the numbers are all being output backwards. Im not really sure what I did to make that happen or why its happening and I would really appreciate you guys helping me out :) Thanks!!

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


string codes[] = {
    "||:::", ":::||", "::|:|", "::||:", ":|::|",
    ":|:|:", ":||::", "|:::|", "|::|:", "|:|::" };

int main() {
    int zip;
    std::cin >> zip; 
    while( zip > 0 ) {
      std::cout << codes[zip % 10];
      zip /= 10;
    }

    return 0;
}
Last edited on
let's say zip is 39857

39857 % 10 is 7
39857 / 10 = 3985

3985 % 10 is 5
3985 / 10 = 398

and so on. So the numbers get isolated from right to left.
How can I fix this so that instead of the numbers being isolated from right to left I get all the numbers that are inputed?
loop to find the max multiplier, i.e. 39857 : mx = 10000

do
39857 / 10000 = 3
39857 % 10000 = 9857
mx : 10000 / 10 = 1000
9857 / 1000 = 9
9857 % 1000 = 857
mx / 10 = 100
etc.
Last edited on
Look at this code, It does all of your work. Be sure to understand it well.

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

//Function Prototype
int reverseint(int);
int pow(int b, int e);

int main() {
    
    string codes[] = {
        "||:::", ":::||", "::|:|", "::||:", ":|::|", ":|:|:", ":||::", "|:::|", "|::|:", "|:|::"
    };
    
    int zip;
    cin >> zip; 
    
    //Reversing Integer
    zip = reverseint(zip);
    
    while( zip > 0 ) {
      cout << codes[zip % 10];
      zip /= 10;
    }

    return 0;
}

int reverseint(int num)
{

    int size = 0, result = 0;
    int x = num;
    
    //Calculating Size of num
    while(x>0)
    {
        x /= 10;
        size++;
    }
    x = num;
    
    for(int i=0;i<size;i++)
        result += ( ( ( x % pow(10,i+1) ) / pow(10,i) ) * pow(10, size-(i+1)) );
    
    return result;
}

int pow(int b, int e)
{
    int result = 1;
    
    for(int i=0;i<e;i++)
    {
        result *= b;
    }
    
    return result;

}
This is the code implemented from my last suggestion,


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


string codes[] = {
    "||:::", ":::||", "::|:|", "::||:", ":|::|",
    ":|:|:", ":||::", "|:::|", "|::|:", "|:|::" };

int main() {
    int zip;
    cin >> zip; 
    int x = 1;
    while( x*10 <= zip ){
        x *= 10;
    }
    while( x > 0 ) {
      cout << codes[zip / x];
      zip %= x;
      x /= 10;
    }

    return 0;
}
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 <cctype>
#include <string>
using namespace std;


int main()
{
    string codes[] = {
    "||:::", ":::||", "::|:|", "::||:", ":|::|",
    ":|:|:", ":||::", "|:::|", "|::|:", "|:|::" };

    cout << "Please enter an integer: ";
    char temp =  cin.get();

    while(isdigit(temp))
    {
        cout << codes[(temp - '0')];
        temp = cin.get();
    }

    return 0;
}


or

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cctype>
#include <string>
using namespace std;


int main()
{
    string codes[] = {
    "||:::", ":::||", "::|:|", "::||:", ":|::|",
    ":|:|:", ":||::", "|:::|", "|::|:", "|:|::" };

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

    for(auto element : word)
        cout << codes[(element - '0')];


    return 0;
}
Last edited on
Topic archived. No new replies allowed.