943,840 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 916
  • C RSS
Nov 19th, 2008
0

Sending "char" to "int" variable

Expand Post »
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?

  1. #include <iostream>
  2. #include <stdio.h>
  3. using namespace std;
  4. main()
  5. {
  6. int x=0;
  7. while (true)
  8. {
  9. scanf("%d",&x);
  10. printf("%d", x);
  11. }
  12.  
  13. }
Last edited by salman213; Nov 19th, 2008 at 10:12 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
salman213 is offline Offline
32 posts
since Jun 2008
Nov 20th, 2008
0

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.............
Reputation Points: 5
Solved Threads: 3
Newbie Poster
gautam610 is offline Offline
13 posts
since Jul 2008
Nov 20th, 2008
0

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.
Reputation Points: 33
Solved Threads: 18
Junior Poster in Training
mahlerfive is offline Offline
77 posts
since Aug 2008
Nov 20th, 2008
0

Re: Sending "char" to "int" variable

If you're going to include the C++ headers, why not use cin and cout ?
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Nov 20th, 2008
0

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++?
Reputation Points: 10
Solved Threads: 0
Light Poster
salman213 is offline Offline
32 posts
since Jun 2008
Nov 20th, 2008
0

Re: Sending "char" to "int" variable

I don't believe so. No reason for one.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Nov 21st, 2008
1

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. 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:
  1. #include <iostream>
  2. #include <ios>
  3. #include <limits>
  4.  
  5. std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
And the equivalent C code uses a loop:
  1. #include <stdio.h>
  2.  
  3. int ch;
  4.  
  5. do
  6. ch = fgetc ( stdin );
  7. while ( ch != EOF && ch != '\n' );
Which can also be done in C++:
  1. #include <iostream>
  2.  
  3. char ch;
  4.  
  5. do
  6. std::cin.get ( ch );
  7. while ( std::cin && ch != '\n' );
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: External Delaration for An Array?
Next Thread in C Forum Timeline: Malloc a struct





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC