![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 28
| Beginner Question: Problem with scanf and strings. Help! Anyway, my real question is when I run this program (below) it lets me type in my first name, but then when I press Return it skips over letting me enter my last name and shows this: Code: What is your FIRST name: Bryan What is your LAST name: Your FULL name is Bryan and it is 6 long. Code: #include <stdio.h>
#include <string.h>
int main (int argc, const char * argv[]) {
char firstname[31];
char lastname[31];
char fullname[63];
printf( "What is your FIRST name: " );
scanf( "%[^\n]", firstname ); // accepts everything until it sees a \n
printf( "What is your LAST name: " );
scanf( "%[^\n]", lastname ); // accepts everything until it sees a \n
strcpy( fullname, firstname );
strcat( fullname, " " );
strcat( fullname, lastname );
printf( "Your FULL name is %s and it is %d long.", fullname, strlen(fullname) );
return 0;
}
|
| lucidrave is offline | |
| | #2 |
| Jack of many languages Join Date: Nov 2007 Location: Katy, Texas
Posts: 1,929
| See this link for information about scanf(): scanf [C++ Reference] In short, the characters between the brackets, ^\n, tells scanf() to suck in everything EXCEPT a newline character. The NEWLINE character is left in the input buffer. When the next scanf() is issued, it reads everything up to the NEWLINE, which is nothing, and returns immediately. If you use the bracket notation, you'll next an extra line of code to remove the NEWLINE from the input buffer.
__________________ Mac and Windows cross platform programmer. Ruby lover. Memorable Quotes From Recent Posts: I can't remember. |
| Dino is offline | |
| | #3 | ||
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| Quote:
Quote:
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | ||
| MK27 is online now | |
| | #4 |
| Registered User Join Date: Jul 2009
Posts: 28
| I figured using a fflush(stdin); would clear the buffer but it doesn't seem to make a difference... I'm clueless how to get this working as expected Code: #include <stdio.h>
#include <string.h>
int main (int argc, const char * argv[]) {
char firstname[31];
char lastname[31];
char fullname[63];
printf( "\nWhat is your FIRST name: " );
scanf( "%[^\n]", firstname );
fflush(stdin); // this doesn't seem to work
printf( "\nWhat is your LAST name: " );
scanf( "%[^\n]", lastname );
strcpy( fullname, firstname );
strcat( fullname, " " );
strcat( fullname, lastname );
printf( "\nYour FULL name is %s and is %d characters long.", fullname, strlen(fullname) );
return 0;
}
|
| lucidrave is offline | |
| | #5 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| Never use fflush(stdin) -- it is not defined by the C standard: Quote:
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is online now | |
| | #6 |
| Registered User Join Date: Jul 2009
Posts: 28
| thanks ohh ok... so it means ignore any characters after it sees the new line? (in this code anyway) |
| lucidrave is offline | |
| | #7 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| Quote:
mk27\n (the newline being because of the enter key), and you capture everything up to but not including the newline with %[^\n], '\n' is left in the buffer. %*c captures one more character and throws it away; because of the context, in this case that character would have to be the newline (get it?).
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is online now | |
| | #8 | |
| Registered User Join Date: Jul 2009
Posts: 28
| I guess what's confusing me is I thought %[^\n] was to ignore the new line, but then %*c also ignores the new line. isn't that redundant? if %[^\n] doesn't work by itself should I not use it? Also is the * part of %*c acting as a "wildcard"? It's a bit confusing know what purpose it serves since it could be multiply (which wouldn't make any sense in this case) or referencing a pointer. Quote:
| |
| lucidrave is offline | |
| | #9 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| As mentioned, %[^\n] means "get everything that isn't a newline". If you do that, well, the newline is still there since you got everything that wasn't a newline. You need to now do something with that newline, instead of what you seem to be doing which is "ignore it and hope that it goes away". |
| tabstop is offline | |
![]() |
| Tags |
| beginner, scanf, strcat, strcpy, weird |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Strings in Scanf function | dcwang3 | C Programming | 21 | 09-11-2008 03:47 AM |
| scanf for strings? | pobri19 | C Programming | 7 | 05-31-2008 03:47 AM |
| scanf problem | a1dutch | C Programming | 8 | 04-19-2005 04:14 AM |
| Reading strings input by the user... | Cmuppet | C Programming | 13 | 07-21-2004 06:37 AM |
| Problem with scanf input | ejpjoker | C Programming | 4 | 04-29-2004 08:28 PM |