char NAME (void) {
//Allow character name input.
char Name[21]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,...
printf("\n\nPlease enter a name for your character.");
printf("\nThe name(s) should be 20 characters or less.");
gets(Name);
return Name[21];
}
How can I get gets() to halt for user input like scanf()?
Why doesn't this C function stop to allow the user to input their name?
To other answerers: If you don't know the answer, please don't respond with guesses. If you don't know C programming, don't answer with C++ code. Question asked for C help.
Now, to the asker: You're code is riddled with issues, probably because of poor quality tutorials and resources. There are some really good C books at accu.org worth looking at. Take a look at the book reviews and pick a recommended one. It's worth learning C properly.
Top to bottom:
"char NAME (void)"
Two issues already. The first is that you decided to return a char, while you actually want to create a whole name. Notice a name is a string meaning *many* characters. Note the plural. So you can eliminate char as a possible return type. Second, pick better names. NAME isn't the issue here, but it isn't a badge of honor either. I'm going to use getName in my example.
"char Name[21]={0,0,0,0,0,0,0,0,0,0,..."
Just do a char name[21]. Oh, and we'll change this later, but notice how char name[21] declares a variable on the stack. What happens when the function returns? The variable goes out of scope and the memory is deallocated.
"gets(Name);"
Perhaps you picked this up from a bad resource? gets is a broken function. You should use fgets.
"return Name[21];"
You cannot return multiple values. If you want to return a C string, you have a few options. The first is to take the address of a character buffer as an argument, and insert into that buffer. The second is to dynamically allocate a buffer, and return a pointer to that buffer. The third is a kludge, and is to return a struct. The struct itself is nothing more than a wrapper around a character buffer. I'll do the dynamic allocation in this one, although you probably should pass in a buffer pointer as an argument.
char* getName()
{
printf("\n\nPlease enter a name for your character.");
printf("\nThe name(s) should be 20 characters or less.");
char* name = malloc(21 * sizeof(name));
fgets(name, sizeof name, stdin);
return name;
}
Reply:Try putting an fflush(stdout) statement before gets. This happens sometimes because characters are in the buffer when gets() is called, so it takes those and runs with it. fflush should clear the buffer and cause gets() to wait for new contents.
Reply:To the answerers, but xanon: please stick to the question's point and don't post guesses nor day-dreams!.
To the poster: xanon explained everything correctly! Go for his solution.
Reply:using namespace std;
char NAME () {
char Name[21];
cout %26lt;%26lt; "\n\nPlease enter a name for your character." %26lt;%26lt; "\nThe name(s) should be 20 characters or less." %26lt;%26lt; endl;
cin %26gt;%26gt; Name;
cin.igonre();
return Name[21];
}
Reply:Try here for info
http://www.good-tutorials.com/
http://www.digitaljuice.com/
http://www.tutorialkit.com/
http://www.codestyles.com/
http://www.sitecube.com/website/promo_bw...
Reply:add "\n" at the end of the output string and don't press enter before adding ur input.
why u are returning the last char and what is {0,0,0,0,0,0,0,0,0,0,... use ZeroMemory or memset
Reply:Use cin and cout instead.
You dont need to specify what the char name array contains either;
like this:
char NAME(void)
{
char Name[21];
cout%26lt;%26lt; "Please enter a name for your charachter.\n");
cout%26lt;%26lt; "The name(s) should be 20 charachters or less\n");
cin%26gt;%26gt;Name;
}
Reply:I think there is some problem with the way u have declared ur function..i m not sure but can the name of the array and the name of the function be the same? and do arrays really take in name like that n insert it without using the for loop..just chk ur entire code again!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment