Morra - Odds and Even - c

Im writing a code that evolves the computer and I entering numbers and added them together to see who wins.
There are three problems:
Problem 1: When I try to recall the 'main()' function the 'game()' wont appear.
Problem 2: I can't seem to run the code forever until user decides to stop.
Problem 3: The point system isn't accurate enough.
Any help would be grateful.
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int human_fingers;
int comp_fingers;
int menu_choice;
int answer;
int count = 1;
int point1 = 0, point2 = 0;
int total;
void intro() {
printf("Welcome to Morra - Odds and Even!\n\n");
printf("The rules of the game are pretty simple\n");
printf("You and the computer will pick a side each round\n");
printf("You must enter a number and the total sum will determine the winner!\n\n");
}
void example() {
printf("You picked even, by default the computer will be odd this round\n");
printf("You entered 3 and the computer entered 5\n");
printf("3 + 5 = 8, so you win because you choose even!\n");
}
void game() {
while(count < 7)
{
count = count + 1;
printf("Enter a number to choose a side:\n");
printf("Even [1] / Odd [2]\n");
scanf("%d", &menu_choice);
while((menu_choice<1) || (menu_choice>2)){
printf("Invalid entry, please Enter 1-2: ");
scanf("%d",&menu_choice);}
if(menu_choice == 1)
{
printf("The computer will be odd this turn\n");
printf("\nPlease enter an number (1-10)\n");
scanf("%d", &human_fingers);
while((human_fingers<1) || (human_fingers>10)){
printf("Invalid entry, please enter 1-10:");
scanf("%d",&human_fingers);}
printf("Computer is choosing a number....\n");
srand(time(NULL));
comp_fingers = rand() % 10 + 1;
printf("You: %d\n", human_fingers);
printf("Computer: %d\n", comp_fingers);
int result;
result = human_fingers + comp_fingers;
printf("Total is %d\n", result);
total = result % 2;
if(total == 0)
{
printf("This turn goes to You!\n\n");
printf("You: %d\n", point1 + 1);
printf("Computer: %d\n\n", point2 + 0 );
point1++;
}
else
{
printf("This turn goes to Computer!\n\n");
printf("You: %d\n", point1 + 0 );
printf("Computer: %d\n\n", point2 + 1);
point2++;
}
}
if(menu_choice == 2)
{
printf("The computer will be even this turn\n");
printf("\nPlease enter an number (1-10)\n");
scanf("%d", &human_fingers);
while((human_fingers<1) || (human_fingers>10)){
printf("Invalid entry, please enter 1-10:");
scanf("%d",&human_fingers);}
printf("Computer is choosing a number...\n");
srand(time(NULL));
comp_fingers = rand() % 10 + 1;
printf("You: %d\n", human_fingers);
printf("Computer: %d\n", comp_fingers);
int result;
result = human_fingers + comp_fingers;
printf("Total is %d\n", result);
total = result % 1;
if(total == 0)
{
printf("This turn goes to Computer!\n\n");
printf("You: %d\n", point1 + 0);
printf("Computer: %d\n\n", point2 + 1);
point1++;
}
else
{
printf("This turn goes to You!\n\n");
printf("You: %d\n", point1 + 1);
printf("Computer: %d\n\n", point2 + 0 );
point2++;
}
}
}
if(point1 > point2)
printf("You won, you beat the computer!\n");
else
printf("Unlucky the computer won!\n");
}
void end(){
printf("Game has ended!\n");
}
int main()
{
intro();
printf("Would you like an example to demostrate?\n");
printf("Yes [1] / No [2]\n");
scanf("%d", &answer);
if( answer == 1 )
{ example();
printf("\n\n");
game();
}
else
{ printf("\n\n");
game();
}
end();
printf("Would you like play another game?\n");
printf("Yes [1] / No [2]\n");
scanf("%d", &answer);
if( answer == 1)
{
main();
}
else {
printf("Thanks for playing MORRA - ODDS AND EVEN.");
}
return 0;
}

Why not:
int main()
{
for(;;) // Infinite loop. -- equivalent of while(1) if you prefer.
{
intro();
printf("Would you like an example to demostrate?\n");
printf("Yes [1] / No [2]\n");
scanf("%d", &answer);
if( answer == 1 )
{ example();
printf("\n\n");
game();
}
else
{ printf("\n\n");
game();
}
end();
printf("Would you like play another game?\n");
printf("Yes [1] / No [2]\n");
scanf("%d", &answer);
if( answer != 1)
{
printf("Thanks for playing MORRA - ODDS AND EVEN.");
break;
//^___ exit the infinite loop.
}
}
return 0;
}
Recalling the main you should definitely not, in almost any case. Just create loops if you want to repeat everything.

Related

Issues with do while loop

The code allows you to choose between star and delta resistive network conversions. There is also an exit option. I wanted to validate the values for R_a, R_b, R_c etc. however, I have run into some trouble with my do while loop. The lower limit is 1000 and the upper is 1000000.
I intend to have the program carry on if the input is within range and prompt for another input from the user if it is not. However, as of now, the program continues if the value is within range, but also continues after giving a warning when it is not - when I want it to loop back to the first input prompt.
Once correct, I will add the loop to all inputs.
If anyone is able to fix/find the issue, it would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
printf("\n\n\t\tDelta and Star Converter\n\n\n");
int choice, num, i;
unsigned long int fact;
while(1)
{
printf("1. Star \n");
printf("2. Delta\n");
printf("0. Exit\n\n\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:;
float R_a=0,R_b=0,R_c=0,R_ab,R_bc,R_ac;
printf("Please enter the value of the Star connected resistors:\n");
do {
printf("R_a = ");
scanf("%f",&R_a);
if (R_a > 1000000) {
printf("Please");
} else if (R_a < 1000) {
printf("Number to low\n");
}else {
}
}while(R_a = -0);
printf("R_b = ");
scanf("%f",&R_b);
printf("R_c = ");
scanf("%f",&R_c);
R_ab=R_a+R_b+(R_a*R_b)/R_c;
R_bc=R_b+R_c+(R_b*R_c)/R_a;
R_ac=R_a+R_c+(R_a*R_c)/R_b;
printf("the equivalent Delta values are: \n");
printf("R_ab = %.2f Ohms\n",R_ab);
printf("R_bc = %.2f Ohms\n",R_bc);
printf("R_ac = %.2f Ohms\n",R_ac);
break;
case 2:;
printf("Please enter the values of the Delta connected resistors:\n");
printf("R_ab = ");
scanf("%f",&R_ab);
printf("R_bc = ");
scanf("%f",&R_bc);
printf("R_ac = ");
scanf("%f",&R_ac);
R_a = (R_ab*R_ac)/(R_ab + R_bc + R_ac);
R_b = (R_ab*R_bc)/(R_ab + R_bc + R_ac);
R_c = (R_ac*R_bc)/(R_ab + R_bc + R_ac);
printf("the equivalent Star values are: \n");
printf("R_a = %.2f Ohms\n",R_a);
printf("R_b = %.2f Ohms\n",R_b);
printf("R_c = %.2f Ohms\n",R_c);
break;
case 0:
printf("\n\nAdios!!\n\n\n");
exit(0); // terminates the complete program execution
}
while (0) ; }
printf("\n\n\t\t\tThank you!\n\n\n");
return 0;
}
while(R_a = -0)
= is the assignment operator. This assigns -0 to R_a, and evaluates to the same falsy value, ending the loop.
Change the do ... while to an infinite loop, and break the loop when the value is in range.
while (1) {
printf("R_a = ");
if (1 != scanf("%f", &R_a))
exit(EXIT_FAILURE);
if (R_a > 1000000) {
fprintf(stderr, "Number too high.\n");
} else if (R_a < 1000) {
fprintf(stderr, "Number to low.\n");
} else {
break;
}
}

I don't understand why a part of my code isn't connecting to the rest

I have an assignment for a coding class where I have to create a code for the rock, paper scissors game, with different functions, calling rock 1 and paper 2 and scissors 3.
I also have to create a menu, then have the person chose, save that, then have the computer generate a random number and then print what they played, what the computer played and who won.
However, the last part isn't connecting, and I don't understand where I went wrong, so I'm really really lost and I would really appreciate any help.
For reference here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float response, choice;
int playerInput() {
printf(" For rock press 1\n ");
printf("For paper press 2\n ");
printf("For scissors press 3\n ");
printf("What would you like to play? ");
scanf("%f", &response);
return response;
}
int computerChoice() {
int lower = 1, upper = 3, count = 1;
srand(time(0));
printf("The random number that: ");
for (int i = 0; i < count; i++) {
int choice = (rand() % (upper - lower + 1)) + lower;
printf("the computer chose is %d ", choice);
}
return choice;
}
void displayWinner() {
if (response == choice) {
printf("You tied, lets play again");
}
if (choice == 1 && response == 2) {
printf("you won");
}
if (choice == 1 && response == 3) {
printf("computer won");
}
if (choice == 2 && response == 1) {
printf("computer won");
}
if (choice == 2 && response == 3) {
printf("you won");
}
if (choice == 3 && response == 1) {
printf("you won");
}
if (choice == 3 && response == 2) {
printf("computer won");
}
}
int main(void) {
int pinput, cinput, dinput;
pinput = playerInput();
cinput = computerChoice();
displayWinner();
return 0;
}
int choice =
But why declare a local with the same name as a global. I suggest that's your first mistake.
On fixing it you'll find the next line prints garbage because it's now printing a %d from a float (that got widened to a double).

Loop terminates because of condition, but the condition is necessary?

I'm working on a program that is a guessing game.
The problem is I have to set remainingguesses to 0 so that the amount of guesses a person can make decreases by 1. But at the same time the condition set in the loop is based on the remainingguesses not being 0. Meaning once it is 0 the loop terminates and moves on.
I don't know how to solve this while making the condition in the loop work properly.
Here's the loop in question:
printf( "Type the number of guesses that player 2 gets and press return: \n");
scanf("%d",&guesses);
remainingguesses = 0;
while (remainingguesses != 0) {
printf("Player 2: Type your guess and press return (guesses remaining:%d):\n",remainingguesses);
scanf(" %d",&secretnumberguess);
remainingguesses = guesses - 1;
if (secretnumberguess > secretnumber) {
printf("Your guess was greater than the secret number.\n");
}
else if (secretnumberguess < secretnumber){
printf("Your guess was less than the secret number.\n");
}
else{
printf("Your guess was equal to the secret number. You win!\n");
}
}
if (remainingguesses == 0)
printf("Sorry you are out of guesses. You lose.\n");
Here's the full code in question if needed:
#include <stdio.h>
int main()
{
int secretnumber;
int guesses;
int secretnumberguess;
int remainingguesses;
while (1) {
printf("Player 1: Type a number between 0 and 99 and press return:\n");
scanf(" %d",&secretnumber);
if (secretnumber > 99 || secretnumber < 0) {
printf("Secret number cannot be greater than 99 or below 0.\n");
continue;
}
break;
}
printf( "Type the number of guesses that player 2 gets and press return: \n");
scanf("%d",&guesses);
remainingguesses = 0;
while (remainingguesses != 0) {
printf("Player 2: Type your guess and press return (guesses remaining:%d):\n",remainingguesses);
scanf(" %d",&secretnumberguess);
remainingguesses = guesses - 1;
if (secretnumberguess > secretnumber) {
printf("Your guess was greater than the secret number.\n");
}
else if (secretnumberguess < secretnumber){
printf("Your guess was less than the secret number.\n");
}
else{
printf("Your guess was equal to the secret number. You win!\n");
}
}
if (remainingguesses == 0)
printf("Sorry you are out of guesses. You lose.\n");
return 0;
Simplified code:
#include <stdio.h>
int main()
{
int secretnumber;
int guesses;
int secretnumberguess;
int flag=0;
while (1) {
printf("Player 1: Type a number between 0 and 99 and press return:\n");
scanf(" %d",&secretnumber);
if (secretnumber > 99 || secretnumber < 0) {
printf("Secret number cannot be greater than 99 or below 0.\n");
continue;
}
break;
}
printf( "Type the number of guesses that player 2 gets and press return: \n");
scanf("%d",&guesses);
while (guesses > 0 && flag==0) {
printf("Player 2: Type your guess and press return (guesses remaining:%d):\n",guesses);
scanf(" %d",&secretnumberguess);
guesses=guesses - 1;
if (secretnumberguess > secretnumber) {
printf("Your guess was greater than the secret number.\n");
}
else if (secretnumberguess < secretnumber){
printf("Your guess was less than the secret number.\n");
}
else{
printf("Your guess was equal to the secret number. You win!\n");
flag=1;
}
}
if (guesses == 0 && flag==0)
printf("Sorry you are out of guesses. You lose.\n");
return 0;
}
Here is probably a simple way of coding this.
read guesses;
while (guesses > 0) {
read input;
if (input == secret) {
print "you win!!";
return;
}
else {
print "try again!";
}
guesses--;
}
print "Sorry! You are out of guesses";

Vending Machine - Logic Error

// My issue is a rather specific one, the code compiles "Insert Coins" "Coin
//not accepted " indefinitely and doesn't allow input at all. I've tried this
//program with "While" only loops and "do" loops and it always compiles
//indefinitely without allowing input.I'm trying to figure out where my logic
//error is and possibly a simpler solution if possible. Thanks.
#include <stdio.h>
int main(void){
int money, drink_selection, coins;
money = 0;
do{ //ISSUE HERE??
do{ //OR HERE??
printf("Insert Coins: "); //REPEATS THIS
scanf("%d" ,&coins); //DOESNT ALLOW THIS
if(coins == 0 || coins == 5 || coins == 10 || coins == 25)
{
money +=coins;
}
else {
printf("Coin not accepted \n");//REPEATS INDEFINITELY
}
}while(coins != 0); // looping here?
printf("Please select from the following menu: 1 - Coffee or 2 - Tea ) \n");
printf("Enter your choice: \n");
scanf("%d", &drink_selection);
switch(drink_selection){
case 1:
printf("You have selected coffee as your choice. \n");
money-=25;
if (money >=0){
printf("Please take your coffee. \n");
}
break;
case 2:
money-=15;
if (money >= 0){
printf("Tea dispensing \n");
}
break;
default:
printf("Ivalid input");
break;
}
if (money > 0){
printf("Your change is %d cents ", &coins);
}
else if (money < 0){
printf("Insufficient amount, your change is: %d", &coins);
}
}while(money == 0); //POSSIBLY ISSUE IS HERE?
return 0;
}

How can I restart the program in C?

Hi I'm new to C and I wrote a simple program. I want to restart the program if the user picked the wrong choice, here is the code:
#include <stdio.h>
#include <cs50.h>
int main(void){
char choices;
float math, pc, svt, eng, philo;
do {
do {
printf("Enter your math score: ");
math = GetFloat();
}
while( math>20 || math<0);
do {
printf("Enter your pc score: ");
pc = GetFloat();
}
while(pc>20 || pc<0);
do {
printf("Enter your svt score: ");
svt = GetFloat();
}
while(svt>20 || svt<0);
do {
printf("Enter your eng score: ");
eng = GetFloat();
}
while(eng>20 || eng<0);
do {
printf("Enter your philo score: ");
philo = GetFloat();
}
while(philo>20 || philo<0);
printf("Are you pc or sm?\n");
printf("Write 1 for pc. 2 for sm\n");
int choice = GetInt();
if(choice == 1){
float score = (math*7 + pc*7 + svt*7 + eng*2 + philo*2)/25;
printf("Your score is %.2f\n", score);
}
else if(choice == 2){
float score = (math*9 + pc*7 + svt*3+ eng*2 + philo*2)/23;
printf("Your score is %.2f\n", score);
}
else{
printf("You've picked the wrong choice \n");
}
printf("Do you want to try it again? (Y/N) ");
choices = getchar();
while (choices != '\n' && getchar() != '\n') {};
} while (choices == 'Y' || choices == 'y');
}
So what I mean here, I want to insert the code in the else block to restart the program and give the user another time. It would be very nice if I can just make him choose again between 1 or 2.
If you have any suggestions or improvement please don't hesitate to comment.
Thanks :)
What you need is do while loop surrounding choice code:
int choice;
do {
choice = GetInt();
if (choice == 1) {
float score = (math*7 + pc*7 + svt*7 + eng*2 + philo*2)/25;
printf("Your score is %.2f\n", score);
}
else if (choice == 2) {
float score = (math*9 + pc*7 + svt*3+ eng*2 + philo*2)/23;
printf("Your score is %.2f\n", score);
}
else {
printf("You've picked the wrong choice, try again.\n");
}
} while(choice < 1 || choice > 2)
You already have a loop to try again, you can reuse that loop to get the user input again for choice. So, if the user enters choice other than 1 or 2, you can set choices = Y and redo the loop. without asking for user input.
Code is below.
#include <stdio.h>
#include <cs50.h>
int main(void)
{
char choices;
float math, pc, svt, eng, philo;
do
{
// Other code here
int choice = GetInt();
if(choice == 1){
float score = (math*7 + pc*7 + svt*7 + eng*2 + philo*2)/25;
printf("Your score is %.2f\n", score);
}
else if(choice == 2){
float score = (math*9 + pc*7 + svt*3+ eng*2 + philo*2)/23;
printf("Your score is %.2f\n", score);
}
else{
printf("You've picked the wrong choice \n");
choices = 'Y';
}
if ((choice == 1) || (choice == 2))
{
printf("Do you want to try it again? (Y/N) ");
choices = getchar();
while (choices != '\n' && getchar() != '\n') {};
}
} while (choices == 'Y' || choices == 'y');
}
Well, if somebody reading this has to actually restart their program for a more nontrivial reason (e.g. an exception handler, like me a couple of minutes ago), there is a portable way to to so in C, like so:
#include <setjmp.h> //from C standard library
jmp_buf restart_env;
int main() {
//some initialization you don't want to repeat
if(setjmp(restart_env)) {
//restarted, do whatever
}
//the code
}
void evenFromAnotherFunction() {
//...
if(something_that_justifies_this_approach) {
longjmp(restart_env, 1); //this restarts the program
//unreachable
}
//...
}
Note that you had better not use this if there's a better way. Using setjmp is bound to produce extremely annoying bugs. If you have no choice, remember that some data may preserve the value it had before calling setjmp for the first time, and some may not, so don't assume anything.

Resources