tags:

views:

62

answers:

4

Here is the code I have at the moment:

char ch;
int sum = 0;
double values[10];
int i = 0;
cin >> ch;
    while (!isalpha(ch))
    {  
        values[i] = ch; 
        sum += values[i]; 
        i++;  
        cin >> ch;
    }

What is happening is that if I enter the value 1, that gets assigned to ch as a char. Now ch is assigning it's value to a double and doing an implicit cast. So it is assigning the ASCII value of '1' to values[i]. I want it to just assign 1 to values[i]. Is there a better way to do this? Or is there something that I'm missing?

A: 

string stream should be able to do the conversion for you. A pass through atoi could also get the job done.

stonemetal
+3  A: 

If you want to convert a digit character to its corresponding digit value, you can simply subtract '0' from it:

char ch = '2';
int value = ch - '0'; // value is 2

However, the best way to read in floating point numbers is just to extract doubles from cin:

double value = 0;
if (cin >> value)
{
    // value is good
}
else
{
    // invalid value was entered; handle the error appropriately
}

Note that if the extraction fails and you want to continue reading from the stream, you will need to call cin.clear() to clear the fail state of the stream and you may need to call cin.ignore() to ignore any trailing characters (for example, if you are using line-based input, you'll need to ignore everything until the next newline).

James McNellis
I think something just clicked for me. So using your example, I could just do something like this?if (cin >> isdigit(value)){ //value is good}
Brundle
No, and why would you do that? If `cin` cannot extract the value matching the type, it will be put into a fail state, and you won't enter the if branch.
GMan
@Brundle: No... `isdigit()` tests whether a character is a digit; you can't `cin >> isdigit(value)`. Just `cin >> value` where `value` is a `double` and it will attempt to read in and handle format conversion for a floating point value. If it fails, then the `if` test will fail. (Oh, GMan beat me to the explanation)
James McNellis
@Brundle I think what you're missing is the fact that `cin >> value` inherently determines whether the input is valid for the type of `value`. It's all implied by the overloaded `>>` operator.
Cogwheel - Matthew Orlando
Thanks, for the clear up with cin >> value in the if statement. I tried changing my code to this and everything worked great. I knew it was a little thing I was not understanding.
Brundle
+1  A: 

Read the entire number as a string, and the convert the whole thing to a double by using the strtod function or some other C++-specific means.

AndreyT
+1  A: 

While this is not a good solution, you could technically subtract the ASCII code for '0' from your char variable when assigning the array element:

char ch;
int sum = 0;
double values[10];
int i = 0;
cin >> ch;
while (!isalpha(ch))
{  
    values[i] = ch - '0'; 
    sum += values[i]; 
    i++;  
    cin >> ch;
}

If ch was a number (0-9), then ch-'0' would give you that digit ch in "int" form ('0' is dec48 in an ASCII table, and the digits 0 to 9 are from dec48 to dec57).

This is a bit of a hack though, and probably shouldn't really be used, however, it is a possible solution.

Kathoz
Actually, it is required that the numbers are ordered sequentially in the execution character set, so the `ch - '0'` isn't really a hack at all (though there are usually better ways to handle input).
James McNellis