-
I'm stuck
I have a program to write and I have no idea on what to do. I need to be able to enter in a list of names like this:
Doe, John
White, Tim
Brown, Dan
etc...
then it has to come out like this:
John Doe
Tim White
Dan Brown
I have absolutely no idea how to do it.
-
Do you know how to read from the keyboard? Start there.
Quzah.
-
ill give u a little hint
create two arrays,
one array has the first input
put the last entry of the array into the first entry of the second array
out put the second array
-
char name[80];
printf("enter name: ");
fgets(name, sizeof(name), stdin);
Think of what you got to split the name - a space and a comma. Use them.
Check into strstr.
-
so...
strstr means string string right? I was thinking I'd have to use a swap in there...
So I have to have 2 arrays. I knew that...
But that is not my problem, don't get me wrong, I appreciate all the help so far. but I don't understand why I'd need to pull the name from a file... (fgets)
Um...
lets see... where else...
ah
I don't know what you mean about using the space and the comma.. Is that just using
scanf("%s", &variable);
or are you getting at something else?
-
>>to pull the name from a file... (fgets)
fgets don't take data from a file, it take a data from a stream and a stream can be a file, or even stdin (standard input).
>>scanf("%s", &variable);
you'll get some erros using this after sometimes you call it, also you don't need the '&' operator here if variable it's really a string.
-
fgets() gets from the keyboard by specifying the file as stdin. This is better than using scanf() or gets().
To determine the location of a comma in a string, use strchr() (as opposed to strstr() ).
-
We aren't supposed to use the input from an outside file. We "learned" about that yesterday. My teacher isn't really good at teaching. He knows 'C' way too well and feels that every one should be fluent in it. I think I could do it using scanf statements, but that would take a longer time and isn't what the program is being written for.
I'm beyond stuck... I get brainstorms but they all just fizzle into a little static shock. I'm gonna fail this stupid course...
-
This thread is getting far too long with nothing at all being done.
Code:
#include <stdio.h>
int main( void )
{
char buf[BUFSIZ] = {0};
printf("Enter a string:\n");
fgets( buf, BUFSIZ, stdin );
printf( buf );
return 0;
}
Like everyone has been saying, fgets is not only used for files. There, you do the rest.
Quzah.
-
I'm gonna go try that now. Thanks. I'll let you know if this works for me. BTW, the program we use for this is turbo C