Finding highest and lowest values in a string

Nov 30, 2015 at 9:21pm
I am supposed to write a program that lets the user enter random numbers from the keyboard into a string and then the program should total all the numbers and output the total for instance if a user enters 1,2,3,4,5 then it would display the total as "15". I have it to where its correctly displaying the total of the numbers entered but I'm also supposed to display the maximum and minimum digits entered. I can't get this to work correctly because I don't know how to get it to only scan the entered numbers in the string, not the entire randomized string. Here is my code ... thanks in advance for any help!

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 <cstdlib>



int main()
{

    char numbers[100];
    int total = 0, minimum, maximum;


    std::cout << "Enter a series of numbers in a row " << std::endl;
    std::cin >> numbers;

    for (int i = 0; i < strlen(numbers); i++)
    {
        total += numbers[i] - '0';

        for (int i = 0; i < strlen(numbers); i++)
        {

            minimum = numbers[0];
            maximum = numbers[0];


            if(numbers[i] < minimum)
                minimum = numbers[i];

            if(numbers[i] > maximum)
                maximum = numbers[i];

        }

    }
    std::cout << "The total is: " << total << std::endl;
    std::cout << "The maximum number is: " << maximum << std::endl;
    std::cout << "The minimum number is: " << minimum << std::endl;
}

Nov 30, 2015 at 9:37pm
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
#include <iostream>
#include <cstdlib>



int main()
{

    char numbers[100];
    int total = 0, minimum, maximum;


    std::cout << "Enter a series of numbers in a row " << std::endl;
    std::cin >> numbers;

    minimum = numbers[0];
    maximum = numbers[0];

    for (int i = 0; i < strlen(numbers); i++)
    {
        total += numbers[i] - '0';
    }
        for (int i = 1; i < strlen(numbers); i++)
        {
            if(numbers[i] < minimum)
                minimum = numbers[i];

            if(numbers[i] > maximum)
                maximum = numbers[i];

        }
    std::cout << "The total is: " << total << std::endl;
    std::cout << "The maximum number is: " << maximum << std::endl;
    std::cout << "The minimum number is: " << minimum << std::endl;
}
Nov 30, 2015 at 9:52pm
When I compile and run the code you gave me it still outputs this:
(when i enter 1,2,3,4,5)

The total is: 15
The maximum number is: 53
The minimum number is: 49

My guess is that it's scanning the entire array and there are random numbers initialized to it since the user didn't enter all 100 spots themselves.
Dec 1, 2015 at 2:26pm
True, you need to count how many numbers the user enters.
Topic archived. No new replies allowed.