passing cstring into function?
I am trying to pass the created c string into a get c string function. the get string function is at the bottom
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
#include <iostream>
#include <cstring>
using std::cout;
using std::cin;
using std::endl;
int printMenu();
char getString(char cString);
const int LENGTH = 25;
int main()
{
char cString[LENGTH] = "0";
int choice = 0;
while (choice != 12)
{
choice = printMenu();
switch (choice)
{
case 1:
getString(cString);
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
case 10:
break;
case 11:
break;
case 12:
cout << endl << "Exiting program . . ." << endl;
break;
default:
cout << "Invalid input" << endl;
break;
}
}
return 0;
}
int print_menu()
{
int choice = 0;
do
{
cout << "***Menu***" << endl << "1. Get a cString" << endl << "2. Copy one cString into another (strcpy)" << endl << "3. Concatenate one cString onto the Original cString (strcat)" << endl
<< "4. Finds the length of a cString (strlen)" << endl << "5. Finds the substring in a cString (strstr)" << endl << "6. Reverse a cString (strrev)" << endl
<< "7. Case insensitive cString compare(stricmp)" << endl << "8. Delete a Portion of the String" << endl << "9. Insert another String into the First String" << endl
<< "10. Create a substring" << endl << "11. Find Index of Sub-String" << endl << "12. Exit" << endl << endl;
cin >> choice;
if (choice > 12 || choice < 1)
{
cout << "Invalid input" << endl;
}
} while (choice > 12 || choice < 1);
return choice;
}
char getString(char cString)
{
cout << "Please enter a cString (max length 25)" << endl;
cin >> cString;
return cString;
}
|
Last edited on
Well the mistake is quite obvious. Your function takes a char, you're giving it a char array.
I emphatically suggest using C++ standard strings for this kind of stuff.
If you want to go this route instead, your getString function will need to receive a pointer to char, which will point to the beginning of an array.
Topic archived. No new replies allowed.