Validation in C ++

0

Good Night, I have the following exercise in which I ask for an answer and there are two options (S / N), S or N (yes or no), the program is great however the validation that only S or No, I can not make it work. If I put any other letter that is not S or N the program keeps running. My question is how and where do I validate ...

#include "iostream"
#include "stdio.h"
#include "math.h"
#include "stdlib.h"
#include "iomanip"

using namespace std;

int main(int argc, char const *argv[])
{
int i, n, cn = 0, combo, c1, c2, c3;
int c1total = 0, c2total = 0, c3total = 0;
float combo1 = 8500, combo2 = 11700, combo3 = 15000;
float descuento = 0, tdescuento= 0, subtotal, total, ttotal = 0, total_neto;
char nombre[30], carnet = 's';

cout<<"< = = = C L A P Edo. T A C H I R A = = = >";
cout<<"\n\nCantidad de Personas para Atender: ";
cin>>n;

for(i = 0 ; i < n ; i++)
{
    cn++;
    //system("cls");
    cout<<fixed<<setprecision(2);
    cout<<"\n\n< = = = C L A P Edo. T A C H I R A = = = >";
    cout<<"\n\nNombre del Ciudadano: "<<cn;cout<<" ";
    cin>>nombre;
    cout<<"\nPosee Carnet de la Patria (S/N): ";
    cin>>carnet;

    if(carnet == 's' || carnet =='S')
    {
        system("cls");
        cout<<"< = = = C L A P Edo. T A C H I R A = = = >";
        cout<<"\n\nCombos Disponibles: ";
        cout<<"\n\nCombo 1 (Bs. 8500)";
        cout<<"\nCombo 2 (Bs. 11700)";
        cout<<"\nCombo 3 (Bs. 15000)";
        cout<<"\n\nCombo a Comprar: ";
        cin>>combo;

        switch (combo)
        {
            case 1:

            system("cls");
            cout<<"< = = = C L A P Edo. T A C H I R A = = = >";
            cout<<"\n\nCombo 1 (Bs. 8500)";
            cout<<"\n\nCantidad a Comprar: ";
            cin>>c1;

            subtotal = c1 * combo1;
            if(subtotal > 50000)
                descuento = subtotal * 8.7/100;
            total = subtotal - descuento;
            tdescuento = tdescuento + descuento;
            c1total = c1total + c1;
            ttotal = ttotal + total;

            system("cls");
            cout<<"< = = = C L A P Edo. T A C H I R A = = = >";
            cout<<"\n\n< - F A C T U R A - >";
            cout<<"\n\nCliente: "<<nombre;
            cout<<"\n\nSubtotal: Bs. "<<subtotal;
            cout<<"\nDescuento: Bs. -"<<descuento;
            cout<<"\n\nTotal a Pagar: Bs. "<<total;
            break;

        case 2:

            system("cls");
            cout<<"< = = = C L A P Edo. T A C H I R A = = = >";
            cout<<"\n\nCombo 2 (Bs. 11700)";
            cout<<"\n\nCantidad a Comprar: ";
            cin>>c2;

            subtotal = c2 * combo2;
            if(subtotal > 50000)
                descuento = subtotal * 8.7/100;
            total = subtotal - descuento;
            tdescuento = tdescuento + descuento;
            c2total = c2total + c2;
            ttotal = ttotal + total;

            system("cls");
            cout<<"< = = = C L A P Edo. T A C H I R A = = = >";
            cout<<"\n\n< - F A C T U R A - >";
            cout<<"\n\nCliente: "<<nombre;
            cout<<"\n\nSubtotal: Bs. "<<subtotal;
            cout<<"\nDescuento: Bs. -"<<descuento;
            cout<<"\n\nTotal a Pagar: Bs. "<<total;
            break;

        case 3:

            system("cls");
            cout<<"< = = = C L A P Edo. T A C H I R A = = = >";
            cout<<"\n\nCombo 3 (Bs. 15000)";
            cout<<"\n\nCantidad a Comprar: ";
            cin>>c3;

            subtotal = c3 * combo3;
            if(subtotal > 50000)
                descuento = subtotal * 8.7/100;
            total = subtotal - descuento;
            tdescuento = tdescuento + descuento;
            c3total = c3total + c3;
            ttotal = ttotal + total;

            system("cls");
            cout<<"< = = = C L A P Edo. T A C H I R A = = = >";
            cout<<"\n\n< - F A C T U R A - >";
            cout<<"\n\nCliente: "<<nombre;
            cout<<"\n\nSubtotal: Bs. "<<subtotal;
            cout<<"\nDescuento: Bs. -"<<descuento;
            cout<<"\n\nTotal a Pagar: Bs. "<<total;
            break;

        default:
            cout<<"Opcion Invalida, Intente Nuevamente";
            cn--;
        }
        subtotal = 0;
        descuento = 0;
        total = 0;

    }else

       if(carnet == 'n' || carnet == 'N')
        {
            system("cls");
            cout<<"< = = = C L A P Edo. T A C H I R A = = = >";
            cout<<"\n\nCombo 3 (Bs. 15000)";
            cout<<"\n\nCantidad a Comprar: ";
            cin>>c3;
            if(c3 >= 2)
                {
                    cout<<"\nSolo puede Comprar Uno";
                    cout<<"\nLe Invitamos a Sacarse el Carnet de la Patria";
                    cout<<"\n\nCantidad a Comprar: ";
                    cin>>c3;
                }

            total = c3 * combo3;
            c3total = c3total + c3;
            ttotal = ttotal + total;

            system("cls");
            cout<<"< = = = C L A P Edo. T A C H I R A = = = >";
            cout<<"\n\n< - F A C T U R A - >";
            cout<<"\n\nCliente: "<<nombre;
            cout<<"\n\nTotal a Pagar: Bs. "<<total;
        }

}

system("cls");
cout<<"< = = = C L A P Edo. T A C H I R A = = = >";
cout<<"\n\nCantidad de Combo 1 Vendido: "<<c1total;
cout<<"\nCantidad de Combo 2 Vendido: "<<c2total;
cout<<"\nCantidad de Combo 3 Vendido: "<<c3total;
cout<<"\n\nTotal General Facturado: Bs. "<<ttotal;
cout<<"\n\nTotal Descontado: Bs. "<<tdescuento;
total_neto = ttotal - tdescuento;
cout<<"\n\nTotal en Ganancias: Bs. "<<total_neto;

return 0;

}

    
asked by Carlos Agustin Guanipa Alvarez 09.04.2017 в 03:57
source

1 answer

1

Removing intermediate code the part that matters looks like this:

cout<<"\nPosee Carnet de la Patria (S/N): ";
cin>>carnet;

if(carnet == 's' || carnet =='S')
{
  // ...
}else if(carnet == 'n' || carnet == 'N')
{
  // ...
}

If you want to show an error message you should add a last else to that statement. In addition, the logical thing would be that before an incorrect entry the program would ask again ... for that you need a loop. An example:

bool opcionCorrecta = true;
while( opcionCorrecta )
{
  cout<<"\nPosee Carnet de la Patria (S/N): ";
  cin>>carnet;

  if(carnet == 's' || carnet =='S')
  {
    // ...
    opcionCorrecta = true;
  }else if(carnet == 'n' || carnet == 'N')
  {
    // ...
    opcionCorrecta = true;
  }
  else
  {
    cout << "ERROR. Elige una opción válida"; // Por ejemplo
    opcionCorrecta = false;
  }
}

Consider also dividing the program into shorter and simpler functions to improve its readability.

    
answered by 09.04.2017 / 04:06
source