String Largest Smallest

Having trouble figuring out how to pull the largest and smallest from my string. This is the code set-up I want with two functions and I got myself very confused! 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

#include <iostream>
using namespace std;

void findLargest();
void findSmallest();

int main()
{
	const int LENGTH = 100;
	char s[LENGTH];
	int total = 0, index;
	cout << "Enter a series of numbers in a row " << endl;
	cin >> s;
	for (index = 0; index < strlen(s); index++)
	{
		total += s[index] - '0';
	}
	cout << "The total is " << total << endl;
	cout << "The largest number is ";
	findLargest();
	cout << "The smallest number is ";
	findSmallest();
	return 0;
}
void findSmallest()
{

}

void findLargest()
{

}
 
hi.
1. read in the string using std::string (I don't think lines 15 to 18 are doing what you think they are). Look here:
http://www.cplusplus.com/doc/tutorial/basic_io/
2. These function signatures:
1
2
void findLargest();
void findSmallest();

are fairly useless in their current form. You'd want to pass in your array, and return an int (i.e. the number you want to display.
3. You don't actually need functions to do this, you can keep a track of the current smallest and largest as you're iterating through your string (each element in your string you'll have to convert to a number: http://www.cplusplus.com/reference/string/stoi/ ).
Last edited on
1
2
3
4
5
6
7
8
9
10
void findSmallest(string text)
{
     char smallestNum;
     for(int a = 0; a < sizeof(text);a++)
    {
           if(a == 1)  smallestNum = text[1];
           if(text[a] < smallestNum) smallestNum = text[a];
    }
    cout<<smallestNum;
}


I think this should do what you want. If you want to do it for largest then flip the < symbol on line 7.
Topic archived. No new replies allowed.