Hi guys, how would I call a string that sits within a switch loop in the main function, into a separate function?
Like this:
1 2 3 4 5
void statistics()
{
cout << "Here are the statistics for your entry: " << endl;
cout << "\nThe size of your entry is " << s.length() << " characters." << endl;
}
I am calling string s from this:
1 2 3 4 5 6 7 8 9 10
switch (toupper(myChoice))
{
case'A': cin.ignore();
cout <<"\nPlease enter your text: ";
getline(cin, s);
cout << "\nYou entered: " << s << endl;
cout << endl << "(Press Enter key to continue)\n" << endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
myMenu();
break;
You need to give the function an argument and then pass the string to it. This sounds complicated, but luckily, it really isn't!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void statistics(string statsx) //That thing in the brackets is an argument.
//It means whenever we call statistics() we give it a string inside those brackets.
{
cout << "Here are the statistics for your entry: " << endl;
cout << "\nThe size of your entry is " << statsx.length() << " characters." << endl;
}
int main()
{
string s = "this is some data in a string.";
statistics(s);
return 0;
}
If you didn't quite understand that, here is another example with something simpler:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
using std::cout;
void n(int x)
{
cout << x;
}
int main()
{
int y = 5;
n(y);
return 0;
}
By George, you've got it! That was super simple, I realize now how simply confusing it is sometimes lol... I really appreciate the help, it means a lot!