Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Tutorial Simple Guessing Game In C[TUTORIAL]

D

Deleted member 205

Guest
Hey everyone.

Welcome to this C-Programming Tutorial. In today's Tutorial, I'm going to be showing you how to make a simple Guessing Game in C. Let's begin.

First of all, choose where you want to store your Files. Is it in your Documents Directory, a separate Hard-Drive, choose where it will be stored and create a Folder in it called Guessing-Game. This is where your Program will be stored. Once you've done that, create a new File called program.c. This is the File in which our Program's Code will be stored in.

Open it up in your Text-Editor/IDE and write this Code first:
[CODE lang="c" title="program.c"]// Guessing Game in C

#include <stdio.h>
#include <string.h>

int main(void) {


return 0;
}[/CODE]

This is the first part of the Code we'll right. First, we include two of the standard C Libraries. One of the important ones being <string.h>. Then, we write our main() Function. Our Program will all be done in this Function.

Next, insert this Code into your main() Function:
[CODE lang="c" title="program.c"] char Answer[3] = "Dog"; // This is our Char Variable. It's a String which will hold a certain word in it. It only accepts 3 Characters.

// Introduce the Player and have them insert an Answer
printf("Welcome to the Guessing Game! To win, you'll need to insert an Answer.\n");
printf("Please insert your Answer: ");
// Get the Player's Input
scanf("%s", Answer);[/CODE]

First, we create a Variable of the type, char. Then, we give it a certain String which in this case is Dog. And so when the User inserts something, it needs to be Dog or the Player will lose. Then, we get the Player's Input using the scanf() Function. This will allow the Player to insert a String. In scanf(), we have this %s thing and so you're probably wondering about what it does. Well, when getting Input for a String, %s in scanf() will tell the Program that it's a String that we're getting. But if it's a single letter, it would only be, %c.

But wait, how will the Player know what they're actually trying to Guess? Well, let's fix that. Insert this Code here:
[CODE lang="c" highlight="3"] // Introduce the Player and have them insert an Answer
printf("Welcome to the Guessing Game! To win, you'll need to insert an Answer.\n");
printf("Please insert your Answer(*Hint: It Barks and wears a Collar*): ");
// Get the Player's Input
scanf("%s", Answer);[/CODE]

There, now the Player will know about what they're actually trying to Guess.

But now, we need to determine if the Player's Answer is correct or not. To do that, we'll need to use what's called an if-else if Statement. Insert this Code below your scanf() Function:
[CODE lang="c" title="program.c"] // Determine if the Player's Answer is right or not.
if() {

}
else if() {

}[/CODE]

This will be our if-else if Statement that will determine if the Player's Answer is correct or not. And to determine if they are correct, we'll use the strcmp() Function. This Function is primarily used for comparing things in C but can also be used to determine if the Input is correct.

Insert this Code into your if-else if Statement:
[CODE lang="c" title="program.c" highlight="2-3, 5-6"] // Determine if the Player's Answer is right or not.
if(strcmp("Dog", Answer) == 0) {
printf("Hooray, You got it right!");
}
else if(strcmp("", Answer) == 0) {
printf("Sorry. The Answer was actually Dog.");
}[/CODE]

So, as stated, we used the strcmp() Function to determine if the User's Answer was correct or not. If it was, we congratulate them. And if not, then we tell them what the Answer was. Looking at our if-else if Statement, in the strcmp() in if(), we have "Dog" and so if the User inserts Dog, it then goes onto congratulate them. But looking at strcmp() in else if(), we can see that we don't have anything inside our quotation-marks. This is because, anything other than Dog could be wrong.

Now that we've written our Program, we can now Compile and run it.
Note: I'm on a GNU/Linux System which has the GCC Compiler on it. If you're a Windows User, you'll have to install MinGW if you want to Compile C Programs. There are multiple Tutorials online for MinGW so look around and try installing it.

First of, open-up Terminal and type in the following:
cd Guessing-Game
This will take us to our Directory in which we wrote the Program in. Then after that, insert the following:
gcc program.c -o program
This will Compile our Program and create an Executable in which we can run via Terminal.
Note: The Command we just did to Compile our Program, was done using the GCC Compiler. If you use a different Compiler then you'll have to know how to Compile Programs using your Non-GCC Compiler.

After we've Compiled, insert this Command into Terminal:
./program
This will run the Program in Terminal and hopefully, this is what you'll see:

Screenshot at 2019-09-15 13-42-52.png

Congratulations. You've just written your first(Extremely basic Program), in C and successfully Compiled it. Good job!

Challenge: Try and make it so the Program asks you another Question. Then, try and get the Player's Input for that second Question. The Question doesn't have to be an animal. It could be a number or a word.

Note: If your Program didn't Compile and the Terminal displayed Errors, go back, check your Code and make sure everything has been done correctly.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom