C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-11-2009, 08:28 PM   #1
Registered User
 
Join Date: Jul 2009
Posts: 28
Beginner Question: Problem with scanf and strings. Help!

First off how does scanf know that this: %[^\n] is supposed to be formatted for a String? Shouldn't %s need to be in there? According to the syntax it's not supposed to be.

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   Reply With Quote
Old 08-11-2009, 08:41 PM   #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   Reply With Quote
Old 08-11-2009, 08:53 PM   #3
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,946
Quote:
scanf( "%[^\n]", firstname ); // accepts everything until it sees a \n
Yep. Now check your docs for this angle:

Quote:
scanf( "%[^\n]%*c", firstname );
Or, you could just use %s, and then, as Dino is trying to say, remove the '\n' from your string if desired.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is online now   Reply With Quote
Old 08-11-2009, 08:56 PM   #4
Registered User
 
Join Date: Jul 2009
Posts: 28
Question ok, i tried adding this (still doesn't work)

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   Reply With Quote
Old 08-11-2009, 09:01 PM   #5
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,946
Quote:
Originally Posted by lucidrave View Post
I figured using a fflush(stdin); would clear the buffer
Never use fflush(stdin) -- it is not defined by the C standard:
Quote:
[#2] If stream points to an output stream or an update
stream in which the most recent operation was not input, the
fflush function causes any unwritten data for that stream to
be delivered to the host environment to be written to the
file; otherwise, the behavior is undefined.
Read my previous post; the "*" flag means ignore. In the context of your scanf line, that %c will be the newline.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is online now   Reply With Quote
Old 08-11-2009, 09:03 PM   #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   Reply With Quote
Old 08-11-2009, 09:34 PM   #7
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,946
Quote:
Originally Posted by lucidrave View Post
ohh ok... so it means ignore any characters after it sees the new line?
(in this code anyway)
No, it means ignore the newline, which is a single character ('\n'). So if you typed:

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   Reply With Quote
Old 08-11-2009, 10:14 PM   #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:
Originally Posted by MK27 View Post
No, it means ignore the newline, which is a single character ('\n'). So if you typed:

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?).
lucidrave is offline   Reply With Quote
Old 08-11-2009, 10:22 PM   #9
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by lucidrave View Post
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?
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   Reply With Quote
Reply

Tags
beginner, scanf, strcat, strcpy, weird

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:21 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22