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?
#include <iostream>
usingnamespace std;
void findLargest();
void findSmallest();
int main()
{
constint 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/ ).