comparing an inputted string to a printed function - c

Below is a code I wrote for a dice game called cho han. To input your guess I've used number to represent the words 'odd' and 'even'. Since then I have tried to write it again, but to actually write odd or even in the scanf section, but can't get it to work. Any help would be appreciated :)
//cho-han
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
srand(time(NULL));
int x = (rand() % 6) + 1;
int y = (rand() % 6) + 1;
int result = 0;
int guess = 0;
printf("The values of two dice rolls will be added up. The aim is to guess whether that total number will be odd or even.\n");
printf("To guess odd, enter '1'. To guess even, enter '2'.\n\n");
printf("Please enter your guess for the combined total of the two dice rolls: ");
scanf_s("%d", &guess);
if (guess == 2)
{
printf("\nyour guess is even.\n");
}
if (guess == 1)
{
printf("\nyour guess is odd.\n");
}
if (guess > 2 || guess < 1)
{
printf("\nInvalid guess.\nYou lose!\n");
return (1);
}
printf("\ndice roll 1 = %d\n", x);
printf("dice roll 2 = %d\n", y);
result = x + y;
printf("\ncombined total of both rolls is %d", result);
if (result == 1 || result == 3 || result == 5 || result == 7 || result == 9 || result == 11)
{
printf("\ncombined total of both rolls is odd.\n");
}
else
{
printf("\ncombined total of both rolls is even.\n");
}
if (guess == 1 && result == 1 || guess == 1 && result == 3 || guess == 1 && result == 5 || guess == 1 && result == 7 || guess == 1 && result == 9 || guess == 1 && result == 11)
{
printf("\nYou win!\n");
}
else if (guess == 2 && result == 2 || guess == 2 && result == 4 || guess == 2 && result == 6 || guess == 2 && result == 8 || guess == 2 && result == 10 || guess == 2 && result == 12)
{
printf("\nYou win!\n");
}
else
{
printf("\nYou lose!\n");
}
return 0;
}

You should change scanf_s to scanf
The line if (result == 1 || result == 3 ... could be if (result % 2 == 1) {
You could use strcmp to solve your question
The following code could work:
//cho-han
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int main(void)
{
srand(time(NULL));
int x = (rand() % 6) + 1;
int y = (rand() % 6) + 1;
int result = 0;
int guess = 0;
char buf[10];
printf("The values of two dice rolls will be added up. The aim is to guess whether that total number will be odd or even.\n");
printf("To guess odd, enter 'odd'. To guess even, enter 'even'.\n\n");
printf("Please enter your guess for the combined total of the two dice rolls: ");
fgets(buf, sizeof buf, stdin);
if (strcmp(buf, "even\n") == 0) {
guess = 2;
printf("\nyour guess is even.\n");
} else if (strcmp(buf, "odd\n") == 0) {
guess = 1;
printf("\nyour guess is odd.\n");
} else {
printf("\nInvalid guess.\nYou lose!\n");
return 1;
}
printf("\ndice roll 1 = %d\n", x);
printf("dice roll 2 = %d\n", y);
printf("\ncombined total of both rolls is %d", x + y);
result = (x + y) % 2;
if (result == 1)
printf("\ncombined total of both rolls is odd.\n");
else
printf("\ncombined total of both rolls is even.\n");
if (guess == result)
printf("\nYou win!\n");
else
printf("\nYou lose!\n");
return 0;
}

You need to change your guess to char type and scanf to capture string.
char guess[256];
scanf("%s", guess);
And then the best way would be to call toupper() and compare with your text using strcmp().

Related

Rock Paper .. game

This code runs just fine.
but there is some minor mistake in the program. I don't know what's the problem 'cause it's like I keep on winning the game and the computer just loses the game every time. 3
the game Rock, Paper, Scissors. My problem is if I execute the program, I just keep winning.I need the game to let the computer win. How can I rectify this?
#include <stdio.h
#include <time.h>
int Random(int n) {
srand(time(NULL));
printf("%d\n", rand() % 3); }
int greater(char ch1, char ch2) {
if (ch1 == ch2)
{
return -1;
}
else if ((ch1 == 'R') && (ch2 == 's'))
{
return 1;
}
else if ((ch2 == 'R') && (ch1 == 's'))
{
return 0;
}
else if ((ch1 == 's') && (ch2 == 'p'))
{
return 1;
}
else if ((ch2 == 's') && (ch1 == 'p'))
{
return 0;
}
else if ((ch1 == 'p') && (ch2 == 'r'))
{
return 1;
}
else if ((ch2 == 'p') && (ch1 == 'r'))
{
return 0;
}
}
int main() {
char playerchar, computerchar, a;
int playerscore = 0, compscore = 0, b;
char ch[] = {'R', 'P', 'S'};
printf("\n");
printf("~~~~~Welcome to the Rock, Paper, Scissors Game~~~~~\n\n");
printf("Choose a number\n 1. for Rock\n 2. for Paper\n 3. for Scissiors\n");
for (int i = 0; i < 3; i++)
{
printf("Player's Turn :\n");
scanf("%d", &b);
getchar();
playerchar = ch[b - 1];
printf("You chose %c\n", playerchar);
printf("Computer's Turn :\n");
b = Random(3) + 1;
computerchar = ch[b - 1];
printf("Computer chose %c\n", computerchar);
if ((greater(computerchar, playerchar) == 1))
{
compscore += 1;
printf("Computer got it!\n\n");
}
else if ((greater(computerchar, playerchar) == -1))
{
compscore += 1;
playerscore += 1;
printf("It's a draw\n");
}
else
{
playerscore += 1;
printf("You got it!\n\n");
}
printf("You : %d\nComputer: %d\n\n",playerscore,compscore);
}
if (compscore > playerscore)
{
printf("Computer win the game!\n\n");
}
else if (playerscore > compscore)
{
printf("You win the game!\n\n");
}
else
{
printf("The game is draw\n");
}
return 0;}
There's really no point complex if statements for determining the winner. Just realize that rock paper scissors is basically just a directed cycle graph.
// Returns winner in rock paper scissors
// 0 - Rock, 1 - Paper, 2 - Scissors
// Return values:
// 0 - draw
// 1 - a win, b loose
// -1 - b win, a loose
int rps(int a, int b) {
if(a == b) return 0;
return (a+1) % 3 == b ? -1 : 1;
}
Normal rps (rock, paper, scissors) has 3 nodes in the graph. This can actually easily be generalized to n nodes. The algorithm works like this. Start on node a. If it's the same as b, then it's a draw. Otherwise, check how many steps it is required to to go around the graph to get to b and determine the winner depending on if the number of steps is even or odd.
int rpsn(int a, int b, int n) {
if(a == b) return 0;
int steps = 0;
while((a+steps) % n != b) steps++;
return steps % 2 ? -1 : 1;
}
Read about Rock, Paper, Scissors, Lizard, Spock for a 5 node version.
I'm fairly confident that the loop could be exchanged for a single clever math operation, but I'm having a brain freeze at the moment. I'll update the post if I find it.
EDIT:
I found a way. Here is a very short rps function with no loops for arbitrary n:
// Returns the number of steps required to go from a to b
int steps(int a, int b, int n) {
return (n+b-a) % n;
}
int rpsn(int a, int b, int n) {
if(a == b) return 0;
return steps(a,b,n) % 2 ? -1 : 1;
}
The reason for doing (n+b-a) % n instead of (b-a) % n is that we need the left operand of % to be nonnegative.
First of all, srand should be called just once at the beginning of the program. You call it on every call of Random().
But the bigger issue is that your function Random() does not return anything, it just prints the value. This should have grnerated a warning in your compiler. This probably means that Random always returns 0.
To fix the problem, do return rand() % 3; instead of printing it.

Boolean Logic in AND statement

I created a program that calculates the divisibility by 3 and 5. If divisible by 3 print 'CS' and if divisible by 5 print 'CS1714'. If divisible by both print 'CS1714'. If not divisible by 3 or 5 print 'ERROR'.
My code executes properly. However is it possible for the Boolean && to break if say both values are false. Also why does the code print 'CSERROR' when input is 98988?
#include <stdio.h>
int main(void)
{
int userInput;
scanf("%d", &userInput);
if((userInput % 3) == 0){
printf("CS");
}
if((userInput % 5) == 0){
printf("1714");
}
else if(!((userInput % 5) == 0 && (userInput % 3) == 0)){
printf("ERROR");
}
return 0;
}
When the input is 98988 it is printing CSERROR because the number 98988 is divisible by 3 and that is why the first if condition is true and thus it prints CS, and then you gave another if where you are checking if the number is divisible by 5 or not, if the number is divisible by 5 then you print 1714, else you print ERROR, as 98988 is not divisible by 5 that is why it goes to the else part and prints ERROR (The last else will always true if your second if is false. So, here is a logical error). Previously it printed CS and now it printed ERROR, combinedly you are seeing CSERROR.
One is need to be clear that, the if-else blocks structures are like below, and it started implementing from a if to go further until finds a true condition or else.
if(condition){
}
else if(condition){
}
else{
}
You should check both conditions (when the number is divisible by 3 and also divisible by 5) first, then check whether it is divisible by 3 , if not then check by 5. And finally if all are false that means it is not divisible by 3 and also not divisible by 5, so print error.
The code should be like:
#include <stdio.h>
int main(void)
{
int userInput;
scanf("%d", &userInput);
if(((userInput % 5) == 0 && (userInput % 3) == 0)){
printf("CS1714");
}
else if((userInput % 3) == 0){
printf("CS");
}
else if((userInput % 5) == 0){
printf("1714");
}
else{
printf("ERROR");
}
return 0;
}
Input: 98988
Output: CS
#include <stdio.h>
int main(void)
{
int userInput;
scanf("%d", &userInput);
if((userInput % 3) == 0){
printf("CS");
}
if((userInput % 5) == 0){
printf("1714");
}
else if(((userInput % 5) != 0 && (userInput % 3) != 0)){
printf("ERROR");
}
return 0;
}

C | Comparison between two groups of arrays

I'm pretty fresh in the computers world and started to work on c.
now i'm building a little project about generating code (shown below):
My main:
#include <stdio.h>
#include "connect.h"
#define EASY 20
#define NORMAL 15
#define HARD 10
#define CRAZY 30
int main ()
{
int choice = 1;
char enter, returnV;
system("cls");
printf("Welcome to ""THE MAGSHIMIM CODE BREAKER""!!!\n");
printf("\n");
printf("A secret password was chosen to protect the credit card of Pancratius,\nthe descendant of Antiochus\n");
printf("Your mission is to stop Pancratius by revealing his secret password.\n");
printf("the rules are the follows:\n");
printf("\n");
printf("1. In each round you try to guess the secret password (4 distinct digits)\n");
printf("2. After every guess you'll receive two hints about the password");
printf("\nHITS the number of digits in your guess witch were exactly right.\n");
printf("MISSES the number of digits in your guess witch belong to the password but weremiss-placed\n");
printf("3 - if you'll fail to guess the password after a certain number of rounds \tPancratius will buy all the gift to Hanukkah!!!\n");
printf("\n");
printf("Press Enter to continue...\n");
enter = getch();
if (enter = '\n'){
do{
system("cls");
printf("please choose level:\n");
printf("1 - easy (20 rounds)\n");
printf("2 - normal (15 rounds)\n");
printf("3 - hard (10 rounds)\n");
printf("4 - crazy (random number of rounds 5-25)\n");
printf("Make a choice:");
_flushall();
scanf("%1d",&choice);
if (choice == 1 || choice == 2 || choice == 3 || choice == 4 ){
switch(choice){
case (1):
system("cls");
returnV = levels(EASY);
break;
case (2):
system("cls");
returnV = levels(NORMAL);
break;
case (3):
system("cls");
returnV = levels(HARD);
break;
case (4):
system("cls");
returnV = levels(CRAZY);
break;
switch(returnV){
case('y'):
break;
case('n'):
return 0;
break;
}
}
}
else if (choice != 1 || choice != 2 || choice != 3 || choice != 4 ){
system("cls");
printf("please choose level:\n");
printf("1 - easy (20 rounds)\n");
printf("2 - normal (15 rounds)\n");
printf("3 - hard (10 rounds)\n");
printf("4 - crazy (random number of rounds 5-25)\n");
printf("Make a choice:");
_flushall();
scanf("%1d",&choice);
}
} while (returnV != 'n');
}
system("PAUSE");
}
header file to connect:
char levels (int level);
"levels" function:
#include <stdio.h>
#include "connect.h"
#include <time.h>
#include <stdlib.h>
char cases (int myCase, char crazyl);
char levels (int level) {
char crazyl = 'n',playerChoice;
switch (level){
case (20):
playerChoice = cases (level,crazyl);
printf ("\n%c\n",playerChoice);
return playerChoice;
break;
case (15):
playerChoice = cases (level,crazyl);
return playerChoice;
break;
case (10):
playerChoice = cases (level,crazyl);
return playerChoice;
break;
case (30):
crazyl = 'x';
level = rand() % (25 - 5 + 1);
playerChoice = cases (level,crazyl);
return playerChoice;
}
}
int ranGen (int code1,int code2, int code3, int code4);
char cases (int level,char crazyl)
{
unsigned int check;
char choise = 't',code1,code2,code3,code4;
if(level > 0){
while (level > 0){
if(crazyl == 'n'){
printf("Write your guess (only 1-6, no ENTER is needed) [%d guess left]\n",level);
}
else if (crazyl == 'x'){
printf("Write your guess (only 1-6, no ENTER is needed) [xxx guess left]\n");
}
code1 = getch();
printf("%c",code1);
code2 = getch();
printf("\t%c",code2);
code3 = getch();
printf("\t%c\t",code3);
code4 = getch();
printf("%c\n",code4);
if(code1 > '0' && code1 < '7' && code2 > '0' && code2 < '7' && code3 > '0' && code3 < '7' && code4 > '0' && code4 < '7'){
check = ranGen(code1,code2,code3,code4);
level--;
} else {
printf("\nnot a good number only numbers between 1-6\n");
level--;
}
if (level == 0) {
printf("FOR GOD SAKE WE LOST!!!\n");
printf("The secret password was:\n");
while (choise != 'y' || choise != 'n') {
if (choise != 'y' || choise != 'n') {
printf("keep playing? (y/n):");
_flushall();
scanf("%1c",&choise);
if (choise == 'y' || choise == 'n' ) {
return choise;
} else {
continue;
}
}
}
}
}
}
}
int ranGen(int code1,int code2, int code3, int code4) {
unsigned int ranCode[3],check,code[3];
code[0] = (int) code1 - 48;
code[1] = (int) code2 - 48;
code[2] = (int) code3 - 48;
code[3] = (int) code4 - 48;
do {
ranCode[0] = rand() % (6 - 1 + 1);
ranCode[1] = rand() % (6 - 1 + 1);
ranCode[2] = rand() % (6 - 1 + 1);
ranCode[3] = rand() % (6 - 1 + 1);
} while (code[0] != ranCode[0] || code[1] != ranCode[1] || code[2] != ranCode[2] || code[3] != ranCode[3]);
}
Now my problem is in ranGen function at "levels" paste.
I need to Generate 4 random numbers between 1-6 that different from each other
(1234 is ok, 1878 not ok, 2234 not ok either) and that the user will guess the digits while if he made a correct digits lets say:
generated code = 2345
guess = 1364
The user will get: 1 HIT and 1 MISS while if user will input 2222 he will get 1 HIT and 3 MISSES.
Now im pretty lost here and any help will be great.
I see the problems in your ranGen() function...
you have...
unsigned int ranCode[3],check,code[3];
ranCode[3] gives you 3 elements in that array. Array elements start counting at zero, so that would give you ranCode[0], ranCode[1] and ranCode[2]. If you want ranCode[3] and code[3], than you need to change the above line to...
unsigned int ranCode[4],check,code[4];
That gives you 4 elements, numbered from 0 to 3 (0, 1, 2 & 3 equals 4 numbers).
I'm also not certain what you are trying to do with...
ranCode[0] = rand() % (6 - 1 + 1);
...in your program, it will evaluate what is inside the brackets first, so 6 - 1 = 5, + 1 = 6. So in essence, that line will also look like ranCode[0] = rand() % 6; to the compiler and will give a number from 0 to 5. If you wish to add one to the result, you can use: ranCode[0] = (rand() % 6) + 1;, that would do the random number first, then add one to it for 1 to 6 (which is what I assume you wanted for a dice roll?).
Anyhow, you're passing code4 to a nonexistent element, out of range and probably corrupting memory somewhere, which would have undefined behaviour, possibly crash the system, or effect another variable's memory etc.

Coding Tic Tac Toe in C with only If statements

So the past week I've been attempting to learn some C, I was giving an exercise to code a really simplistic version of tic tac toe.
I have been asked:
Prompt two users to enter a ‘naught’ or a ‘cross’ respectively into one of the nine positions on the tic, tac, toe grid
After all 9 inputs have been made display the grid
To make it simpler: only when all 9 grid positions have been entered figure out if there is a winner (you can do this with a few ‘if’ statements)
.
I'm told this can be done with a few if statements, so far I have only learnt up to if's, including the basic int, char, float, double, ect.
So, what I'm not truly grasping in how to only use if statements to check if:
the position is already taken, if it has prompt the user to try again or place the current persons naught or cross in that position.
keep track of the positions the two users enter, so I know which position each of the two users they have entered to check those positions if its empty or not.
I feel like I am somewhat over thinking this but I am unsure, if anyone has got any help that'd be great.
This is what I've written so far:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char board[9];
int num;
printf("Today we are playing a game of Tic Tac Toe\n");
printf("To play the game you need to line up 3 of the same type 'x's or o's' in a line to win\n");
printf("Before we start, whoever wants to be 'X' starts first, and whoever wants to be 'O' starts second\n");
printf("The board looks like this\n");
printf("%d%c%d%c%d\n", 1, 124, 2, 124, 3);
printf("%d%c%d%c%d\n", 4, 124, 5, 124, 6);
printf("%d%c%d%c%d\n", 7, 124, 8, 124, 9);
printf("Lets begin");
printf("\n");
printf("\n");
printf("\n");
printf("Please enter a number between 1 - 9 ");
scanf("%d", &num);
board[num] = 'X';
printf("Please enter a number between 1 - 9 ");
scanf("%d", &num);
board[num] = 'O';
printf("Please enter a number between 1 - 9 ");
scanf("%d", &num);
board[num] = 'X';
printf("Please enter a number between 1 - 9 ");
scanf("%d", &num);
board[num] = 'O';
printf("Please enter a number between 1 - 9 ");
scanf("%d", &num);
board[num] = 'X';
printf("Please enter a number between 1 - 9 ");
scanf("%d", &num);
board[num] = 'O';
printf("Please enter a number between 1 - 9 ");
scanf("%d", &num);
board[num] = 'X';
printf("Please enter a number between 1 - 9 ");
scanf("%d", &num);
board[num] = 'O';
printf("Please enter a number between 1 - 9 ");
scanf("%d", &num);
board[num] = 'X';
return 0;
}
Note: Could I also ask that all answers be kept to only using if statements as that is as far as I am up too, and it is how the exercise is meant to be completed, although probably this is ten times easier with for/while and arrays.
Thank you!
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ROWS 3
#define COLUMNS 3
#define PLAYER_ONE 'X'
#define PLAYER_TWO 'O'
void board();//calling class required by C
int checkForWinner();//calling class required by C
char tic_tac_toe[ROWS][COLUMNS] =
{
{ '1', '2', '3'},
{ '4', '5', '6'},
{ '7', '8', '9'}
};
int main()
{
int isGameOn=1, currentPlayer=1, pick;
char checked;
do
{
board();
currentPlayer = (currentPlayer % 2) ? 1 : 2;
printf("Player %d, pick a number: ", currentPlayer);
scanf("%d", &pick);
checked = (currentPlayer == 1) ? 'X' : 'O';
isGameOn = checkForWinner();
if(pick == 1 && tic_tac_toe[0][0] == '1'){
tic_tac_toe[0][0] = checked;
if(isGameOn == 1)break;
}else if(pick == 2 && tic_tac_toe[0][1] == '2'){
tic_tac_toe[0][1] = checked;
if(isGameOn == 1){break;}
}else if(pick == 3 && tic_tac_toe[0][2] == '3'){
tic_tac_toe[0][2] = checked;
if(isGameOn == 1){break;}
}else if(pick == 4 && tic_tac_toe[1][0] == '4'){
tic_tac_toe[1][0] = checked;
if(isGameOn == 1){break;}
}else if(pick == 5 && tic_tac_toe[1][1] == '5'){
tic_tac_toe[1][1] = checked;
if(isGameOn == 1){break;}
}else if(pick == 6 && tic_tac_toe[1][2] == '6'){
tic_tac_toe[1][2] = checked;
if(isGameOn == 1){break;}
}else if(pick == 7 && tic_tac_toe[2][0] == '7'){
tic_tac_toe[2][0] = checked;
if(isGameOn == 1){break;}
}else if(pick == 8 && tic_tac_toe[2][1] == '8'){
tic_tac_toe[2][1] = checked;
if(isGameOn == 1){break;}
}else if(pick == 9 && tic_tac_toe[2][2] == '9'){
tic_tac_toe[2][2] = checked;
if(isGameOn == 1){break;}
}
else{
printf("Invalid moved");
}
isGameOn = checkForWinner();
if(isGameOn == 1){
printf("***************************************\n");
printf("The winner is player %d\n", currentPlayer);
printf("***************************************");
break;
}
currentPlayer++;
}while(isGameOn == -1);
board();
return(0);
}
/*Functions*/
/*Board display*/
void board()
{
printf("\n");
printf("%c\t %c\t %c\n", tic_tac_toe[0][0], tic_tac_toe[0][1], tic_tac_toe[0][2]);
printf("%c\t %c\t %c\n", tic_tac_toe[1][0], tic_tac_toe[1][1], tic_tac_toe[1][2]);
printf("%c\t %c\t %c\n", tic_tac_toe[2][0], tic_tac_toe[2][1], tic_tac_toe[2][2]);
}
/*Checking for winner*/
int checkForWinner()
{
/*checkign vertical line*/
for(int col=0; col<COLUMNS; col++){
int row=0;
if(tic_tac_toe[row][col] == tic_tac_toe[row+1][col] && tic_tac_toe[row+1][col] == tic_tac_toe[row+2][col]){
return 1;
}
}
/*checking horizontal line*/
if(tic_tac_toe[0][0] == tic_tac_toe[0][1] && tic_tac_toe[0][1] == tic_tac_toe[0][2]){
return 1;
}
else if(tic_tac_toe[1][0] == tic_tac_toe [1][1] && tic_tac_toe[1][1] == tic_tac_toe[1][2])
{
return 1;
}
else if(tic_tac_toe[2][0] == tic_tac_toe [2][1] && tic_tac_toe[2][1] == tic_tac_toe[2][2])
{
return 1;
}
else if(tic_tac_toe[0][0] == tic_tac_toe[1][1] && tic_tac_toe[1][1] == tic_tac_toe[2][2])/*diagonal*/
{
return 1;
}
else {
return -1;
}
}

The code for my program works once and doesn't loop- gets an error on codeblocks and crashes

The code is as follows:
guessing game , 10 guesses max, prompts user for replay. It crashes after one run. It works fine without the replay() module but then I can't incorporate the replay option. I have tried several different things to no avail. Please kindly help me resolve this in a timely manner. Help is appreciated in advance.
thanks
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void guessGame ();
int replay();
int main()
{
int selection;
printf("Welcome to the Number Guess Game! I choose a number between 1 and 100 and you\nhave only 10 chances to guess it!");
do
{
printf("\n\nok, I made my mind!");
guessGame();
replay();
}
while (replay() != 0);
printf("Thank you! have a nice day.\n");
return 0;
}
void guessGame()
{
int attempt,guess;
srand(time(NULL));
int r = rand () % 100 + 1;
for (guess = 1; (guess < 11 && attempt != r); guess = guess + 1)
{
printf("\nWhat is your guess> ");
scanf("%d",&attempt);
if (attempt < 1 || attempt > 100)
{
printf("Invalid guess!\n");
guess = guess - 1;
}
else
{
if (attempt > r && guess < 10)
printf("My number is smaller than %d\n",attempt);
else if (attempt < r && guess < 10)
printf("My number is larger than %d\n",attempt);
}
if ((guess < 9) && (attempt >= 1 && attempt <= 100))
printf("%d guesses left.\n",(10 - guess));
if ((guess == 9) &&(attempt >= 1 && attempt <= 100))
printf("%d guess left.\n",(10 - guess));
}
if (attempt == r)
{
printf("You did it! My number is %d.\nYou did it in %d guesses.\n",r,guess);
}
if (guess >= 10 && attempt != r)
{
printf("SORRY! you couldn't guess it with 10 guesses.\nMy number was %d. Maybe next time!\n",r);
}
}
int replay()
{
char selection;
printf("\nDo you want to play again");
scanf("%c",selection);
if (selection == 'N')
return 0;
else
return 1;
}
You're calling scanf() incorrectly. You need to give the address of the variables to store into:
scanf("%c", &selection);

Resources