It gives me an error of char is incompatible with parameter type const char**
I'm not sure that error message matches your description.
In other words, if your program looks the way you say it looks, you might still get an error but I don't think it would be that one.
This small program compiles and runs fine, for example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
void add( constchar* text );
int main()
{
char my_str[] = "hello";
add( my_str );
return 0;
} // main
void add( constchar* text )
{
std::cout << text << "\n";
} // add
So, it WAS working, it would build correctly but still give me that error. But now I have a new problem
I have a problem entering in my strings. (I am using Microsoft VS).
I'm using a do-while loop to give the user options. on the first option I have two getlines that take user input.
char menuChoise; // char to take user menu choice
do
{
cout << "(A) Add Book" << endl;
cout << "(S) Sell Book" << endl;
cout << "(X)Exit" << endl;
cout << "Enter action: "; // prompts user for selection
cin >> menuChoise; // stores user input for acton.
switch (menuChoise)
{
case 'A':
{
char t[30]; // arrays to hold input
char a[20];
cout << "enter name: ";
cin.getline( t, 30, '\n'); // filled the array with user input.
cout << "enter type ";
cin.getline( a, 20, '\n');
add(t); // see first post.
} while ( menuChoise != 'X');
when I run the program I get this. Bold text is my input
"(A) Add Book"
"(S) Sell Book"
"(X)Exit"
"Enter action: " A *enter*
"enter name :" // doesn't let me enter anything
"enter type :" big
my problem is that my program skips any user entry for name. I think this has something to do with using the return key to enter. when I use the ending char as '\t' in the getline it lets me enter the name, however there is a new line as the first char in name.
my problem is that my program skips any user entry for name. I think this has something to do with using the return key to enter.
Correct.
cin >> menuChoise; // stores user input for acton.
That line extracts one char/letter from the input sequence and stores it in your menuChoise variable.
So the newline character that got stuffed into the input sequence when you 'entered' that previous value is still there. And that's what immediately is extracted when this line executes:
cin.getline( t, 30, '\n'); // filled the array with user input.
And because newline is your 'delimiter' it thinks you're already done :(
So you need to get that newline character out of your input sequence before asking the user for their name.
1 2
cin >> menuChoise; // stores user input for acton.
cin.ignore; // extract and throw away next character in input sequence
Regards, keineahnung
*Edit*
Actually this isn't a very robust solution. If your first input is more than one character followed by newline, it'll fail as before, because cin.ignore will remove only one extra character and leave the newline in place.