Fuel Consumption C ++

0

I do not understand why when I answer the question of the percentage the program advances me, on the other hand if I answer but without the% I simply get a suitable answer, it will be wrong maybe the formula? Thanks in advance!

#include <iostream>
using namespace std;

int main()
{
    float litrosDeposito, consumoCoche, kmTotales;
    int porcentajeRestante;

    cout << "De cuanto es el deposito de combustible de vuestro coche? \n";
    cin >> litrosDeposito;
    cout << "Y cuanto porcentaje os queda? 100%, 75%, 50% o 25%? \n";
    cin >> porcentajeRestante;
    cout << "Cuantos KM haces con un litro de combustible? \n";
    cin >> consumoCoche;

    kmTotales = consumoCoche * litrosDeposito - porcentajeRestante;

    if (kmTotales > 200)
    {
        cout << "Tira millas! \n";
    }
    else if (kmTotales == 200)
    {
        cout << "Por los pelos, pero llegamos! \n";
    }
    else
    {
        cout << "Pasa por caja y llena el deposito! \n";
    }
    system("PAUSE");
    return 0;
}
    
asked by Dan Cezanne Galavan 29.10.2018 в 23:19
source

2 answers

4

If you make the percentage the last thing you ask, everything works fine, with and without a symbol of %

cout << "De cuanto es el deposito de combustible de vuestro coche? \n";
cin >> litrosDeposito;
cout << "Cuantos KM haces con un litro de combustible? \n";
cin >> consumoCoche;
cout << "Y cuanto porcentaje os queda? 100%, 75%, 50% o 25%? \n";
cin >> porcentajeRestante;

Why does this happen? The input flow ( std::cin ) when reading in numeric values reads numbers until it finds a non-numeric data, at which moment it stops reading. This means that reading 12% the reading pointer stays after 2 :

Before (read in porcentajeRestante : nothing):

Posición del búfer: | 0 | 1 | 2 |
                    +---+---+---+
Datos del búfer:    | 1 | 2 | % |
                    +---+---+---+
Puntero de lectura: | ^ |   |   |

After (read in porcentajeRestante : 12 ):

Posición del búfer: | 0 | 1 | 2 |
                    +---+---+---+
Datos del búfer:    | 1 | 2 | % |
                    +---+---+---+
Puntero de lectura: |   |   | ^ |

When you try to read the following data (suppose that it is 20 ), the first thing you find is the % that since it is not a numerical value, it does not read:

Before (read in consumoCoche : nothing):

Posición del búfer: | 0 | 1 | 2 | 3 | 4 |
                    +---+---+---+---+---+
Datos del búfer:    | 1 | 2 | % | 2 | 0 |
                    +---+---+---+---+---+
Puntero de lectura: |   |   | ^ |   |   |

After (read in consumoCoche : nothing , % is not a number):

Posición del búfer: | 0 | 1 | 2 | 3 | 4 |
                    +---+---+---+---+---+
Datos del búfer:    | 1 | 2 | % | 2 | 0 |
                    +---+---+---+---+---+
Puntero de lectura: |   |   |   | ^ |   |

To avoid the problem, you can change the order of data collection by leaving the percentage for the last or, after reading the percentage, clean the buffer:

cout << "De cuanto es el deposito de combustible de vuestro coche? \n";
cin >> litrosDeposito;
cout << "Y cuanto porcentaje os queda? 100%, 75%, 50% o 25%? \n";
cin >> porcentajeRestante;

if (cin.peek() == '%') // Si el siguiente dato es un '%', lo ignoramos.
    cin.ignore(1);

cout << "Cuantos KM haces con un litro de combustible? \n";
cin >> consumoCoche;
    
answered by 30.10.2018 в 09:43
-1

The problem is that if you add the% the variable stops being numeric and becomes alphanumeric, which prevents the subtraction operation on it.

I think it would be best if you validate what was entered by the user, something like this:

while (!(cin >> porcentajeRestante))
    cout << "Por favor ingrese un valor numérico\n";

Good luck!

    
answered by 29.10.2018 в 23:27