Here's my program:
#include %26lt;iostream%26gt;
#include %26lt;iomanip%26gt;
using namespace std;
void main ()
{
 char name_list[9];  //10 names max
 
 cout%26lt;%26lt;"Enter Name: ";
 cin.get(name_list[0], 100);
 cout%26lt;%26lt;endl;
  
}
Here's the error:
error C2664: 'class std::basic_istream%26lt;char,struct std::char_traits%26lt;char%26gt; %26gt; %26amp;__thiscall std::basic_istream%26lt;char,struct std::char_traits%26lt;char%26gt; %26gt;::get(char *,int)' : cannot c
onvert parameter 1 from 'char' to 'char *'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
------
I'm pretty new to C++ and I know this must be something very basic about syntax or C-String.  I DON'T want to replace the c-string array with a "string" (i.e.: string name_list[] instead of char name_list[])
I've been working on this all day - been trying to find help online online, but this seems to be too simple of a mistake that it's not online.
C++: Adding user inputs into a C-String Array?
3paul is correct.  It would be much easier if you simply use the Standard Library class 'string'.  I don't understand why you don't want to use it, unless your instructor is forbidding it.  I'm also confused with your term C-String.  It sounds like the MFC class CString (which you should not be using).
If you do use the Standard Library class string, you can simply declare an array of string:
string name_list[10];
// your stuff
cin %26gt;%26gt; name_list[0];
// etc.
Even better, if you use the template vector, you can have a variable sized array:
vector%26lt;string%26gt; name_list;
string input;
do {
cout %26lt;%26lt; "Enter name (blank to stop)\n";
cin %26gt;%26gt; input;
if (!input.empty())
name_list,push_back(input);
}
while (!input.empty());
Reply:char name_list[9]; does not create an array of 10 character strings.  It's an array of 9 (not 10)  characters. Period.  So when you reference name_list[0] in the cin.get call, you're referencing a char, not a char*.
Now, char* name_list[9] would create an array of nine character pointers, but they wouldn't automatically point to character strings either.  That's more detail you'd have to work out.
Hope that helps some.
Reply:You have name_list as something like a "string" right now instead of an array of "strings."  Try char name_list[9][100].  
But you can just do this with std::string which would make your life much simpler, and you're using the newer standard library stuff anyway so shouldn't make much of a difference in overhead.  You'd probably prefer to just go that route.
Subscribe to:
Post Comments (Atom)
 
No comments:
Post a Comment