0

This is my course project for school, and my program is working for the most part, but when an incorrect pin is entered, the program pauses and closes out. I would like it to return to the main so that you can retry to enter a pin. I think my else statement is being skipped (obviously), but an explanation as to why and how to fix the issue would be much appreciated. I don't know if I can ask two questions in one post, but I can't seem to get my float to print two decimal points. How do I do that? I have researched both issues and cant seem to get anywhere on my own. I am using visual studio 2015 if that matters. Any other critiques of my code would be appreciated as well. Cheers!

int main()
{
    int pin = 0;
    int keypin;
    keypin = 2000;
    int comppin;

    cout << "Enter your pin: "; // Checking user pin 
    cin >> pin;
    cout << "\n\n";

    comppin = (pin, keypin);

    if (pin == 2000) // If correct print welcome screen
    {
        cout << "Pin Approved!\n";
    }
    else // When incorrect print below and return to main **planning to add attempts to this depending on how difficult that turns out to be 
    {
        cout << "Incorrect pin. Try again. ";
        return main();
    }
        system("pause");
        return 0;
    }
}
  • `comppin = (pin, keypin);` does not compare or check anything. It just assigns a value to `comppin` unconditionally – UnholySheep Mar 29 '20 at 22:33
  • ***This is my course project for school, and my program is working for the most part, but when an incorrect pin is entered, the program pauses and closes out. I would like it to return to the main so that you can retry to enter a pin.*** You need a loop maybe a do { } while () loop. – drescherjm Mar 29 '20 at 22:33
  • 1
    I will do my best to do that. – jon-claud ferreira Mar 29 '20 at 22:34
  • `return main();` is not allowed by the C++ standard. Your compiler should at least give a warning about this line – UnholySheep Mar 29 '20 at 22:36
  • Related if not a duplicate: [https://stackoverflow.com/questions/26467682/c-looping-user-input-until-correct](https://stackoverflow.com/questions/26467682/c-looping-user-input-until-correct) this answer is similar to what I am talking about: [https://stackoverflow.com/a/26467721/487892](https://stackoverflow.com/a/26467721/487892) – drescherjm Mar 29 '20 at 22:38
  • @UnholySheep should I just use main(); then? I didn't receive a warning on that at all. – jon-claud ferreira Mar 29 '20 at 22:44
  • `cout << "Incorrect pin. Try again. "; return main();` -- So a batch program can crash your program (blowing out the stack memory) by just entering an invalid pin over and over again. If you want a loop, use a `while()` or `do-while`, not the bogus way of calling `main` over and over again. And as a previous comment stated, this isn't legal C++ to begin with (calling `main`). – PaulMcKenzie Mar 29 '20 at 22:45
  • 1
    No, you are not allowed to call `main` anywhere in your program. See: https://stackoverflow.com/questions/2128321/can-main-function-call-itself-in-c – UnholySheep Mar 29 '20 at 22:45
  • Okay so int main is okay, but calling main is not allowed got it. I guess i'll research some of the above links and try to change it to a while loop. – jon-claud ferreira Mar 29 '20 at 22:54

2 Answers2

0

You have misplaced a closing brace.

if (pin == 2000)
{
    ...

    if (selection == '1')
    { ... }
    else if (selection == '2')
    { ... }
    else if (selection == '3')
    { ... }
} // <-- HERE
else
{
    cout << "Incorrect pin. Try again. ";
    return main();
}

system("pause");
return 0;
} // <-- NOT HERE
theQmech
  • 1
  • 1
  • 1
0

To start off it seems there is a lot of unnecessary code there.

int pin = 0;
int keypin;
keypin = 2000;
int comppin;

You just need two variables here:

constexpr int correctPin{2000};
int userPin;

One for keeping correct pin and second for keeping data from user input. Then define variable which will be holding amount of possible attempts.

Next step: e.g. while loop.

while(amountOfRetries > 0)
{
    //decrement retries
    //all of the user input handling
    //if pin correct break out from the loop
    //if not we just go to another iteration of loop
}

And answering your second question, c'mone first link in google after typing "c++ 2 decimal points".

2 decimal points on stack

rafauu
  • 101
  • 4