Error displaying in "Guess my number" game - c

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

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!");
}
}

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

School exercice in c about prime numbers

Make a program that asks the user for an integer and says if the
number is prime or not. A number greater than 1 is prime if only
is divisible by 1 and by itself. Then, it will tell us what the prime number is.
example:
Enter a number: 8
8 is not first. The first one immediately superior to 8 is 11.
Enter a number: 5
5 is first. The first one immediately above 5 is 7.
I can only solve first part.
Here is my code:
#include <stdio.h>
int main() {
int num, i;
do {
printf("Enter a numer: ");
scanf("%d", & num);
}
while (num < 1);
for (i = 2; i < num; i++) {
if (num % i == 0)
printf("Its prime");
}
if (num % 1 == 0 && num % num == 0)
printf("Not prime");
return 0;
}
Try this logic. Not tested
#include <stdio.h>
int main()
{
int num, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
int isPrime=IsPrime(num)
if(isPrime==0){
numNext=num+1;
int nextPrimeNum=checkNextPrime(numNext);
}
}
int IsPrime(int num){
for(i = 2; i <= num/2; ++i)
{
// condition for nonprime number
if(num%i == 0)
{
flag = 1;
break;
}
}
if (num == 1)
{
flag=1;//neither prime nor composite
}
return flag;
}
int checkNextPrime(int numNext){
int isNextPrime=IsPrime(numNext)
if(isNextPrime==0){
printf("This is the required output :"numNext);
return numNext;
}
else{
numNext=numNext+1;
checkNextPrime(int numNext)
}
}

I can not determine if it is a prime number

The below program correctly outputs the divisors of the input numbers, but it does not correctly report whether the inputs are prime. For example, when the input is 13, it does not print "The number you entered is a prime number." What's wrong with it?
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int num;
bool isPrime = true;
printf("Enter a number: ");
while (scanf("%d", &num) == 1)
{
for (int i = 2; i * i <= num; ++i)
{
if (num % i == 0)
{
if (i * i != num)
{
printf("%d ve %d, divides %d\n", i, num / i, num);
}
else
{
printf("%d divides %d.\n", i, num);
}
isPrime = false;
}
}
}
if (isPrime)
{
printf("The number you entered is a prime number.");
}
return 0;
}
the reason is that scanf is in a while loop if there's a valid input but you are checking & printing if it's prime outside of the loop... if you expect this program just get one input and validate it once, then you just need to change that while to if:
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int num;
bool isPrime = true;
printf("Enter a number: ");
if (scanf("%d", &num) == 1)
{
for (int i = 2; i * i <= num; ++i)
{
if (num % i == 0)
{
if (i * i != num)
{
printf("%d ve %d, divides %d\n", i, num / i, num);
}
else
{
printf("%d divides %d.\n", i, num);
}
isPrime = false;
}
}
}
if (isPrime)
{
printf("The number you entered is a prime number.");
}
return 0;
}
If you expect this program goes in a loop to keep on getting input and validating if it's prime or not, this should do the job:
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int num;
bool isPrime = true;
while (1)
{
isPrime=true;
printf("Enter a number: ");
if (scanf("%d", &num) == 1)
{
for (int i = 2; i * i <= num; ++i)
{
if (num % i == 0)
{
if (i * i != num)
{
printf("%d ve %d, divides %d\n", i, num / i, num);
}
else
{
printf("%d divides %d.\n", i, num);
}
isPrime = false;
}
}
}
if (isPrime)
{
printf("The number you entered is a prime number.\n");
}
}
return 0;
}
You have missed 2 things !
You should have printed inside the while loop !
In addition to that you didn't change the status of isPrime=true; !
Hope this answers your question !
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int num;
bool isPrime = true;
while (scanf("%d", &num) == 1)
{
for (int i = 2; i * i <= num; ++i)
{
if (num % i == 0)
{
if (i * i != num)
{
printf("%d ve %d, divides %d\n", i, num / i, num);
}
else
{
printf("%d divides %d.\n", i, num);
}
isPrime = false;
}
}
if (isPrime)
{
printf("The number you entered is a prime number.\n");
}
isPrime=true;
}
return 0;
}
When you entered a prime number, your while loop doesn't break. Try it:
#include <stdio.h>
#include <stdbool.h>
int main(void) {
int num;
bool isPrime = true, finishIt = false;
printf("Enter a number: ");
while (1) {
while (1) {
if (scanf("%d", &num) != 1)
continue;
if (num == 0) {
finishIt = true;
break;
}
int i;
for (i = 2; i * i <= num; ++i) {
if (num % i == 0) {
if (i * i != num) {
printf("%d ve %d, divides %d\n", i, num / i, num);
} else {
printf("%d divides %d.\n", i, num);
}
isPrime = false;
}
}
if (i * i >= num)
break;
}
if (isPrime) {
printf("The number you entered is a prime number.");
}
isPrime = true;
if (finishIt)
break;
}
return 0;
}

Why does it say 'expected declaration specifiers before 'main''

first of all, I understand mostly everything in this program that I copied from this book.
second, i just wanted to see if it worked ,
the only problem is that it says 'expected declaration specifiers before 'main''
and i don't know what it means
ps this is a really long program (300+ lines)
#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <stdlib.h>
#define FALSE 0
#define TRUE 1
void printGreeting();
int getBet();
char getSuit(int suit);
char getRank(int rank);
void getFirstHand(int cardRank[], int cardSuit[]);
void getFinalHand
(int cardRank[], int cardSuit[], int finalRank[], int finalSuit[], int
ranksinHand[], int suitsinHand[])
int analyzeHand(int ranksinHand[], int suitsinHand[]);
main()
{
int bet;
int bank = 100;
int i;
int cardRank [5];
int cardSuit [5];
int finalRank[5];
int finalSuit[5];
int ranksinhand[13];
int suitsinhand[4];
int winnings;
time_t t;
char suit, rank, stillPlay;
printGreeting();
do{
bet = getBet();
srand(time(&t));
getFirstHand(cardRank, cardSuit);
printf("Your five cards: \n\n");
for (i = 0; i < 5; i++)
{
suit = getSuit(cardsSuit[i]);
rank = getRank(cardRank[i]);
printf("Card #%d: %c%c\n\n", i+1, rank, suit);
}
for (i=0; i < 4; i++)
{
suitsinHand[i] = 0;
}
for (i=0; i < 13; i++)
{
ranksinHand[i] = 0;
}
getFinalHand(cardRank, cardSuit, finalRank, finalSuit, ranksinHand,
suitsinHand);
printf("Your five final cards:\n\n");
for (i = 0; i < 5; i++)
{
suit = getSuit(finalSuit[i]);
rank = getRank(finalRank[i]);
printf("Card #%d: %c%c\n\n", i+1, rank, suit);
}
winnings = analyzeHand(ranksinHand, suitsinHand);
printf("You won %d!\n\n", bet*winnings);
bank = bank - bet + (bet*winnings)
printf("\n\nYour bank is now %d.\n\n", bank);
printf("Want to play again? ");
scanf(" %c", &stillPlay);
}while (toupper(stillPlay) == 'Y');
return;
}
/*************************************************************************/
void printGreeting();
{
printf("**********************************************************\n\n");
printf("\n\n\tWelcome to the Absolute Beginner's Casino\n\n");
printf("\tHome of the Video Draw Poker");
printf("**********************************************************\n\n");
printf("Here are the rules\n");
printf("You start with 100 credits, and you make a bet from");
printf("1 to 5 credits.\n");
printf("You are dealt 5 cards, and then you choose which ");
printf("cards to keep");
printf("or discard\n");
printf("You want to make the best possible hand.\n");
printf("\nHere is the table for winnings (assuming a ");
printf("bet of 1 credit):");
printf("\nPair \t\t\t\t1 credit");
printf("\nTwo pairs\t\t\t2 credits");
printf("\nThree of a kind\t\t\t3 credits");
printf("\nStraight \t\t\t4 credits");
printf("Flush\t\t\t\t5 credits");
printf("Full House\t\t\t8 credits");
printf("Four of a Kind\t\t\t10 credits");
printf("Straight Flush\t\t\t20 credits");
printf("\n\nHave fun!!\n\n");
}
void getFirstHand(int cardRank[], int cardSuit[]);
{
int i,j;
int carDup;
for(i=0; i < 5; i++)
{
carDup = 0;
do{
cardRank[i] = (rand() % 13);
cardSuit[i] = (rand() % 4);
for (j=0; j < i; j++)
{
if ((cardRank[i] == cardRank[j] &&
cardSuit[i] == cardSuit[j]))
{
carDup = 1;
}
}
}while (carDup == 1;);
}
}
char getSuit(int suit)
{
switch
{
case 0:
return('C');
case 1:
return('D');
case 2:
return('H');
case 3:
return('S');
}
}
char getRank(int rank)
{
switch (rank)
{
case 0:
return('A');
case 1:
return('2');
case 2:
return('3');
case 3:
return('4');
case 4:
return('5');
case 5:
return('6');
case 6:
return('7');
case 7;
return('8');
case 8:
return('9');
case 9:
return('T');
case 10:
return('J');
case 11:
return('Q');
case 12:
return('K');
}
}
int getBet()
{
int bet;
do
{
printf("How much do you want to bet?(Enter a number");
printf("from 1 to 5, or 0 to quit the game): ");
scanf(" %d", &bet);
if (bet >= 1 && bet <= 5)
{
return(bet);
}
else if (bet == 0)
{
exit(1);
}
else
{
printf("\n\nPlease enter a bet from 1-5 or ");
printf("0 to quit the game\n\n");
}
}while ((bet < 0) || (bet > 5));
}
int analyzeHand(int ranksinHand[], int suitsinHand[])
{
int num_consec = 0;
int i, rank, suit;
int straight = FALSE;
int flush = FALSE;
int four = FALSE;
int three = FALSE;
int pairs = 0;
for (suit = 0; suit < 4; suit++)
if (suitsinHand[suit] == 5)
flush = TRUE;
rank = 0;
while (ranksinHand[rank] == 0)
rank++;
for (; rank < 13 && ranksinHand[rank]; rank++)
num_consec++;
if(num_consec == 5) {
straight = TRUE;
}
for (rank = 0; rank < 13; rank++){
if (ranksinHand[rank] == 4)
four == TRUE;
if (ranksinHand[rank] == 3)
three == TRUE;
if (ranksinHand[rank] == 2)
pairs++;
}
if (straight && flush){
printf("Straight Flush\n\n");
return(20);
}
else if (four){
printf("Four of a kind\n\n");
return (10);
}
else if (three && pairs == 1){
printf("Full House\n\n");
return (8);
}
else if (flush){
printf("Flush\n\n");
return (5);
}
else if (straight){
printf("Straight\n\n");
return (4);
}
else if (three){
printf("Three of a Kind\n\n");
return (3);
}
else if (pairs == 2){
printf("Two Pairs\n\n");
return (2);
}
else if (pairs == 1){
printf("Pair\n\n");
return (1);
}
else{
printf("High Card\n\n");
return (0);
}
}
void getFinalHand
(int cardRank[], int cardSuit[], int finalRank[], int finalSuit[], int
ranksinHand[], int suitsinHand[])
{
int i, j, carDup;
char suit, rank, ans;
for (i=0; i < 5; i++)
{
suit = getSuit(cardSuit[i]);
rank = getRank(cardRank[i]);
printf("Do you want to keep card #%d: %c%c", i+1, rank, suit);
printf("\nPlease answer (Y/N):");
scanf(" %c", &ans);
if (toupper(ans) == 'Y')
{
finalRank[i] = cardRank[i];
finalSuit[i] = cardSuit[i];
ranksinHand[finalRank[i]]++;
suitsinHand[finalSuit[i]]++;
continue;
}
else if (toupper(ans) == 'N')
{
carDup = 0;
do{
carDup = 0;
finalRank[i] = (rand() % 13);
finalSuit[i] = (rand() % 4);
for (j=0; j < 5; j++)
{
if((finalRank[i] == finalRank[j]) && (finalSuit[i] ==
finalSuit[j]))
{
carDup = 1;
}
}
for (j=0; j < i; j++)
{
if((finalRank[i] == finalRank[j]) && (finalSuit[i] ==
finalSuit[j]))
{
carDup = 1;
}
}
}while (carDup == 1);
ranksinHand[finalRank[i]]++;
suitsinHand[finalSuit[i]]++;
}
}
}
By convention main() must return an integer. You have to declare it like that:
int main()
{
// Your method
return 0;
}
int mainor
void main
would do the work. Here, int main is shown to be the C standard. Refer to
Return type of main() too.

Resources