| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Code:
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
cout << "Enter a line of input:\n";
char next;
do
{
cin.get(next);
cout << next;
} while ((! isdigit(next)) && (next != '\n'));
cout << "<END OF OUTPUT";
return 0;
}
My input: I'll see you at 10:30 AM. The Output: I'll see you at 1<End OF OUTPUT My question is, why didn't the code prevent the digit 1 from being displayed? Why was it able to pass through? How would you track this code? ![]() |
|
#2
|
|||
|
|||
|
This happens because you are doing cin.get(next);, and then checking after. So you've already stored and printed the character.
What you could do to fis this, is just switch cout << next; and cin.get(next); |
|
#3
|
||||
|
||||
|
Even nicer, and based on my sick use of for() loops for everything (really, it's the greatest function in C).
__________________
This is my code. Is it not nifty? "The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools." ---Douglas Adams Join the Itsacon fanclub! Zero Tolerance: Spammers banned so far: 280
![]() |
|
#4
|
|||
|
|||
|
Quote:
thanks destin. I get it now. ![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Why was it able to pass through? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|