No output in functions

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?

Would you at least tell us what it is you need help with?
Use return values:
1
2
3
4
5
6
7
8
int add(int x, int y) {
cout << x << " + " << y<< " = " << x+y;
return 0;
}// don't do this

int add(int x, int y) {
return (x+y)
}//do this 


then you can do this in main():
cout << add(6,8);

Or is that not what you meant?
Last edited on
Sorry, maybe this will help. Here is the last bit of the function (not all of it). He wants the cout statement to be in main.

1
2
3
4
5
6
7
8
9
  if (count > max)
  {
    max = count;
    ch = *start;
  }

}
cout << "The most common character is " << ch << " which appears " << max << " times.\n";
}


Here is main. It's pretty simple:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "tip.h"

const int SIZE = 35;

int main()
{
char sequence [SIZE];

cout << "Enter String:";
cin.getline(sequence, SIZE);

mostcommon(sequence, SIZE);

return 0;
}


Thanks.
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.
Last edited on
You could also return a std::pair<> of the values.
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/

A std::pair<T1, T2> is basically a struct of two data types. It is defined in the STL header <utility> and there is more information on it here:
http://www.cplusplus.com/reference/std/utility/pair/
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
//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";
   ...
}
Thanks!
Topic archived. No new replies allowed.