Group,
I am teaching myself c++. I have been using a variety of sources and have landed on C++ Primer. Very good, easy to follow.
I am having a problem making one of the example programs work. The code from page 95 is below. I am sure I have copied it correctly.
It works if I take out the while statement and put in
cin << n;
instead. That gives an answer of
Your hex number is: C
for an input of 12.
The program with the while statement is suppose to give an answer of:
Your hex number is: C05F8F
for an input of: 12 0 5 15 8 15
This is the third printing of this book so I am assuming that there is no typo. This book does have an annoying habit of using code from previous paragraphs as part of an example that they are talking about. But I have been able to figure most of that out.
The question is what am I missing here?
Thank you in advance for your help,
Dan
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main () {
const string hexdigits = "0123456789ABCDEF";
cout << "enter a series of numbers between 0 and 15"
<< " separated by spaces. Hit ENTER when finished: " << endl;
string result;
string::size_type n;
while (cin >> n)
if (n < hexdigits.size())
result += hexdigits[n];
cout << "Your hex number is; " << result << endl;
}
|