Checking each digit

I have question how do I check each digit of number?

You will see what I have down below. So I have numbers :
14 254 102 55 91

And what I need to do with them is check each digit and find the highest one. So for example 254 it would be 5. (I need to do it with 2-3 cycles). I know how I'm going to use if to find that but I just don't know how to check each digit. Tried reversing numbers etc but I'm completely lost.


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
#include <iostream>
#include <fstream>

//
const char duom[] = "duomenys.txt";
const char rez[] = "rezultatai.txt";
//

using namespace std;

int main()

{
    ifstream fd(duom);
    ofstream fr(rez);
    
    int n, sk, i ,i2, liek, did, maz, sksum;
    
    sksum = 0;
    did = 0; // highest
    maz = 0; // lowest
    
    fd >> n;
    
    for (i = 1; i <= n; i++) {
        
        fd >> sk;
    
        while (sk > 0) {
            
            sksum = (sksum * 10) + sk % 10;
            sk /= 10;  
        
        }
        
    }
    
    
    
    fd.close();
    fr.close();
    
    return 0;
}
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <algorithm>
using namespace std;

unsigned maxDigit( unsigned n ) { return n ? max( maxDigit( n / 10 ), n % 10 ) : 0; }

int main()
{
   for ( int n : { 14, 254, 102, 55, 91 } ) cout << n << ": " << maxDigit( n ) << '\n';
}
14: 4
254: 5
102: 2
55: 5
91: 9
Last edited on
You could use the % and / operators. % 10 will give you the last digit and dividing the number by ten will remove it.

Another way would be to convert to a string and use std::max to get the highest digit.

My guess is hat this is homework and you wwon't be allowed the second option.

I both case it would be best to write a function for this task, so it's easy to test - if you have learned functions.
1
2
3
4
int highest_digit(int num)
{
  // your code here
}
You could turn each number into a string, and then loop over that string to find the highest digit.

Something like this:

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
#include <iostream>
#include <string>


int findHighestDigit(int input)
{
  std::string numberAsString = std::to_string(input);

  char highest = numberAsString [0];
  for (auto val : numberAsString)
  {
    if (val > highest)
    {
       highest = val;
     }
   }
return (highest-'0');
}

int main()
{
    std::cout << findHighestDigit (14 ) << '\n';
    std::cout << findHighestDigit (254 ) << '\n';
    std::cout << findHighestDigit (102 ) << '\n';
    std::cout << findHighestDigit (55 ) << '\n';
    std::cout << findHighestDigit (91) << '\n';
}



There are a few things in that you might not understand. If so, now's a good time to ask about them.
Last edited on
UPDATED CODE AGAIN ALMOST GOT IT BUT IT PRINTS ME ALL DIGITS INSTEAD OF THE HIGHEST ONE ! ! !

Okay so first things first I updated a little bit my code because the previous one was really messy and had unneccesarry ints. Both of you tried to help me and I see that it would work but we haven't learn using strings or as lastchance mentioned how he would do it.


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
#include <iostream>
#include <fstream>

//
const char duom[] = "duomenys.txt";
const char rez[] = "rezultatai.txt";
//

using namespace std;

int main()

{
    ifstream fd(duom);
    ofstream fr(rez);

    int n, sk, i, liek, did, maz, sksum;

    sksum = 0;
    did = 0;

    fd >> n;

    for (i = 1; i <= n; i++) {

        fd >> sk;

        while (sk > 0) {

            liek = sk % 10;
            sk /= 10;

            if (liek > sk || sk > liek)

                did = liek;
                fr << "Didziausias skaiciaus skaitmuo yra: " << did << endl;
        }

    }


    fd.close();
    fr.close();

    return 0;
}
Last edited on
Also, you're not being charged for letters. You can make your variable names as big as you like; it won't cost you any more. Someone reading your code (granted, I can't speak you mother tongue, so not me) should be able to understand it; making variable names meaningful is very helpful.

Also also, creating all your variables in a lump at the top is old-school C style. In C++, you can create them where it makes the most sense. Create them as you need them, next to code that uses them. This will make your code more readable and will help you structure your code.

if (liek > sk || sk > liek)
Could this be more clearly written as:
if (liek != sk)
because that's what it says. Is that what you meant?


1
2
    fd.close();
    fr.close();

This looks like someone is thinking in C. This is C++; while it's not wrong to close these streams when you need to, they will close by themself when they go out of scope. Writing these explicitly in your code above isn't wrong, but it's unnecessary and makes the reader wonder why you're doing it.
Last edited on
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
#include <iostream>
#include <fstream>

//
const char duom[] = "duomenys.txt";
const char rez[] = "rezultatai.txt";
//

using namespace std;

int main()

{
//   ifstream fd(duom);
//   ofstream fr(rez);
    istream &fd = cin;
    ostream &fr = cout;
    
    int n, sk, i, liek, maz;    // No did or sksum

    fd >> n;

    for (i = 1; i <= n; i++) {

        fd >> sk;
        maz = 0;                  // Initialise maximum to 0
        while (sk > 0) {

            liek = sk % 10;       // Get digit
            sk /= 10;
            
            if (liek > maz ) maz = liek;   // check whether to update maximum

        }    
        fr << maz << '\n';              // output maximum
    }    

//   fd.close();
//   fr.close();
    
    return 0;
}
It's still not working as it supposed to. It prints me all the numbers from the text file :
"
Didziausias skaiciaus skaitmuo yra: 4
Didziausias skaiciaus skaitmuo yra: 1
Didziausias skaiciaus skaitmuo yra: 4
Didziausias skaiciaus skaitmuo yra: 5
Didziausias skaiciaus skaitmuo yra: 2
Didziausias skaiciaus skaitmuo yra: 2
Didziausias skaiciaus skaitmuo yra: 0
Didziausias skaiciaus skaitmuo yra: 1
Didziausias skaiciaus skaitmuo yra: 1
Didziausias skaiciaus skaitmuo yra: 5
Didziausias skaiciaus skaitmuo yra: 1
Didziausias skaiciaus skaitmuo yra: 9
"
Didziausias skaiciaus skaitmuo yra --- Highest number's digit is : .... (Translated)

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
#include <iostream>
#include <fstream>

//
const char duom[] = "duomenys.txt";
const char rez[] = "rezultatai.txt";
//

using namespace std;

int main()

{
    ifstream fd(duom);
    ofstream fr(rez);

    int n, sk, liek, did; // n = how many numbers there are, sk = the numbers in the file, liek = reminder, did = highest,

    did = 0;

    fd >> n;

    for (int i = 1; i <= n; i++) {

        fd >> sk;

        while (sk > 0) {

            liek = sk % 10;
            sk /= 10;

            if (liek != sk)

                did = liek;
                fr << "Didziausias skaiciaus skaitmuo yra: " << did << endl;
        }

    }

    return 0;
}
1
2
3
   if (liek != sk)

                did = liek;


You are not testing that the digit is higher. You are testing that the digit is not the same. That's wrong.

You should be testing to see if the digit is higher.


Also, this indentation is suspicious:

1
2
3
4
       if (liek != sk)

                did = liek;
                fr << "Didziausias skaiciaus skaitmuo yra: " << did << endl;


That line, fr << "Didziausias skaiciaus skaitmuo yra: " << did << endl;, is NOT inside the if block. That line, fr << "Didziausias skaiciaus skaitmuo yra: " << did << endl;, will execute every time. Did you mean it to be inside the if block?
Last edited on
But I don't know how to check if it's higher already tried a lot of ways for it. Nothing seems to be working expect that I get all numbers printed but they are reversed :

"
Didziausias skaiciaus skaitmuo yra: 4
Didziausias skaiciaus skaitmuo yra: 1
Didziausias skaiciaus skaitmuo yra: 4
Didziausias skaiciaus skaitmuo yra: 5
Didziausias skaiciaus skaitmuo yra: 2
Didziausias skaiciaus skaitmuo yra: 2
Didziausias skaiciaus skaitmuo yra: 0
Didziausias skaiciaus skaitmuo yra: 1
Didziausias skaiciaus skaitmuo yra: 5
Didziausias skaiciaus skaitmuo yra: 5
Didziausias skaiciaus skaitmuo yra: 1
Didziausias skaiciaus skaitmuo yra: 9
"

41 = 14, 452 = 254, 201 = 102, 55 = 55, 19 = 91
if (liek > maz ) maz = liek; // check whether to update maximum
But I don't know how to check if it's higher already tried a lot of ways for it.


1
2
3
4
if (x > y)
{
   cout << "x is higher";
}

You can try it by yourself it's still not working even after updating it.

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
#include <iostream>
#include <fstream>

//
const char duom[] = "duomenys.txt";
const char rez[] = "rezultatai.txt";
//

using namespace std;

int main()

{
    ifstream fd(duom);
    ofstream fr(rez);

    int n, sk, liek, did; // n = how many numbers there are, sk = the numbers in the file, liek = reminder, did = highest,

    did = 0;

    fd >> n;

    for (int i = 1; i <= n; i++) {

        fd >> sk;

        while (sk > 0) {

            liek = sk % 10;
            sk /= 10;

            if (liek > did);
            
                did = liek;

                fr << "Didziausias skaiciaus skaitmuo yra: " << did << endl;
        }

    }

    return 0;
}
if (liek > did);

Where do you think that if block ends? Clue ... it's somewhere in that one-line snippet of code.
I don't understand. Am I missing "else" ? or brackets ?
Remove that trailing semicolon

if (liek > did);
               ^
               |
             there
Yeah I just realized that but it still gives me non sense answer. Btw why I was counting

1
2
3
4
        while (sk > 0) {

            liek = sk % 10;
            sk /= 10;


sk if I'm not using it anywhere ? And my

1
2

                fr << "Didziausias skaiciaus skaitmuo yra: " << did << endl;


prints 12 times instead of 5. (n = 5)
Last edited on
Your last question has already been answered by Repeater. Perhaps you should read the posts people make more carefully - and all the way through to the end?
What do you mean you aren't using sk? It's the number that you read from file and try to find the maximum digit of. If you wrote those lines surely you know what they are doing?

If you want the printout less often then move it outside the while loop. I've no idea what it means in a foreign language to me.

I gave you a perfectly good correction of your code earlier - just redirected input and output streams because I (obviously) don't have access to your files.
Topic archived. No new replies allowed.