Click here to Skip to main content
15,746,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C program to give user 3 tries to enter correct code number use a while loop and if else statements 

#include<stdio.h>	//header	

int main()		//start
	int pass, num =3;
	printf("You have 3 tries to enter the correct passcode!\n");
	while(num == 1)		//begin while loop
		printf("Please enter the passcode: ");		//asks user for passcode	
		scanf("%d", &pass);							//checks passcode
		if(pass == 11862)
			printf("Your passcode is correct!");
			num = 0;
			break;		//stops or exits the loop, the password is correct
		printf("That is not the correct passcode, you have 2 more attempts.\n");
			scanf("%d", &pass);					//checks passcode
			if(pass != 11862)	//if user doesn't enter 11862 then...
	     printf("That is not the correct passcode, you have 1 more 
        attempt!\n");//user is told incorrect 1 more time

		scanf("%d", &pass);				////checks passcode
		printf("That is the correct passcode!\n");		//tells user correct
		scanf("%d", &pass);							
		break;											//exits the loop
		printf("Your account is locked, you failed 3 attempts.");		 
     //tells user they entered wrong passcode 3times locked
		break;													//exits
	return 0;								//end

What I have tried:

I don't know how to enter a pic of my code that I have that isn't working! I'm a COMPLETE BEGINNER. Ok, so I have attempted to copy and paste my code but it was a real mess! My apologies!
Updated 16-Jan-23 10:24am
Patrice T 16-Sep-22 23:23pm    
Try to copy your code as text, or retype your code in question.
Use Improve question to update your question.
Heather J Coleman 16-Sep-22 23:38pm    
Thank You for your help! I attempted to tidy the code up after pasting. I can see that didn't work out so well either!

I don't know how to enter a pic of my code that I have that isn't working!
First off, don't post "pictures of code" - they really aren't helpful because we can't run them without retyping it all, and that introduces its own problems as we can easily make mistakes.
Copy and paste your code, and include it in a code block by using the code widget on the toolbar above the text box, or by selecting "Code block" from the "Paste as" pop-up menu when you paste the code.
Then you get something like this:
#include <stdio.h>

int main()
    printf("Hello World");

    return 0;
Which means we can read your code exactly as you typed and tried to compile it, and if necessary copy and paste it directly into a compiler and try it! Just don't give us a whole 400 page app: give us the relevant fragments so we can focus on the error area rather than trying to work out where it might be.

Secondly, think about what you are trying to ask: remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.

So when you ask a question which is just
C program to give user 3 tries to enter correct phone number use a while loop and if else statements
It really doesn't tell us much at all:
1) We know it's language: C - but you told us that in the tags.
2) It has something to do with a while loop and an if condition, but what makes a phone number "correct" we have no idea.
3) We don't even know if your code compiles, let alone runs and does something unexpected!

Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

Start here: Asking questions is a skill[^] and think about what you need to know, and what you need to tell us in order to get help.

I'm going to assume that your code doesn't run because that's the normal problem for a beginner: they try to compile the code and the system throws then a lot of error messages instead of running the app.
That's normal: you should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you look at those error messages and work out what they mean a little better.

If it isn't a compilation problem, then we need to know a lot more about what the code is meant to do, what it actually does, what you did to cause it, and what have tried to do to fix it - along with teh relevant code fragments itself, of course!
We all had to start there once! Just stop panicking, calm down, and think logically about it.

If you can't work it out at all, then follow the links above and this one: How to Write Code to Solve a Problem, A Beginner's Guide[^] and feel welcome to come back to us with a better problem description later.

Sorry that doesn't immediately solve your problem, but our hands are tied without the relevant information! Good luck!
Share this answer
Heather J Coleman 16-Sep-22 23:56pm    
Thank you so much for this guidance you were actually a huge help and guided my thoughts to stop panicking and I answered my own question!!! I appreciate you guiding me in a gentle manner also. At times, people can be rude when trying to reach out for guidance on a website that I am also new with. Please have a blessed weekend! Thank You again
OriginalGriff 17-Sep-22 0:02am    
Well done! I'm impressed - and you are more than welcome!
1. obviously the while loop terminates directly because the condition is not fulfilled.The condition should be the maximum number of passes. To count the runs you need another variable.
int pass, num=1, maxnum = 3;
while (num <= maxnum) { ... }

If the correct password was entered, the while loop can be aborted with break
otherwise an error message with remaining attempts is displayed.
printf("That is not the correct passcode, you have %d more attempts.\n", maxnum - num);

When the while loop has finished, you can check if the maximum number of attempts has been exceeded and in that case terminate the program with exit().

What should happen if you don't enter any numbers? The current version terminates directly here.

It is not a good idea to write the correct password in plain text in the source code, because it is easy to find.
Share this answer
#include <stdio.h>

int main() {
    int n, i = 3, pass = 1234;
    printf("You have 3 tries to enter the correct passcode!\n");
    while (i-- > 0) {
        printf("Please enter the passcode: ");
        scanf("%d", &n);
        if (pass == n) {
            printf("Your passcode is correct!\n");

        if (i > 0) 
            printf("That is not the correct passcode you have %d more attempt!\n", i);
            printf("Your account is locked, you failed 3 attempts.\n");
    return 0;
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900