My instructor doesn't want any outputs in the functions (ex. cout). He wants everything output in main. I was taught differently last semester (main and functions were all in one program, not split into many with a .h file) and I won't get a chance to ask him before my program is due. Can anyone help?
Since you need to get two values from the function, you can't use return to get both of them. What you can do is declare two variables in main (they could be called "ch" and "max" if you want), and pass them by reference to the function. Use these variables in your function just like the "max" and "ch" ones you currently have. Then you can access these values in main once your "mostcommon" function ends. Hope that helps.
I'm not sure I follow you on pass by reference variables. I've used them in function header/protoypes, but not just as variables by themselves. Am I misunderstanding what your saying? I also have no idea what std::pair<> is. We haven't talked about anything like that yet. This is just the 2nd semester of our first programming class. Sorry if I seem dumb.
When you pass by reference, any modifications you make to that parameter inside the function will affect the variable outside the function. Sometimes functions use this as a way to return more than 1 value. More info here: http://www.cplusplus.com/doc/tutorial/functions2/
//Define a structure to hold all the info you need from the function. Example:
struct CharMode {
char mode;
int howMany;
};
//Define the function to return one of these structures.
CharMode mostcommon(char seq[], int size) {
//Do whatever the function did before,
...
//but instead of printing the value(s),
// cout << "The most common character is " << ch << " which appears " << max << " times.\n";
//return them via your structure...
CharMode retVal;
retVal.mode = ch;
retVal.howMany = max;
return retVal;
}
int main() {
...
//Now call the function, and store it's return value
CharMode answer = mostcommon(sequence, SIZE);
// and then print the info.
cout << "The most common character is " << answer.mode << " which appears " << answer.howMany << " times.\n";
...
}