Debugging my c program, number guessing game - c

I need help debugging this program and I don't know what is wrong. I am using putty and the Vi editor to run my program. This is my code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <malloc.h>
int main(void) {
int playerNumber = 0;
int number = 0;
int playerInput = 0;
int guess = 0;
char input;
char str[6] = {0};
int playerA = 0;
int playerB = 0;
int passA = 3;
int passB = 3;
int i = 0;
int playerTurn = 0;
srand(time(NULL));
playerNumber = 1 + rand() % 2; /* Random number is generated */
printf("\nPlayer %d goes fist\n", playerNumber);
printf("Player Number?\n");
while (playerNumber != playerInput) {
scanf("%d", &playerInput);
if (playerNumber != playerInput) printf("You Have to wait your turn.\nPlayer number?\n");
playerNumber = playerA;
if (playerA = 1) playerB = 2;
else playerB = 1;
srand(time(NULL));
number = 0 + rand() % 100; /* Random number is generated */
printf("Enter Your Guess, 0 - 100 or Pass: "); /* Input your guess */
while(number != guess) {
for(i = 1; i < 1000; i++) {
if (i%2 == 1) playerTurn = playerA;
else PlayerTurn = playerB;
scanf("%s", str);
if (strcmp(str, "pass") == 0) printf("Player Number?\n");
else {
guess = atoi(str);
if(guess < number) /* if the guess is lower, output: the guess is to low */
printf("Your guess was to low.\n Player Number:\n ");
else if(guess > number) /* if the guess is higher, output: the guess is to high */
printf("Your guess was to high.\n Player Number:\n ");
else /* is the guess is equial to the random number: Success!! */
printf("Yes!! you got it!\n");
return 0;
}
}
}
}
}
This is what I get as an error:
project2total.c: In function main':
project2total.c:49: error:PlayerTurn' undeclared (first use in this function)
project2total.c:49: error: (Each undeclared identifier is reported only once
project2total.c:49: error: for each function it appears in.)

C is case-sensitive. In your function, there is no PlayerTurn declared, but you seem to have declared playerTurn. Simply correcting the upper-case P to lower-case will work, assuming that this is actually the variable that you want to refer to. :)

Related

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

how to extract the even number from user input, and combine them as a new number in C program

test case:
input: 1234
output: 24
input: 2468
output: 2468
input: 6
output: 6
I have this code:
#include <stdio.h>
#include <math.h>
int main() {
int num;
printf("Enter a number: \n");
scanf("%d", &num);
int numberLength = floor(log10(abs(num))) + 1;
int inputNumberArray[numberLength];
int evenNumberCount = 0;
int even[10];//new even no. array
int i = 0;
do {
inputNumberArray[i] = num % 10;
num = num / 10;
i++;
} while (num != 0);
i = 0;
while (i < numberLength) {
if (inputNumberArray[i] % 2 == 0) {
evenNumberCount ++;
even[i] = inputNumberArray[i];
}
i++;
}
printf("array count %d\n", evenNumberCount);
i = 0;
for (i = 0; i < 8; i++) {
printf(" %d", even[i]);//print even array
}
i = 0;
int result = 0;
for (i = 0; i < 10; i++) {
if (evenNumberCount == 1) {
if (even[i] != 0) {
result = even[i];
} else {
break;
}
} else {
if (even[i] != 0) {
result = result + even[i] * pow(10, i);
} else
break;
}
}
printf("\nresult is %d", result);
/*
int a = 0;
a = pow(10, 2);
printf("\na is %d", a);
*/
}
when I enter number 1234, the result/outcome is 4, instead of 24.
but when I test the rest of test case, it is fine.
the wrong code I think is this: result = result + even[i] * pow(10, i);
Can you help on this?
Thanks in advance.
why do you have to read as number?
Simplest algorithm would be
Read as text
Validate
loop through and confirm if divisible by 2 and print live
next thing, have you try to debug?
debug would let you know what are doing wrong. Finally the issue is with indexing.
evenNumberCount ++; /// this is technically in the wrong place.
even[i]=inputNumberArray[i]; /// This is incorrect.
As the user Popeye suggested, an easier approach to accomplish this would be to just read in the entire input from the user as a string. With this approach, you can iterate through each letter in the char array and use the isdigit() method to see if the character is a digit or not. You can then easily check if that number is even or not.
Here is a quick source code I wrote up to show this approach in action:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char input[100] = { '\0' };
char outputNum[100] = { '\0' };
// Get input from user
printf("Enter a number: ");
scanf_s("%s", input, sizeof(input));
// Find the prime numbers
int outputNumIndex = 0;
for (int i = 0; i < strlen(input); i++)
{
if (isdigit(input[i]))
{
if (input[i] % 2 == 0)
{
outputNum[outputNumIndex++] = input[i];
}
}
}
if (outputNum[0] == '\0')
{
outputNum[0] = '0';
}
// Print the result
printf("Result is %s", outputNum);
return 0;
}
I figured out the solution, which is easier to understand.
#include <stdio.h>
#include <math.h>
#define INIT_VALUE 999
int extEvenDigits1(int num);
void extEvenDigits2(int num, int *result);
int main()
{
int number, result = INIT_VALUE;
printf("Enter a number: \n");
scanf("%d", &number);
printf("extEvenDigits1(): %d\n", extEvenDigits1(number));
extEvenDigits2(number, &result);
printf("extEvenDigits2(): %d\n", result);
return 0;
}
int extEvenDigits1(int num)
{
int result = -1;
int count = 0;
while (num > 1) {
int digit = num % 10;
if (digit % 2 == 0) {
result = result == -1 ? digit : result + digit * pow(10, count);
count++;
}
num = num / 10;
}
return result;
}
}
You are overcomplicating things, I'm afraid.
You could read the number as a string and easily process every character producing another string to be printed.
If you are required to deal with a numeric type, there is a simpler solution:
#include <stdio.h>
int main(void)
{
// Keep asking for numbers until scanf fails.
for (;;)
{
printf("Enter a number:\n");
// Using a bigger type, we can store numbers with more digits.
long long int number;
// Always check the value returned by scanf.
int ret = scanf("%lld", &number);
if (ret != 1)
break;
long long int result = 0;
// Use a multiple of ten as the "position" of the resulting digit.
long long int power = 1;
// The number is "consumed" while the result is formed.
while (number)
{
// Check the last digit of what remains of the original number
if (number % 2 == 0)
{
// Put that digit in the correct position of the result
result += (number % 10) * power;
// No need to call pow()
power *= 10;
}
// Remove the last digit.
number /= 10;
}
printf("result is %lld\n\n", result);
}
}

Program loops infinitely if a non-integer is entered, and does not accept plural digit inputs

Assigned task is to ask for # of values, and then at the end output the minimum, maximum, and average values and at this point I've run out of bug fixes
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main()
{
int ErrorDetection = 1;
char valCounter;
int valnumber;
int Incrementer;
int StoredValue;
int MinimumValue = 100;
int MaximumValue = 0;
float Average;
int AddToStored;
int Sum = 0;
printf("MIN, MAX, and MEAN CALCULATOR\n\n");
while (ErrorDetection != 0)
{
printf("How many values are to be entered?\n");
scanf("%s", &valCounter);
if (valCounter > '0' && valCounter < '9') {
ErrorDetection = 0;
}
else {
ErrorDetection = 1;
printf("INPUT ERROR!\n");
}
valCounter = valCounter - 47;
}
for (Incrementer = 1; Incrementer < valCounter; Incrementer++)
{
ErrorDetection = 1;
while (ErrorDetection != 0) {
printf("Value %d: ", Incrementer);
scanf(" %d", &StoredValue);
if (StoredValue > 0 && StoredValue < 9) {
ErrorDetection = 0;
}
else {
ErrorDetection = 1;
printf("INPUT ERROR!\n");
continue;
}
}
if (StoredValue > MaximumValue) {
MaximumValue = StoredValue;
}
if (StoredValue <= MinimumValue) {
MinimumValue = StoredValue;
}
Sum = Sum + StoredValue;
}
valCounter = valCounter - 1;
Average = (float)Sum / (float)valCounter;
printf(
"Minimum value is %d, maximum value is %d, and average value is %g.\n",
MinimumValue, MaximumValue, Average
);
}
If you input a 2 digit number things begin to breakdown, but at the same time I don't know how to go through with errorchecking if I allow multiple digit answers, as I make use of ASCII conversions to check if an input is a number or not.
You have undefined behavior here.
char valCounter;
scanf("%s", &valCounter);
You have declared valCounter as char type but trying to read string type.
Hence change the scanf to.
scanf("%c", &valCounter);
I would suggest you declare valCounter as int
int valCounter;
scanf("%d", &valCounter);
in that case your if will become.
if ((valCounter > 0) && (valCounter < 9))
and you don't need
valCounter = valCounter - 47; //remove
Also your for loop should start from 0 instead of 1
for(Incrementer = 1 ; Incrementer < valCounter; Incrementer++)
should be
for(Incrementer = 0 ; Incrementer < valCounter; Incrementer++)
Your problem is here.
char valCounter;
scanf("%s", &valCounter);
You're telling scanf to read a string, but you're passing it the address of a character. You should be asking for an integer, and giving it the address of an integer.
int valCounter;
scanf("%d", &valCounter)
There's more information here, including reasons why scanf might not be the best idea:
How to scanf only integer?

I am getting a _\377 in my output

I have a school assignment to make a hangman game. The game works how I want it to except for one small glitch. If the user entered word is 4 letters or less, the hidden word is displayed with an extra "_\377" at the end. When the user entered word is 5 letters or more, then there is no glitch. I am hoping that someone would be kind enough to help me trouble shoot the problem. Thanks in advance!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int letterfinder(char string[], char a, int vari)
{
int length = strlen(string);
int i = vari;
int val = 0;
while( i <= length && val != 1)
{
if( string[i] == a)
{
val = 1;
}
i++;
}
if( val == 0)
{
return 100;
}
else
{
return i;
}
}
int main()
{
char inWord[] = "1111111111111111111111111111";
char outWord2[] = "1111111111111111111111111111";
char guess;
int gameover = 0;
int trys = 10;
int vari = 0;
printf("Please enter a word: ");
gets(inWord);
printf("%s\n", inWord);
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
int i2 = 0;
int j2 = 0;
int i3 = 0;
i2 = strcspn(inWord, outWord2);
char outWord[80];
while(i3 < i2)
{
outWord[i3] = '1';
i3++;
}
while(j2 < i2)
{
outWord[j2] = '-';
j2++;
}
puts(outWord);
while(gameover != 1 )
{
printf("What is your guess: ");
scanf("%s", &guess);
vari = 0;
if(letterfinder(inWord, guess, vari) == 100)
{
printf("Wrong!");
trys--;
printf("You have %d attempts left\n", trys);
if(trys == 0)
{
gameover = 1;
printf("You ran out of attempts. Game over\n");
}
}
else
{
outWord[(letterfinder(inWord, guess, vari) - 1)] = guess;
vari = (letterfinder(inWord, guess, vari));
while(letterfinder(inWord, guess, vari) != 100)
{
outWord[(letterfinder(inWord, guess, vari) - 1)] = guess;
vari = letterfinder(inWord, guess, vari);
}
puts(outWord);
}
int value = 0;
i3 = 0;
while( i3 <= i2)
{
if( outWord[i3] == '-')
{
value = 1;
}
i3++;
}
if(value != 1)
{
printf("Congratulations, you have guessed the word!\n");
gameover = 1;
}
}
return 0;
}
Your code has Undefined Behaviour. In the cases it "works" it is only by chance/luck. char guess; scanf("%s", &guess); That causes memory corruption as you are writing a string to a variable that can only hold a single char. Even a single letter guess will require two characters to store as all C strings are NUL terminated.
– kaylum

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