View Single Post
Join Date: Sep 2004
Posts: 7,540
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: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Sending "char" to "int" variable

 
0
  #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