C++ IO question

If I enter say 12 at the screen HW goes all the way to 38. Why doesn't end of line get detected after 2 when I hit the carrage return?

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){

string Hexnumber;
int Hexindex = 0;
const char EOL = '\n';

cout << "Entetr Hex Number" <<" ";
getline (cin, Hexnumber);

while (Hexnumber[Hexindex] != EOL)
{
cout << "HW = " <<Hexindex;

Hexindex = Hexindex + 1;
}
return 0;

}
getline() reads and discards the terminating character (endline in your case), it will never be present in the string it creates.

Note that since Hexnumber is a string, it knows how long it is: you can call Hexnumber.size() or Hexnumber.length() to decide when to stop the loop.

I was just thinking that was probably it.

Thanks
Topic archived. No new replies allowed.