| | |
Sending "char" to "int" variable
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2008
Posts: 32
Reputation:
Solved Threads: 0
Hi i have a question. Im using Dev C++ and this simple program takes in values of x and outputs *prints them on the screen. Although x should be an integer If i put in for example the letter "u" or any letter for that matter it goes on and on without asking me for anohter input and uses the last input on the screen. Why?
C Syntax (Toggle Plain Text)
#include <iostream> #include <stdio.h> using namespace std; main() { int x=0; while (true) { scanf("%d",&x); printf("%d", x); } }
Last edited by salman213; Nov 19th, 2008 at 10:12 pm.
•
•
Join Date: Aug 2008
Posts: 77
Reputation:
Solved Threads: 16
The above poster is not exactly correct.
When you call scanf(), if the function fails (which will happen when you try to read an integer but it is given a character), it will not actually consume the input character from the input. This means the next time you call scanf() it will end up trying to read the same character again!
To catch this, you can check the return value from scanf(). It tells you the number of inputs read successfully. When there is an error it should return less than the number of variables you wanted to read in (in this case, less than 1). If this happens, you need to flush the input stream. The easiest way to do this is just by calling fflush(stdin).
Apparently fflush() is a "bad practice", but in small programs it should be fine. There are other "better" ways to deal with this.
When you call scanf(), if the function fails (which will happen when you try to read an integer but it is given a character), it will not actually consume the input character from the input. This means the next time you call scanf() it will end up trying to read the same character again!
To catch this, you can check the return value from scanf(). It tells you the number of inputs read successfully. When there is an error it should return less than the number of variables you wanted to read in (in this case, less than 1). If this happens, you need to flush the input stream. The easiest way to do this is just by calling fflush(stdin).
Apparently fflush() is a "bad practice", but in small programs it should be fine. There are other "better" ways to deal with this.
>im actually programming in C
Then keep in mind that C89 doesn't support the <iostream> header, namespaces, or the true keyword. Your code won't compile as C, and failing to return a value from main is undefined behavior.
>wat is the equivalent statement in C++?
I thought you were programming in C.
Anyway, the equivalent statement in C++ is
And the equivalent C code uses a loop:
Which can also be done in C++:
Then keep in mind that C89 doesn't support the <iostream> header, namespaces, or the true keyword. Your code won't compile as C, and failing to return a value from main is undefined behavior.
>wat is the equivalent statement in C++?
I thought you were programming in C.
fflush(stdin) . It's equally broken in both languages, I strongly recommend you forget that it happens to work on your compiler. A more correct C++ solution is istream::ignore: C Syntax (Toggle Plain Text)
#include <iostream> #include <ios> #include <limits> std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
c Syntax (Toggle Plain Text)
#include <stdio.h> int ch; do ch = fgetc ( stdin ); while ( ch != EOF && ch != '\n' );
C Syntax (Toggle Plain Text)
#include <iostream> char ch; do std::cin.get ( ch ); while ( std::cin && ch != '\n' );
New members chased away this month: 3
![]() |
Other Threads in the C Forum
- Previous Thread: External Delaration for An Array?
- Next Thread: Malloc a struct
| Thread Tools | Search this Thread |
Tag cloud for C
#include adobe ansi array arrays asterisks binarysearch calculate centimeter changingto char convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi






