user474901
user474901

Reputation:

output double in cout C++

I have problem with my C++ console app when I tried to output double that calculate from Vector int it show me 0

for (int i  = 0; i < max_streak_length; i++)
{
    cout<< "data win streak of " << i << " acheived : " << gamesData[i]; 
    cout<< " Propallty \t "<< gamesData[i]/(gamesData[0] + gamesData[1])<<endl;

}

output

data win streak of 0 acheived : 6 Propallty 0
data win streak of 1 acheived : 5 Propallty 0
data win streak of 2 acheived : 2 Propallty 0
data win streak of 3 acheived : 0 Propallty 0
data win streak of 4 acheived : 0 Propallty 0
data win streak of 5 acheived : 1 Propallty 0

Upvotes: 0

Views: 368

Answers (2)

YaleCheung
YaleCheung

Reputation: 620

I think your element type in gamesData[i] is int. If you want to get the result of 3/2, the result is 1, for the result of int/int is a int. If you want to get double data, you need use double(gamesData[i])/(gamesData[0] + gamesData[1]) instead. For the type of result depends on the type of numerator in such a situation.

Upvotes: 0

Jesse Good
Jesse Good

Reputation: 52365

Is your vector contain double or ints?

It looks like it contains ints and if that is the case you need to cast to double:

static_cast<double>(gamesData[i])/(gamesData[0] + gamesData[1])

When you perform calculations with type int, you will always get a result of type int.

Upvotes: 1

Related Questions