Sending "char" to "int" variable

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2008
Posts: 32
Reputation: salman213 is an unknown quantity at this point 
Solved Threads: 0
salman213 salman213 is offline Offline
Light Poster

Sending "char" to "int" variable

 
0
  #1
Nov 19th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 13
Reputation: gautam610 has a little shameless behaviour in the past 
Solved Threads: 3
gautam610 gautam610 is offline Offline
Newbie Poster

Re: Sending "char" to "int" variable

 
0
  #2
Nov 20th, 2008
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.............
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 77
Reputation: mahlerfive is an unknown quantity at this point 
Solved Threads: 16
mahlerfive mahlerfive is offline Offline
Junior Poster in Training

Re: Sending "char" to "int" variable

 
0
  #3
Nov 20th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 960
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Sending "char" to "int" variable

 
0
  #4
Nov 20th, 2008
If you're going to include the C++ headers, why not use cin and cout ?
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 32
Reputation: salman213 is an unknown quantity at this point 
Solved Threads: 0
salman213 salman213 is offline Offline
Light Poster

Re: Sending "char" to "int" variable

 
0
  #5
Nov 20th, 2008
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++?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 960
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Sending "char" to "int" variable

 
0
  #6
Nov 20th, 2008
I don't believe so. No reason for one.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,696
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 728
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Sending "char" to "int" variable

 
1
  #7
Nov 21st, 2008
>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' );
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC