Okay, so I just started teaching myself C++ maybe a few days ago. Messed around with some basic input output things, I wanted to mess with characters and the input buffer to figure that out next.
I went to make a simple code that takes and converts the letters to the ANSI code then stops when it has cleared the input buffer. I spent a solid 8 hours getting it to this point but now I don't even know what to search for this one.
It works, it prints each letter in order and tells you its ANSI, but for some reason it always prints the last letter twice at the end. Can someone point me in the right searching direction?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
int main()
{
char ch;
do
{
std::cout << "Input a keyboard character: ";
std::cin >> ch;
std::cout << ch << " has ASCII code " << static_cast<int>(ch) << std::endl;
} while (std::cin.peek() > 0);
return 0;
}
|
I snagged the ASCI print out from a helpful tutorial, so I still don't fully understand why "static_cast<int> ()" does what it does, but I don't think that should effect it printing one more time after it realizes there isn't anything in the buffer. (I will look that up once this code is working right)
My understanding of this is that it should look at the buffer, if it has a letter it in, it sees a value so it starts over, but once it runs out of letters is sees a 0 or no value so it shouldn't run the "do" again right?