Warning: Statemen has no effect; in C ++ program switch type

1

I was doing a program in C ++, using the switch type function

I asked for the price of an item, I read it, then I asked for the discount option that depends on the price, but this discount option is entered manually

I enclose my code to understand what I mean:

#include <iostream>
using namespace std;

int main() 

int precio, tot_pagar;
char opcion;

    cout<< "Ingrese el precio ";
    cin>> precio;
    cout<< endl;
    cout<< "Ingrese la opción de descuento ";
    cin>> opcion;
    switch (opcion)
    {
            case 'A':  tot_pagar = precio-precio*0.05;
                cout<< "El total a pagar es "; tot_pagar;
                    break;
            case 'B':  tot_pagar = precio-precio*0.10;
                cout<< "El total a pagar es "; tot_pagar;
                    break;
            case 'C':  tot_pagar = precio-precio*0.15;
                cout<< "El total a pagar es "; tot_pagar;
                    break;
            default: cout<< "Su artículo no tiene descuento";
    }
return 0;

When the program starts, enter the price, the option, but then do not print the corresponding operation, depending on the case.

    
asked by osmodiar16 02.06.2016 в 00:40
source

2 answers

0

std::cout expects to receive all the information to be displayed on the screen using operator << . ends when you add ;

In your case, you are adding a string, but you close the flow before adding also the variable tot_pagar :

cout<< "El total a pagar es "; tot_pagar;

The correct thing would be:

cout << "El total a pagar es " << tot_pagar << endl;

It is also advisable to add endl at the end, to specify the end of the line.

Greetings

    
answered by 02.06.2016 / 00:55
source
0

First you have some details how to use ";" instead of "< <" since you are not allowing to add the value of tot_pagar .

You can also use cin.get(); to get only the first character (either A, B, or C).

#include <iostream>
using namespace std;

    int main()
    {
       int precio, tot_pagar;
       char opcion;

        cout<< "Ingrese el precio ";
        cin>> precio;
        cout<< endl;
        cout<< "Ingrese la opción de descuento ";
        cin>> opcion;
        cin.get(); //*** Haz uso de cin.get()
        switch (opcion)
        {
                case 'A':  tot_pagar = precio-precio*0.05;
                    cout<< "El total a pagar es " << tot_pagar;
                        break;
                case 'B':  tot_pagar = precio-precio*0.10;
                    cout<< "El total a pagar es " << tot_pagar;
                        break;
                case 'C':  tot_pagar = precio-precio*0.15;
                    cout<< "El total a pagar es " << tot_pagar;
                        break;
                default: cout<< "Su artículo no tiene descuento";
        }
        return 0;
    }
    
answered by 02.06.2016 в 00:59