samedi 2 octobre 2021

How to terminate my cpp program after 3 attempts of asking for pin?

My program should terminate after 3 wrong attempts but mine would still proceed to the menu even if the attempts were wrong. I've tried using return 0, but I didn't know why it still not worked. Is there any way to fix my program?

#include <iostream>
#include <string>

using namespace std;

int main ()
{
string pin;
int attemptCount = 0;

while ( attemptCount < 3 )
{
cout << "Enter pin: " << endl;
cin >> pin;

if ( pin != "1234")
{
    cout << "Pin is incorrect." << "\n" <<
    endl;

    attemptCount++;

}
else if ( attemptCount = 0 )
{
    cout << "3 pins were unsuccessful.";
    return 0;
}

else
{
    cout << "Access granted." << endl;
    break;
 
}

}



    int a, b, c;
    char choice;
   cout<<"\n\n---Area of a polygon---\n[A] Triangle\n[B] Square\n[C] Rectangle\n[D] Exit \n";
   cout<<"Enter choice: \n";
   cin>>choice;
   
    switch(choice){
        
        case 'a':
        case 'A':
            int square_area, square_side;
            float base, height, area1;

          cout << "\nEnter length of base and height of Triangle: ";
        cin >> base >> height;
    
         area1 = (base * height) / 2;
         cout << "Area of Triangle: " << area1 << endl;
            break;
            
        case 'b':
        case 'B':
            cout<<"\nEnter the side of the square: ";
            cin >> square_side;
            square_area = square_side * square_side;
            cout << "Area of Square: " << square_area << endl;
            break;
            
        case 'c':
        case 'C':
             int length, width, area;
                cout << "\nEnter the Length of Rectangle : ";
   cin>>length;

   cout << "\nEnter the Width of Rectangle : ";
   cin>>width;

   area = length * width;
   cout << "\nArea of Rectangle : " << area;
   break;
   
        case 'd':
        case 'D':
            break;
;
            
        
        
}
   return 0;
}

My program should terminate after 3 wrong attempts but mine would still proceed to the menu even if the attempts were wrong. I've tried using return 0, but I didn't know why it still not worked. Is there any way to fix my program?

Aucun commentaire:

Enregistrer un commentaire