Program:
#include <iostream> #include <string> using std::cin; using std::cout; using std::endl; using std::string; int main(){ double x; string name; while(cin >> name){ cout<<name<<endl; while (cin >> x); cin.clear(); } }
Input:
Moo 100 100 100 100 100 100 100 100
Moore 75 85 77 59 0 85 75 89
Norman 57 78 73 66 78 70 88 89
Western 43 98 96 79 100 82 97 96
Edwards 77 72 73 80 90 93 75 90
Franklin 47 70 82 73 50 87 73 71
Jones 77 82 83 50 10 88 65 80
Carpenter 47 90 92 73 100 87 93 91
Output:
Moo
Moore
Orman
Westerly
wards
ranklin
Jones
rpenter
I started working through the accelerated C ++ book and came across a part of the authors code (chapter 4) that did not work properly on my machine (the above example is shortened, but it is experiencing the same problem as the code directly from the book). The goal is to print all names from the beginning of a string of numbers representing student grades at the input. What actually happens is that some names are printed completely, while others, apparently randomly, skip letters from the very beginning. I was hoping that someone here could shed light on why this is happening.
My initial thought was that in the process of crashing when non-numeric input is reached, the line:
while (cin >> x);
somehow upturned the beginning of the next name, but I'm having difficulty with it, and I'm not sure why this should happen.
Update. I seem to be having the same issue as this question with the clang compiler for mac.
source share