How to end a "do...while" loop after 5 attempts? In C - c

I got my program to fully work, all except I need the program to stop asking after 5 failed attempts. I have absolutely no idea how to go about that. What it is suppose to do, if the number entered is incorrect 5 times in a row, there would then be a sorry message at the end. I can't figure out how to insert the counter into the code, I know what I put in is probably nowhere near correct.
P.S. The code has to have multiple functions. Some I'd rather put into one myself but instruction say multiple.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int number;
void welcomeMessage(){
printf("Welcome to my new guessing game!\n");
printf("Let's get started!\n");
}
int randomNumber(int number){
int range;
srand(time(NULL));
range = (20 - 1) + 1;
number = rand() % range + 1;
return number;
}
int guessInput(){
int guess;
printf("I'm thinking of a number between 1 and 20.\n");
printf("Care to give it a guess? Be careful! You only get 5 tries!: ");
scanf("%d", &guess);
return guess;
}
int guess_input(){
int guess;
scanf("%d", &guess);
return guess;
}
int wrongAnswer(int guess){
if(guess < number)
{
printf("Try again, your guess is too low: ");
return 1;
} else if(guess > number) {
printf("Give it another try, your guess was a bit too high: ");
return 1;
}
return 0;
}
int correctAnswer(int guess){
if(guess == number){
printf("Great job! That time you got it right!\n");
return 0;
} else{
return 1;
}
}
void sorryMessage(int number){
printf("Sorry, the correct number was %number.\n");
printf("Better luck next time!");
}
int main(){
int number;
int loopCount = 5
int guess;
int correct_answer = 1;
int wrong_answer = 1;
welcomeMessage();
number = randomNumber();
guessInput();
do {
correct_answer = correctAnswer(guess);
wrong_answer = wrongAnswer(guess);
guess = guess_input();
}
while(correct_answer == 1);
if(loopcount = 5){
sorryMessage
}
return 0;
}

I have made minimal changes to your program to get it to compile and run as expected.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int number;
void welcomeMessage(){
printf("Welcome to my new guessing game!\n");
printf("Let's get started!\n");
}
int randomNumber(){
int range;
srand((int)time(NULL));
range = (20 - 1) + 1;
number = rand() % range + 1;
return number;
}
int guessInput(){
int guess;
printf("I'm thinking of a number between 1 and 20.\n");
printf("Care to give it a guess? Be careful! You only get 5 tries!: ");
scanf("%d", &guess);
return guess;
}
int guess_input(){
int guess;
scanf("%d", &guess);
return guess;
}
int wrongAnswer(int guess){
if(guess < number)
{
printf("Try again, your guess is too low: ");
return 1;
}
else if(guess > number)
{
printf("Give it another try, your guess was a bit too high: ");
return 1;
}
return 0;
}
int correctAnswer(int guess){
if(guess == number){
printf("Great job! That time you got it right!\n");
return 1;
} else{
return 0;
}
}
void sorryMessage(int number){
printf("Sorry, the correct number was %d\n",number);
printf("Better luck next time!");
}
int main(){
int number;
int loopCount = 0;
int guess;
int correct_answer;
int wrong_answer;
welcomeMessage();
number = randomNumber();
do
{
loopCount += 1;
if( loopCount > 5)
{
sorryMessage(number);
break;
}
guess = guessInput();
correct_answer = correctAnswer(guess);
wrong_answer = wrongAnswer(guess);
} while( !correct_answer );
return 0;
}

Related

Code for if statement is not executing although the condition is met?

The code that I wrote underneath the if statement- if(noMoreGuesses == 1) -is not executing if guessAmount >= guessLimit. I end up just being able to enter in a bunch of numbers without limit. I'm new to c, so if somebody could point out what I am doing wrong I would really appreciate it.
#include <stdio.h>
#include <stdlib.h>
int main() {
int guess;
int guessNumber = 5;
int guessAmount = 0;
int guessLimit = 4;
int noMoreGuesses = 0;
while (guess != guessNumber) {
if (guessAmount >= guessLimit) {
noMoreGuesses = 1;
}
else {
printf("Please enter a number: ");
scanf("%d", &guess);
guessAmount++;
}
}
if (noMoreGuesses == 1) {
printf("You used up all your guesses!\n");
}
else {
printf("You win!");
}
}

As you have used the guess variable before its declaration the problem may occur. The below modified code works fine.
#include <stdio.h>
int main()
{
int guess;
int guessNumber = 5;
int guessAmount = 0;
int guessLimit = 4;
int noMoreGuesses = 0;
while (1) {
if (guessAmount >= guessLimit) {
noMoreGuesses = 1;
break;
}
else {
printf("Please enter a number: ");
scanf("%d", &guess);
guessAmount++;
}
}
if (noMoreGuesses == 1) {
printf("You used up all your guesses!\n");
}
else {
printf("You win!");
}
}

Factorial program in c

Trying to make a code that gets the factorial of the inputted number.
int factorial(int number, int i)
{
int endval;
for(i = number - 1; i>0; i--){
endval = number * i;
}
if (endval == 0){
printf("1");
}
return endval;
}
int main()
{
int endvalue, numA, numB;
char userchoice[1];
printf("Enter a choice to make (f for factorial): \n");
scanf("%s", userchoice);
if(strcmp(userchoice, "f")== 0){
printf("Enter a value to get it's factorial: ");
scanf("%d", &numA);
endvalue = factorial(numA, numB);
printf("%d", endvalue);
return 0;}
getch();
return 0;
}
For some reason the whole for loop doesn't do anything in the function when I set the answer (number*i)= endval. It just prints out the same number I inputted and gives me an absurd answer for 0!.
int factorial(int number, int i)
{
int endval;
for(i = number - 1; i>0; i--){
endval = number * i;
}
if (endval == 0){
printf("1");
}
return endval;
}
However the code works perfectly fine when I remove endval variable entirely (with the exception that it gets 0! = 10)
int factorial(int number, int i)
{
for(i = number - 1; i>0; i--){
number = number * i;
}
if (number == 0) {printf("1");}
return number;
}
Is there anything I missed in the code that's causing these errors?
A definiton of factorial is:
factorial(0) = 1
factorial(n) = n * factorial(n-1)
Note: Factorial is legal only for number >= 0
In C, this definition is:
int factorial(int number)
{
if (number < 0)
return -1;
if (number == 0)
return (1);
/*else*/
return (number * factorial(number-1));
}
#include <stdio.h>
#include <string.h>
int factorial(int number)
{
int endval=1;
for(int i = number ; i>0; i--){
endval *= i;
}
return endval;
}
int main()
{
int endvalue=0;
int numA=0;
char userchoice[1];
printf("Enter a choice to make (f for factorial): ");
int ret=scanf("%s", userchoice);
if (!ret){
printf("Error in scanf: %d", ret);
}
if(strcmp(userchoice, "f")== 0){
printf("Enter a value to get it's factorial: ");
scanf("%d", &numA);
endvalue = factorial(numA);
printf("%d", endvalue);
return 0;
}
getchar();
return 0;
}
Code with some changes will work
factorial() function can get only one argument.
As a good habit all variables must be initialized.
Add include statement to source and be explicit not rely on compiler.
As we use strcmp() we must include string.h
use standard getchar() instead of getch()
Also can check return value of library function scanf() to ensure reading is correct or not.
You can use warnings from compiler to get most of above notes. In gcc: gcc -Wall code.c
Use a debugger to run program line by line and monitor variables value in each steps or use as many printf() to see what happens in function call.
There are possibly few things to correct. See please attached code.
int factorial(int number)
{
if (number == 0){ return 1; }
int endval=1, i;
for(i = 1; i<=number; i++) { endval *= i; }
return endval;
}
int main() {
int endvalue, numA;
char userchoice[1];
printf("Enter a choice to make (f for factorial): \n");
scanf("%s", userchoice);
if(strcmp(userchoice, "f")== 0) {
printf("Enter a value to get it's factorial: ");
scanf("%d", &numA);
endvalue = factorial(numA);
printf("%d", endvalue);
return 0;
}
getch();
return 0;
}

Why is this program going into an infinite loop after the first scanf?

I am trying to get the program to allow the user to input a number and then have the computer tell the user if the number is too small, too big, or equal to a randomly generated number. The prompt and input work, but it gets stuck after the first scanf.
I think it has to do with scanf and not the conditionals, because I added printf("Testing stopping point") and that doesn't get printed to the user's screen. What am I doing wrong?
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <stdlib.h>
int main()
{
//Generate a random number
int n = 1;
int count = 0;
int randomNumber;
srand(time(NULL));
for (int i = 1; i <= n; i++)
{
randomNumber = rand() % 101;
}
printf("Guess a number between 1 - 100: ");
int input;
scanf("%d\n",&input);
printf("Testing stopping point");
do
{
if (input > randomNumber)
{
count +=1;
printf("Too large!Try again: ");
getchar();
}else if (input < randomNumber)
{
count += 1;
printf("Too small!Try again: ");
getchar();
}
}while (input != randomNumber);
if(input == randomNumber)
{
count +=1;
printf("Correct!\n");
printf("You guessed %d times\n", count);
return 0;
}
}
You have to remove the \n in the scanf,try like this:
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <stdlib.h>
int main()
{
//Generate a random number
int count = 0;
int randomNumber;
srand(time(NULL));
randomNumber = (rand() % 101)+1;
int input=-1;
while (input != randomNumber)
{
printf("Guess a number between 1 - 100: ");
scanf("%d",&input);
if (input > randomNumber)
{
count +=1;
printf("Too large!Try again: ");
}else if (input < randomNumber)
{
count += 1;
printf("Too small!Try again: ");
}
}
if(input == randomNumber)
{
count +=1;
printf("Correct!\n");
printf("You guessed %d times\n", count);
}
return 0;
}

Error displaying in "Guess my number" game

I am learning C programming and now I am trying to code this program called "Guess my number" whereby player 1 will pick a number in a range, and then player 2 will guess the number. If the number of guesses exceed 10 tries, player 1 will win. However, the code program breaks after 10 tries and does not display the "Player 1 wins". Anybody can help me in this? Thanks.
#include <stdio.h>
#include <stdbool.h>
#define boolean
int main()
{
int enternumber = -1;
int count = 0;
int maxguesses = 10;
int guessing;
int i;
bool currentguesses = false;
while (1) {
count += 1;
printf("Player 1, enter a number between 1 and 1000\n");
scanf("%d", &enternumber);
if (enternumber > 1000) {
printf("That number is out of range\n");
}
else {
printf("That number is in the range\n");
break;
}
}
printf("Player 2, you have %d tries remaining\n", maxguesses);
for (i = 0; i < maxguesses; i++) {
printf("Enter your guess\n");
scanf("%d", &guessing);
if (enternumber == guessing) {
printf("Player 2 wins.\n");
break;
}
else {
printf("Too %s.\n", enternumber < guessing ? "high" : "low");
}
}
return 0;
if (count == maxguesses) {
printf("Player 1 wins");
}
}
You should put your return 0 statement after checking if count == maxguesses.
#include <stdio.h>
#include <stdbool.h>
#define boolean
int main()
{
int enternumber = -1;
int count = 0;
int maxguesses = 10;
int guessing;
int i;
bool currentguesses = false;
while (1) {
count += 1;
printf("Player 1, enter a number between 1 and 1000\n");
scanf("%d", &enternumber);
if (enternumber > 1000) {
printf("That number is out of range\n");
}
else {
printf("That number is in the range\n");
break;
}
}
printf("Player 2, you have %d tries remaining\n", maxguesses);
for (i = 0; i < maxguesses; i++) {
printf("Enter your guess\n");
scanf("%d", &guessing);
if (enternumber == guessing) {
printf("Player 2 wins.\n");
break;
}
else {
printf("Too %s.\n", enternumber < guessing ? "high" : "low");
}
}
if (count == maxguesses) {
printf("Player 1 wins");
}
return 0;
}

Two player guessing game in C programming

I have a two player guess random number game; player number (1 0r 2) with turn is randomly generated as the game begins and it's prompted to enter the player number. If the player number entered does not match the random player number then it should print "You have to wait your turn" and return to the enter Player number prompt. My program goes directly to the randome number that is to be guessed by the player instead of going back to asking for player number first and then moving onto asking for entering the random number that's to be guessed by player with his/her turn. How do I get it to go back to asking for the correct player number before moving forward.
Also one correct player number is entered; each player can choose to pass by entering "PASS" twice consecutively and thrice throughout the life of the game. How do I make these conditions work in the game. Thanks in advance to all.
Here is the code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <malloc.h>
int main(void) {
int player_num = 0; int number = 0; int player_input = 0;
int guess = 0; char input; char str[6] = {0}; int Player_1 = 1;
int Player_2 = 2; int Pass_1 = 3; int Pass_2 = 3; int i = 1;
int player_turn = 0; int turn = 0;
srand(time(NULL)); player_num = 1 + rand() % 2; /* Random number is generated */
srand(time(NULL)); number = 0 + rand() % 100; /* Random number is generated */
while(number != guess) {
printf("\nIt's player's %d turn\n", player_num);
printf("Player Number?\n");
scanf("%d", &player_input);
while (player_num != player_input) {
printf("You Have to wait your turn.\nPlayer number?\n");
}
if (Player_1 != player_num)
Player_2 = player_num;
if (i%2 == 1) {
player_num = Player_1;
} else {
player_num = Player_2;
}
i = i+1;
printf("Enter Your Guess, 0 - 100 or Pass: ");
scanf("%s", str);
if (strcmp(str, "pass") == 0){
if (player_num == Player_1){
Pass_2 = Pass_2 -1;
printf("Player 2 has %d more 'Pass' left!\n", Pass_2);
}
else {
Pass_1 = Pass_1 -1;
printf("Player 1 has %d more 'Pass' left!\n", Pass_1);
}
} else {
guess = atoi(str);
if(guess < number) /* if the guess is lower, output: the guess is too low */
printf("Your guess was to low.\n ");
else if(guess > number) /* if the guess is higher, output: the guess is too high */
printf("Your guess was to high.\n ");
else /* if the guess is equal to the random number: Success!! */
printf("Yes!! you got it!\n");
}
}
return 0;
}
First, srand needs to be invoked only once and at the start of the program.
Second, scanf was moved inside the second while loop to force user to enter the correct player number or keep asking until he/she gets it right.
The following code fixes all of the above. Please read the comments in the code to see the reasons for the changes:
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
int main(void) {
int player_num = 0; int number = 0; int player_input = 0;
int guess = 0; char input; char str[15] = {0}; // size was increased to compensate for pass pass input
int Player_1 = 1;
int Player_2 = 2; int Pass_1 = 3; int Pass_2 = 3; int i = 1;
int player_turn = 0; int turn = 0; int alternate=0;
int player1Flag=0,player2Flag=0;
int lastPlayer=0;
srand(time(NULL));
player_num = 1 + rand() % 2; /* Random number is generated */
lastPlayer = player_num;
number = 0 + rand() % 100; /* Random number is generated */
while(number != guess) {
while (player_num != player_input) {
printf("\nIt's player's %d turn\n", player_num);
printf("Player Number?\n");
scanf("%d", &player_input);
getchar();// to get rid of \n after the input
if(player_input!=player_num){
printf("You Have to wait your turn.\n");
}
}
if (Player_1 != player_num) Player_2 = player_num;
printf("Enter Your Guess, 0 - 100 or Pass: ");
scanf("%s",str);
if (strcmp(str, "pass") == 0){
if (player_num == Player_1){
player1Flag = player1Flag+1; // flag to detect if last input was a pass
if(player1Flag>1){
printf("Dude you passed in your last attempt .. dont be a pus*y\nEnter a guess : ");
scanf("%s",&str);
guess = atoi(str);
if(guess < number){ /* if the guess is lower, output: the guess is too low */
printf("Your guess was to low.\n ");
}else if(guess > number){ /* if the guess is higher, output: the guess is too high */
printf("Your guess was to high.\n ");
}else{ /* if the guess is equal to the random number: Success!! */
printf("Yes!! you got it!\n");
}
player1Flag = 0; // reset the pass flag = 1 as this pass isn't counted
}else{
Pass_2 = Pass_2 -1;
if(Pass_2<0){
printf("You have already passed Thrice\nEnter a guess: ");
scanf("%s",&str);
guess = atoi(str);
if(guess < number){ /* if the guess is lower, output: the guess is too low */
printf("Your guess was to low.\n ");
}else if(guess > number){ /* if the guess is higher, output: the guess is too high */
printf("Your guess was to high.\n ");
}else{ /* if the guess is equal to the random number: Success!! */
printf("Yes!! you got it!\n");
}
}else{
printf("Player 1 has %d more 'Pass' left!\n", Pass_2);
}
}
}
else{
player2Flag = player2Flag + 1;
if(player2Flag>1){
printf("Dude you passed in your last attempt .. dont be a pus*y\nEnter a guess : ");
scanf("%s",&str);
guess = atoi(str);
if(guess < number){ /* if the guess is lower, output: the guess is too low */
printf("Your guess was to low.\n ");
}else if(guess > number){ /* if the guess is higher, output: the guess is too high */
printf("Your guess was to high.\n ");
}else{ /* if the guess is equal to the random number: Success!! */
printf("Yes!! you got it!\n");
}
player2Flag=0;// reset the player2Flag = 1 as this pass isn't counted
}else{
Pass_1 = Pass_1 -1;
if(Pass_2<0){
printf("You have already passed Thrice\nEnter a guess: ");
scanf("%s",&str);
guess = atoi(str);
if(guess < number){ /* if the guess is lower, output: the guess is too low */
printf("Your guess was to low.\n ");
}else if(guess > number){ /* if the guess is higher, output: the guess is too high */
printf("Your guess was to high.\n ");
}else{ /* if the guess is equal to the random number: Success!! */
printf("Yes!! you got it!\n");
}
}else{
printf("Player 2 has %d more 'Pass' left!\n", Pass_2);
}
}
}
}else {
if (player_num == Player_1){
player1Flag = 0;//reset pass flag as this player enetered a guess
}else{
player2Flag = 0;
}
guess = atoi(str);
if(guess < number){ /* if the guess is lower, output: the guess is too low */
printf("Your guess was to low.\n ");
}else if(guess > number){ /* if the guess is higher, output: the guess is too high */
printf("Your guess was to high.\n ");
}else{ /* if the guess is equal to the random number: Success!! */
printf("Yes!! you got it!\n");
}
}
if(lastPlayer==1){
player_num = 2;
lastPlayer = 2;
}else if(lastPlayer==2){
player_num = 1;
lastPlayer = 1;
}
}
return 0;
}
You need to add an input within your while. In other words, instead of:
while (player_num != player_input) {
printf("You Have to wait your turn.\nPlayer number?\n");
}
try this:
while (player_num != player_input) {
printf("You Have to wait your turn.\nPlayer number?\n");
scanf("%d", &player_input);
}

Resources