A simple exercise

I have a string with n elements and the exercise asks me to display the highest figure of each number.
The code I have wrote so far displays the first figure of the each number that has been introduced.
Please do not use more advanced stuff like functions,I'm a begginer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  int main()
{
    int n,max,c,i,nr;
    cout<<"How many numbers do you want to introduce?"<<endl;
    cout<<"nr=";cin>>nr;
    for(i=1;i<=nr;i++)
    {
        cout<<"n=";cin>>n;
        while(n)
        {
            c=n%10;
            n=n/10;
        }
        max=0;
        if(c>max)
        {
            max=c;
            cout<<c<<endl;
        }
    }
    return 0;
}
if(c>max)
you need to check this with the while() loop still running, currently you're checking after the while() loop has terminated and hence you're left with the first digit:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main()
{
int number{};
int maxDigit{};

std::cout << "Enter a number : \n";
std::cin >> number;//input validation required

while (number != 0)
{
    if ((number % 10) > maxDigit) //Remainder of number / 10
    {
        maxDigit = number % 10;
    }
    number /= 10;  //remove the last digit
}

std::cout << "The largest number was " << maxDigit << "\n";
}
Thank you @gunnerfunner
Topic archived. No new replies allowed.