Home > Software design >  How to terminate my cpp program after 3 attempts of asking for pin?
How to terminate my cpp program after 3 attempts of asking for pin?

Time:10-02

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?

CodePudding user response:

Consider what's happening as you step through your code. attemptCount is set to 0. Your while loop continues while attemptCount is less than 3. So far so good. We do enter the loop.

If the pin entered isn't "1234" then you print an error and increment attemptCount by 1.

If it is equal to "1234" and attemptCount is equal to 0 then it prints a message that "3 pins were unsuccessful." and then program ends because you've returned from main.

Otherwise, the pin was successful, and it prints "Access granted." and breaks out of the loop.

The only condition under which you return early and the code after the while loop in main isn't executed is if the PIN is successfully guessed on the first try.

(As a sidenote, do yourself a favor and neatly format your code. Code that's well-formatted is easier to reason about.)

CodePudding user response:

first you have to do change this like that

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

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

    attemptCount  ;
}

what happening here you are checking first pin is correct or not if it goes there it increment the attemptCount and back to loop and you have to write your menu and all the statements in else condition and remove the break from it. Like that

else
{
    cout << "Access granted." << endl;
    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;

            
        
        
}
 
}

Now your whole code look like this

#include <iostream>
#include <string>

using namespace std;

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

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

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

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

    attemptCount  ;
}

else
{
    cout << "Access granted." << endl;
    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;
}

CodePudding user response:

//Hello everyone! so this is the revised code of my program, and it is now working properly!

#include <iostream>
#include <string>

using namespace std;

int main ()


{
string pin;
int attemptCount = 0;
int x = 1;

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

if ( pin == "1234")
{
    cout << "Access granted." << "\n";
    break;

}
else if ( pin != "1234" )
{
    cout << "The pin is incorrect."<<
    endl;

      x;
}

if (x > 3)
{
    cout << "3 attempts were unsuccessful." << endl;
    
    system("pause");
    return 0;
 
}

}



    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;
}
  • Related