64683

Trouble with input using scanf()

Question:

I'm new to C so I'm having a bit of trouble with scanf().

#include <stdio.h> #include <stdlib.h> int main() { int height; int weight; printf("Enter height in inches:\n"); scanf("%d", &height); printf("Enter weight in pounds:\n"); scanf("%d", &weight); printf("You are %d inches tall and weigh %d pounds.", height, weight); return 0; }

I'm pretty sure this is the correct syntax but when I run it, it shows an empty screen and after I input 2 numbers it shows:

64

120

Enter height in inches:

Enter weight in pounds:

You are 64 inches tall and weigh 120 pounds.

According to some tutorials, it's supposed to display "Enter height in inches:" before I input the 1st number and "Enter weight in pounds:" before I input the 2nd number. Please help me!

<strong><em>I'm using Eclipse to write my programs and MinGW as the compiler if that's relevant.</em></strong>

Answer1:

Its a <a href="http://www.https://bugs.eclipse.org/bugs/show_bug.cgi?id=173732" rel="nofollow">bug in eclipse</a> and this has been reported by most of the people using eclipse and MinGW.

To overcome this problem, you could use fflush(stdout) after every call to printf or use the following in the start of main :

setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0);

This will cause stdout and stderr to flush immediately whenever it is written to.

Answer2:

Try using scanf_s instead of scanf , i just ran it and it worked.

Answer3:

I would suggest you to user sscanf insted of scanf and add fgets to the top would be more logic.

It would be:

int height; char buffer[32]; /*Add this to collect you data*/ printf("Enter height in inches:\n"); fgets(buffer,sizeof(buffer),stdin); /*Add this line to get value from keyboard*/ sscanf(buffer,"%d", &height); /*Change scanf to sscanf*/

and same of the other one

Answer4:

I suggest usying Putty if on Windows or command line on Mac OSX. Use "Wal Werror" compiler

Yes, you need to wait for it to display the output before enterning number.

E.g. you run the program When this line appears

Enter height in inches:

then enter

64

then when this appears

Enter weight in pounds:

Then enter

120

then output should be as expected

Recommend

  • Custom Keyboard or to create own keyboard
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • extjs4 catching the scroll event on panel
  • Facebook iOS SDK Not Calling Completion Handler
  • there is no graph with tensorboard
  • How to make R's read_csv2() recognise the text characters properly
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • C: Incompatible pointer type initializing
  • Jackson Parser: ignore deserializing for type mismatch
  • Word Open XML Mail Merge
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Record samples being played with OpenAL
  • Typescript - Unable to get 'import' statement to function
  • FFmpeg Conversion Error
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Is there a javascript serializer for JSON.Net?
  • Why is an OPTIONS request sent to the server?
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Splitting given String into two variables - php
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • Controls, properties, events and timers running in design time
  • Perl system calls when running as another user using sudo
  • Where to put my custom functions in Wordpress?
  • Updated Ionic CLI but shows previous version (Windows)
  • ActionScript 2 vs ActionScript 3 performance
  • what is the difference between the asp.net mvc application and asp.net web application
  • Build own AppleScript numerical error handling
  • To display the title for the current loaction in map in iphone
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • Compare two NSDates in iPhone
  • Traverse Array and Display in markup
  • Matrix multiplication with MKL
  • Buffer size for converting unsigned long to string
  • How to set the response of a form post action to a iframe source?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Binding checkboxes to object values in AngularJs