Max and min in a string

My code runs but when it outputs the max it gives me 54 and the min is -52 which is not right.

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
#include <iostream>
#include <cstring>
using namespace std;


int main()
{  
	const int LENGTH = 100; //length of string
	char bob[LENGTH]; //string declaration
	int total=0, index; // initialize to zero
	int max = 0; //initialize max to random element
	int min =0; // initialize min to random element
	cout << "Enter a series of numbers in a row: "; // prompts user
	cin >> bob; // enter digits
	for (index=0; index < strlen( bob ); index++) // read through string
	{
		total += bob[index] - '0'; // add them together
	}
	cout << "The total is " << total << endl;
	for (index=0; index < LENGTH; index++)// read through string
	{
		if (bob[index] > max)// find max
		{
			max = bob[index];
		}
		else if (bob[index] < min) // find min
		{
			min = bob[index];
		}
	}
	cout << "The max is: " << max << endl << "The min is: " << min << endl ;
return 0;
}
cout << "Enter a series of numbers in a row: "; // prompts user
cin >> bob; // enter digits

this will only add 1 number to the first index,

cout << "Enter a series of numbers in a row: "; // prompts user
for (int i = 0; i < LENGTH; i ++)
{
cin >> bob[i];
}

since you are using char, if you do any type of operations to it, it will perform the operations to the ASCII code equivalent of the char, not what the value of the char represents, does this make sense?

yes but wouldn't that just give me an infinite loop
why would that give you an infinite loop?

a for condition can never be an infinite loop since you always specifiy the length, so in this case my loop says:

starting from 0 - to the length of your array
enter a digit for [0] =
[1]=
[2]=
etc
Yes but I'm not trying to enter 100 digits i just initialized length to a random number. The amount of digits the user enters is up to them.
ok then there are 2 ways to go about this:

ask the user before to enter how many values they would wish
cin >> size

and make a for loop with i < size;

or you can make a while loop, where;
bool found = false;
while (!found)
{
cin >> bob;
cout <<"do you want to continue? (y/n);
cin >> choice;
if (choice == y || choice == Y_
found = true;
Ok and this will fix my max and min problem.
Topic archived. No new replies allowed.