Home > Software engineering >  My program skips any digit that ends with a 9
My program skips any digit that ends with a 9

Time:10-06

When I build it and run I noticed that my program sets dig1 to 0 as soon as it hits 9. So the output looks like this: 00, 01... 08, 10. I searched on stackoverflow and cplusplus.com for a possible solution but I couldn't find it.

Here's the code in question:

#include <iostream>

using namespace std;

int main()
{
    
    int i;
    char dig1 = '0', dig2 = '0';
    cout << dig2 << dig1 << endl;
    
    for(i = 0; i < 90; i  )
    {
        dig1  ;
        if(dig1 == '9')
        {
            dig1 = '0';
            dig2  ;
        }
        cout << dig2 << dig1 << endl;
    }

    return 0;
}

CodePudding user response:

Just replace (dig1=='9') with (dig1>'9') as in

#include <iostream>

using namespace std;

int main()
{
    
    int i;
    char dig1 = '0', dig2 = '0';
    
    for(i = 0; i < 90; i  )
    {
        cout << dig2 << dig1 << endl;
        dig1  ;
        if(dig1 > '9')
        {
            dig1 = '0';
            dig2  ;
        }
    }

    return 0;
}

https://godbolt.org/z/TMbjbzsE1

Produces:

00
01
02
03
04
05
06
07
08
09
10
11
12
13

CodePudding user response:

Another solution is to check the value of dig1 before you increment it, eg:

#include <iostream>

using namespace std;

int main()
{
    char dig1 = '0', dig2 = '0';
    cout << dig2 << dig1 << endl;
    
    for(int i = 0; i < 90;   i)
    {
        if (dig1 == '9')
        {
            dig1 = '0';
            dig2  ;
        }
        else
        {
            dig1  ; // <-- move it down here
        }
        cout << dig2 << dig1 << endl;
    }

    return 0;
}

Of course, there is an alternative way to implement this - use a single int instead of 2 chars, and simply ask std::cout to pad the integer value with leading zeros when needed, eg:

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int value = 0;

    while (value <= 90)
    {
        cout << setw(2) << setfill('0') << value   << endl;
    }

    return 0;
}

Or:

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int value = 0;
    
    while (value < 10)
    {
        cout << setw(2) << setfill('0') << value   << endl;
    }

    while (value <= 90)
    {
        cout << value   << endl;
    }

    return 0;
}

CodePudding user response:

Okay, so fair warning, I don't know C , and I found this by accidentally clicking the homepage. However, I did tamper with your code for fun, and found the solution (:D).

Your issue is that before your logging the number, your changing it to the next value with your if() statement.

The solution I came up with was to just log it again before updating it.

#include <iostream>

using namespace std;

int main()
{
    
    int i;
    char dig1 = '0', dig2 = '0';
    cout << dig2 << dig1 << endl;
    
    for(i = 0; i < 90; i  )
    {
        if(dig1 == '9')
        {
            cout << dig2 << dig1 << endl;
            dig1 = '0';
            dig2  ;
        }
        
        cout << dig2 << dig1 << endl;
        dig1  ;
    }

    return 0;
}

Hope this helps!

  •  Tags:  
  • c
  • Related