Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16949

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Problems with user input functions Posted by Fuzedmind on 22 Mar 2010 at 5:29 PM
Hey guys, I am using scanf() to input values for a structure and it does this weird thing where it will "skip" input lines. I have no idea why this is happening. Here is my code:

printf("Name: ");
scanf("%s,new_patient.name);

I have about 9 input statements just like this one. They are one after another with combination of string and integer inputs. I don't feel like typing them all out. It's supposed to be input for a patient being admitted to a hospital.

Heres my output:

Name: Lawrence Aiello
Date of birth: Date of your last physical:
December 3
Height (in inches): Weight (be honest!):

See how it skips date of birth and height? I need help fixing this I have no idea what to do. Thanks in advance.
Report
Re: Problems with user input functions Posted by anthrax11 on 22 Mar 2010 at 7:40 PM
Probably the format string is wrong. The best thing you can do is just copy the relevant code here and put it between code tags.
Report
Re: Problems with user input functions Posted by freedom_doc on 11 Apr 2010 at 4:34 PM
scanf is somewhat problematic in this type of thing, especially where you are mixing different types of inputs. But the first thing you have to realize is that scanf STOPS on white space. So if you have

scanf("%s", name); where 'name' is a string and in response you put
Douglas White

then name will only be Douglas.
White would then go to feed other inputs later on.

You should use fgets instead of scanf. It does NOT stop on white space.
But it only inputs into a string. Use it like

fgets(string, 100, stdin);

This reads chars into 'string' up to a max of 100 characters, or until EOL or EOF (end of line or end of file). If you need integer
or floating point values from this use
double x;
int n;
n = atoi(string);
or
x = atof(string);

Be sure to #include <stdlib.h> and <stdio.h> for these.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.