I've got a very simple program that just takes two unsigned int
inputs, and then goes through a for loop reading out each number below 9 as:
input 1 : output one
input 2 : output two
etc. and above 9 it outputs whether it is even or odd.
All goes fine except for when I call std::flush
, it does not flush the buffer to the screen, however std::endl
does. With the code as it is below, I get no output, however by changing flush
to endl
I do get an output. Why is this? I tried looking it up and all I could find slightly relevant was this Calls to flush cout are ineffective which said the problem was that a /r
was being inserted, but I can't see how that could be the case here since I can see all the outputs don't have that.
#include <iostream>
enum integerState{belowTen,aboveNine};
enum parity{even,odd};
integerState process_integer(unsigned int n);
parity even_or_odd(unsigned int n);
int main(){
unsigned int lowerLim,upperLim;
std::cin >> lowerLim >> upperLim;
for (unsigned int i=lowerLim;i<=upperLim;++i){
process_integer(i);
}
return 0;
}
integerState process_integer(unsigned int n){
switch(n){
case 1: std::cout << "one" << std::flush; return belowTen;
case 2: std::cout << "two" << std::flush; return belowTen;
case 3: std::cout << "three" << std::flush; return belowTen;
case 4: std::cout << "four" << std::flush; return belowTen;
case 5: std::cout << "five" << std::flush; return belowTen;
case 6: std::cout << "six" << std::flush; return belowTen;
case 7: std::cout << "seven" << std::flush; return belowTen;
case 8: std::cout << "eight" << std::flush; return belowTen;
case 9: std::cout << "nine" << std::flush; return belowTen;
default:
if(even_or_odd(n)==even){ std::cout << "even" << std::flush; return aboveNine;}
if(even_or_odd(n)==odd){ std::cout << "odd" << std::flush; return aboveNine;}
return aboveNine;
}
}
parity even_or_odd(unsigned int n){
return (n%2==0 ? even:odd);
}
I'm using g++ if that matters.