K, I'm a TOTAL n00b(just started yesterday) to C, so excuse my ignorance!
When trying to use this code:
to get user input, it only grabs the first word! How would I grab all the words inputed? Thanks :)Code:scanf("%s", stuff);
Printable View
K, I'm a TOTAL n00b(just started yesterday) to C, so excuse my ignorance!
When trying to use this code:
to get user input, it only grabs the first word! How would I grab all the words inputed? Thanks :)Code:scanf("%s", stuff);
The best way would be to avoid scanf entirely. I would use fgets to capture everything that the user typed, then use strtok to split the string up into individual words, based on the assumption that the user will put spaces between the words!
You're best off looking up how to use these functions yourself (they are in the stdio.h and string.h libraries) in order to learn how they work, but if you get stuck just show us what you tried!
Ok, thanks for the help! :)
So were I am suppose to look up how to use them? In the files stdio.h and string.h? I looked at them and it all went in one side and straight out the other LOLQuote:
Originally posted by Morgan
You're best off looking up how to use these functions yourself (they are in the stdio.h and string.h libraries) in order to learn how they work, but if you get stuck just show us what you tried!
Ok, look at these examples...
Code:#include <stdio.h>
#include <string.h>
int main()
{
char answer[100], *p;
printf( "\nType something:\n" );
fgets( answer, sizeof( answer ), stdin );
if( ( p = strchr( answer, '\n' )) != NULL )
*p = '\0';
printf( "\nYou typed \"%s\"\n\n", answer );
return 0;
/* NOTE: strchr() returns a pointer to the first occurrence of the
** specified character in the specified string. strrchr() returns a pointer
** to the last specified character.
** In this case, we point p at the newline which fgets() automatically includes
** and then change it to be a proper string terminator i.e. '\0'.
*/
}
I didn't write the first one myself, but it's pretty standard code.Code:
#include <stdio.h>
#include <string.h>
int main()
{
char sentance[100], *word;
sprintf( sentance, "Just a few words" );
word = strtok( sentance, " " );
printf( "\n%s", word );
while( ( word = strtok( NULL, " " )) != NULL )
printf( "\n%s", word );
printf( "\n\n" );
return 0;
}
Just mix and match!!
>So were I am suppose to look up how to use them?
The FAQ would be a good place to start.
FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
FAQ > How do I... (Level 1) > How do I get a number from the user (C)
FAQ > Other references > Misc reference sites > man pages
Or you can just do this:
With this you can also limit the number of chars:Code:char string[100];
scanf("%[^\n]", string); //continue to get input, until an ENTER is pressed
Code:char string[100];
scanf("%99[^\n]", string); // allows you to type in up to 99 chars
Hello,I'm not sure why you guys have your scanf fucntion without the address operator
scanf("%s", &var);
I was referring to some parts of the code you have in the question, the scaf doesn't have the & operator, is that ok or what?
Just a thought...just my thought...
Hello,I'm not sure why you guys have your scanf fucntion without the address operator
scanf("%s", &var);
I was referring to some parts of the code you have in the question, the scaf doesn't have the & operator, is that ok or what?
Just a thought...just my thought...
Yes, it is correct without the &, because var is an array.Quote:
Originally posted by dalguy2004
Hello,I'm not sure why you guys have your scanf fucntion without the address operator
scanf("%s", &var);
I was referring to some parts of the code you have in the question, the scaf doesn't have the & operator, is that ok or what?
In the call to scanf, buffer "decays" into a pointer to the first element (it is the same as &buffer[0]). You want to pass to scanf a pointer to char. With the "decay", buffer does just that.Code:char buffer[20];
scanf("%s", buffer);
It is not correct to use &buffer here because its type is pointer to array-of-char. It may be the same place in memory, but the expressions buffer (&buffer[0]) and &buffer do not have the same type.
Hello Dave,
Thanks so much for pointing that out. My eyes are wide open now..