DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   Sending "char" to "int" variable (http://www.daniweb.com/forums/thread158436.html)

salman213 Nov 19th, 2008 10:11 pm
Sending "char" to "int" variable
 
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?

#include <iostream>
#include <stdio.h>
using namespace std;
main()
{
int x=0;
while (true)
{
scanf("%d",&x);
printf("%d", x);
}

}

gautam610 Nov 20th, 2008 1:12 am
Re: Sending "char" to "int" variable
 
since you declare x as an integer so system allocate 2 byte for this variable & when u give input as character it converts into an integer through ASCII value & print that one.............

mahlerfive Nov 20th, 2008 1:32 am
Re: Sending "char" to "int" variable
 
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.

MosaicFuneral Nov 20th, 2008 1:36 am
Re: Sending "char" to "int" variable
 
If you're going to include the C++ headers, why not use
cin
and
cout
?

salman213 Nov 20th, 2008 7:30 pm
Re: Sending "char" to "int" variable
 
By the way im actually programming in C for something else thats why im using it and not Cin/cout.

AND THANKS


fflush(stdin);

seems to work,
just a question, wat is the equivalent statement in C++?

MosaicFuneral Nov 20th, 2008 8:22 pm
Re: Sending "char" to "int" variable
 
I don't believe so. No reason for one.

Narue Nov 21st, 2008 9:15 am
Re: Sending "char" to "int" variable
 
>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. :icon_rolleyes: Anyway, the equivalent statement in C++ is
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:
#include <iostream>
#include <ios>
#include <limits>

std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
And the equivalent C code uses a loop:
#include <stdio.h>

int ch;

do
  ch = fgetc ( stdin );
while ( ch != EOF && ch != '\n' );
Which can also be done in C++:
#include <iostream>

char ch;

do
  std::cin.get ( ch );
while ( std::cin && ch != '\n' );


All times are GMT -4. The time now is 11:40 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC