So I am making a Craps game in my C class and am having trouble with a particular point. If a user rolls a 4, 5, 6, 8, 9 or 10 that is considered a point. If the user rolls a 7 after rolling a point they lose. My problem is if a user rolls a point, and then they roll again and the sum is equal to their point they win. My program works if they roll a 7 after their point, but not if they roll sum of their point again.
Sorry I know the question is confusing.
case 1: printf("Enter your name here: ");
scanf("%s", name);
for(j = 0; j < i; j++){
if(strcmp(name, pl[j].players)==0){
set = 1;
printf("* * * * * * * * * *\n");
printf("Press ENTER to roll the dice");
getchar();
getchar();
dice = (rand()%(6-1+1)+1);
dice1 = (rand()%(6-1+1)+1);
sum = dice + dice1;
printf("\nThe values of the dice are %d and %d whose sum is %d\n", dice, dice1, sum);
if(sum == 7 || sum == 11)
{
win++;
pl[j].wincount = win;
pl[j].balance = pl[j].balance + 10;
printf("\nYou win the game. \nYour balance is now: %d\n", pl[j].balance);
break;
}
else if((sum == 2) || (sum == 3) || (sum == 12))
{
lose++;
pl[j].balance = pl[j].balance-1;
printf("You lose :(\nYou balance is now: %d\n", pl[j].balance);
dice = (rand()%(6-1+1)+1);
dice1 = (rand()%(6-1+1)+1);
sum = dice + dice1;
break;
}
else
{
do
{
printf("\nYou rolled a 'point' you must now roll again.\n");
printf("* * * * * * * * * *\n");
printf("Press ENTER to roll the dice again");
getchar();
getchar();
dice = (rand() % (6 - 1 + 1) + 1);
dice1 = (rand() % (6 - 1 + 1) + 1);
sum = dice + dice1;
printf("\nThe values of the dices are %d and %d whose sum is %d\n", dice, dice1, sum);
}while((sum != 7) && (sum != point));
if(sum == 7)
{
lose++;
pl[j].balance = pl[j].balance - 1;
printf("You lose :(\nYou balance is now: %d\n", pl[j].balance);
}
else if(sum == point)
{
win++;
pl[j].wincount = win;
pl[j].balance = pl[j].balance + 10;
printf("\nYou won the game, you rolled your point \nYour balance is now: %d\n", pl[j].balance);
}
}
}
}
Related
I'm trying to make a craps game where there would be dice rolling to check if it hits the target. I can't be sure what's wrong with my while loop but it keeps running even when the while loop requirement is false. Here is my code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int rollDice() {
int sum, d1, d2;
sum = 0;
d1 = (rand() % 6) + 1;
d2 = (rand() % 6) + 1;
sum = d1 + d2;
printf("Player rolled %d + %d = %d\n", d1, d2, sum);
return sum;
}
int main() {
int player, target, tries;
srand(time(NULL));
tries = 0;
player = rollDice();
target = player;
if (player == 7 || player == 11) {
printf("Player wins.\n");
}
else if (player == 2 || player == 3 || player == 12) {
printf("Player loses.\n");
} else {
printf("Point is %d. The game continues: \n", target);
player = rollDice();
}
printf("Player is %d, Target is %d.\n", player, target);
while (target != player || player != 7) {
player = rollDice();
}
return 0;
}
The game ends when a 7 is thrown or when the player's score is thrown again. Hence the while test should be while (target != player && player != 7), not while (target != player || player != 7).
You must also move the while loop inside the last else block.
Here is a modified version:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int rollDice(void) {
int sum, d1, d2;
sum = 0;
d1 = rand() % 6 + 1;
d2 = rand() % 6 + 1;
sum = d1 + d2;
printf("Player rolled %d + %d = %d\n", d1, d2, sum);
return sum;
}
int main() {
int player, target, tries;
srand(time(NULL));
tries = 0;
player = rollDice();
target = player;
if (player == 7 || player == 11) {
printf("Player wins.\n");
} else
if (player == 2 || player == 3 || player == 12) {
printf("Player loses.\n");
} else {
printf("Point is %d. The game continues:\n", target);
player = rollDice();
printf("Player is %d, Target is %d.\n", player, target);
for (;;) {
if (player == 7) {
printf("Pass loses\n");
break;
}
if (player == target) {
printf("Pass wins\n");
break;
}
player = rollDice();
}
}
return 0;
}
I have followed everything in the book, yet my average score fails me, every single time. I have debugged my program multiple times, in vain.
My minimal executable code:
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX_TESTS 5
#define MAXQUESTION 10000
bool myread(const char * format, void * address) {
char buffer[1024];
fgets(buffer, sizeof buffer, stdin);
return sscanf(buffer, format, address) == 1;
}
struct struc {
int a;
int b;
int c;
int add;
int grade;
};
int sj(int n) {
int t;
t = rand() % n;
return t;
}
void ctm_i(struct struc * t) {
{
t -> a = sj(101);
t -> c = sj(4);
if (t -> c == 1) {
t -> b = sj(101 - (t -> a));
t -> add = (t -> a) + (t -> b);
} else {
t -> b = sj((t -> a) + 1);
t -> add = (t -> a) - (t -> b);
}
t -> grade = 0;
}
}
void tcm_i(struct struc * t, int n) {
int ad;
printf(" ***********************************************************************"
"*********\n");
printf(" ......................................................................."
".........\n");
printf(" Question %d\n\n", n + 1);
printf(" You have 3 attempts for this question\n\n");
if (t -> c == 1)
printf(" %d+%d= ", t -> a, t -> b);
else
printf(" %d-%d= ", t -> a, t -> b);
myread(" %d", & ad);
if (ad == t -> add)
{
t -> grade = 10;
printf(" You earned 10 marks\n\n");
} else {
printf("\n Incorrect, you have 2 attempts remaining\n\n");
printf(" ");
myread(" %d", & ad);
if (ad == t -> add)
{
t -> grade = 7;
printf(" You earned 7 marks\n\n");
} else {
printf("\n Incorrect, you have 1 attempt remaining\n\n");
printf(" ");
myread(" %d", & ad);
if (ad == t -> add)
{
t -> grade = 5;
printf(" You earned 5 marks\n\n");
} else {
t -> grade = 0;
printf("\n Failure, 0 mark\n\n");
printf("\n The correct answer is %d\n\n", t -> add);
}
}
}
printf(" ......................................................................."
".........\n");
printf(" ***********************************************************************"
"*********\n");
}
void quiz(char name[]) {
int rounds = 0;
int highest = 0;
int lowest = INT_MAX;
float allScore = 0;
float avg = 0.0;
int i, j, g = 0;
struct struc test[MAX_TESTS];
srand((unsigned) time(NULL));
for (;;) {
rounds++;
for (i = 0; i < MAX_TESTS; i++) // generate all questions
{
ctm_i( & test[i]);
for (j = 0; j < i; j++)
if (test[i].a == test[j].a && test[i].b == test[j].b && test[i].c == test[j].c)
//if question is already present
ctm_i( & test[i]); //then re-generate
}
printf("\n Are you ready? Press Enter key to continue. ");
myread("", NULL);
for (i = 1; i <= 5; i++) {
printf(" *******************************************************************"
"**"
"***********\n");
printf(" ..................................................................."
".."
"...........\n");
}
// Take quiz
for (i = 0; i < MAX_TESTS; i++)
tcm_i( & test[i], i);
printf(" End\n\n");
bool done = false;
bool unsure = true;
bool showS = true;
while (unsure) {
unsure = false;
puts("\n");
if (showS) {
puts(" Enter 'S' to show results");
}
puts(" Enter 'P' to play another round");
puts(" Enter 'R' to return to main menu");
char choice;
printf(" ");
myread(" %c", & choice);
printf("\n");
if (choice == 'r' || choice == 'R') {
done = true;
} else {
///////////////////////// Changes /////////////
g = 0;
// calculate total score for current round
for (i = 0; i < MAX_TESTS; i++) {
g += test[i].grade; //add score of each question
}
allScore += g; //add current round's score to total
avg = allScore / rounds; //average of all rounds
if (g > highest) {
highest = g;
}
if (g < lowest) {
lowest = g;
}
if (choice == 'S' || choice == 's') {
showS = false;
if (rounds == 1) {
printf(" Final score: %d/100\n", g); //display round score
printf(" ****** Player: %s ******\n", name);
} else {
printf(" Round %d score: %d/100\n", rounds, g); //display round score
printf(" Highest score: %d/100\n", highest);
printf(" Lowest score : %d/100\n", lowest);
printf(" Average score: %f/100\n", avg);
printf(" ****** Player: %s ******\n", name);
}
unsure = true;
} else if (choice == 'P' || choice == 'p') {
/// nothing to be done here
//we will display next test
} else {
puts(" Invalid input!");
unsure = true;
}
////////////////////////////////////
}
}
if (done)
break;
}
}
int main() {
char i1 = '1';
char name[25]; // ig;
printf("\n Welcome");
printf("\n");
while (i1 != 0) {
printf("\n");
//printf(" **********************Welcome %s! *********************\n", name);
printf(" ************************ Main Menu of Maths Quiz ***************************\n");
printf(" * 1.Enter Quiz *\n");
printf(" * 2.Quit *\n");
printf(" ****************************************************************************\n");
printf(" Please choose one from 1-2:\n");
printf(" ");
myread(" %c", & i1);
switch (i1) {
case '1':
printf("\n Enter Quiz:\n");
quiz(name); // calling quiz function defined in file "maincode.c"
break;
case '2':
printf(" Quit.\n\n");
}
}
return 0;
}
Hope that fulfilled the definition of an MRE.
So I have snipped off some bombastic lines, and the ones left are the important ones, I think.
The problem is in your function quiz, and in particular in the while (unsure) loop. In this loop, you add the most recent score to the running total:
allScore += g; //add current round's score to total
This should happen once per round played. But if the user inputs "S" or something invalid, your program sets
unsure = true;
which means the loop will run again, before the next round is played. It then adds the most recent score a second time to the grand total.
The most logical solution would be to move all the calculations of totals, maximum, minimum, average out of the while-loop. The loop serves a different purpose: it is for user interaction and reporting, not for processing results.
possible chance of multiple avg calculation for same round again. Keep flag to track accounting of score, so we will not do the same more than once till the game is not played again.
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX_TESTS 5
#define MAXQUESTION 10000
bool myread(const char * format, void * address) {
char buffer[1024];
fgets(buffer, sizeof buffer, stdin);
return sscanf(buffer, format, address) == 1;
}
struct struc {
int a;
int b;
int c;
int add;
int grade;
};
int sj(int n) {
int t;
t = rand() % n;
return t;
}
void ctm_i(struct struc * t) {
{
t -> a = sj(101);
t -> c = sj(4);
if (t -> c == 1) {
t -> b = sj(101 - (t -> a));
t -> add = (t -> a) + (t -> b);
} else {
t -> b = sj((t -> a) + 1);
t -> add = (t -> a) - (t -> b);
}
t -> grade = 0;
}
}
void tcm_i(struct struc * t, int n) {
int ad;
printf(" ***********************************************************************"
"*********\n");
printf(" ......................................................................."
".........\n");
printf(" Question %d\n\n", n + 1);
printf(" You have 3 attempts for this question\n\n");
if (t -> c == 1)
printf(" %d+%d= ", t -> a, t -> b);
else
printf(" %d-%d= ", t -> a, t -> b);
myread(" %d", & ad);
if (ad == t -> add)
{
t -> grade = 10;
printf(" You earned 10 marks\n\n");
} else {
printf("\n Incorrect, you have 2 attempts remaining\n\n");
printf(" ");
myread(" %d", & ad);
if (ad == t -> add)
{
t -> grade = 7;
printf(" You earned 7 marks\n\n");
} else {
printf("\n Incorrect, you have 1 attempt remaining\n\n");
printf(" ");
myread(" %d", & ad);
if (ad == t -> add)
{
t -> grade = 5;
printf(" You earned 5 marks\n\n");
} else {
t -> grade = 0;
printf("\n Failure, 0 mark\n\n");
printf("\n The correct answer is %d\n\n", t -> add);
}
}
}
printf(" ......................................................................."
".........\n");
printf(" ***********************************************************************"
"*********\n");
}
void quiz(char name[]) {
int rounds = 0;
int highest = 0;
int lowest = INT_MAX;
float allScore = 0;
float avg = 0.0;
int i, j, g = 0;
struct struc test[MAX_TESTS];
srand((unsigned) time(NULL));
for (;;) {
rounds++;
for (i = 0; i < MAX_TESTS; i++) // generate all questions
{
ctm_i( & test[i]);
for (j = 0; j < i; j++)
if (test[i].a == test[j].a && test[i].b == test[j].b && test[i].c == test[j].c)
//if question is already present
ctm_i( & test[i]); //then re-generate
}
printf("\n Are you ready? Press Enter key to continue. ");
myread("", NULL);
for (i = 1; i <= 5; i++) {
printf(" *******************************************************************"
"**"
"***********\n");
printf(" ..................................................................."
".."
"...........\n");
}
// Take quiz
for (i = 0; i < MAX_TESTS; i++)
tcm_i( & test[i], i);
printf(" End\n\n");
bool done = false;
bool unsure = true;
bool showS = true;
bool acct = false;
while (unsure) {
unsure = false;
puts("\n");
if (showS) {
puts(" Enter 'S' to show results");
}
puts(" Enter 'P' to play another round");
puts(" Enter 'R' to return to main menu");
char choice;
printf(" ");
myread(" %c", & choice);
printf("\n");
if (choice == 'r' || choice == 'R') {
done = true;
} else {
///////////////////////// Changes /////////////
if (false == acct) {
g = 0;
// calculate total score for current round
for (i = 0; i < MAX_TESTS; i++) {
g += test[i].grade; //add score of each question
}
allScore += g; //add current round's score to total
avg = allScore / rounds; //average of all rounds
if (g > highest) {
highest = g;
}
if (g < lowest) {
lowest = g;
}
acct = true;
}
if (showS &&(choice == 'S' || choice == 's')) {
showS = false;
if (rounds == 1) {
printf(" Final score: %d/100\n", g); //display round score
printf(" ****** Player: %s ******\n", name);
} else {
printf(" Round %d score: %d/100\n", rounds, g); //display round score
printf(" Highest score: %d/100\n", highest);
printf(" Lowest score : %d/100\n", lowest);
printf(" Average score: %f/100\n", avg);
printf(" ****** Player: %s ******\n", name);
}
unsure = true;
} else if (choice == 'P' || choice == 'p') {
/// nothing to be done here
//we will display next test
} else {
puts(" Invalid input!");
unsure = true;
}
////////////////////////////////////
}
}
if (done)
break;
}
}
int main() {
char i1 = '1';
char name[25]; // ig;
printf("\n Welcome");
printf("\n");
while (i1 != 0) {
printf("\n");
//printf(" **********************Welcome %s! *********************\n", name);
printf(" ************************ Main Menu of Maths Quiz ***************************\n");
printf(" * 1.Enter Quiz *\n");
printf(" * 2.Quit *\n");
printf(" ****************************************************************************\n");
printf(" Please choose one from 1-2:\n");
printf(" ");
myread(" %c", & i1);
switch (i1) {
case '1':
printf("\n Enter Quiz:\n");
quiz(name); // calling quiz function defined in file "maincode.c"
break;
case '2':
printf(" Quit.\n\n");
}
}
return 0;
}
Output:
Enter 'S' to show results
Enter 'P' to play another round
Enter 'R' to return to main menu
s
Round 2 score: 47/100
Highest score: 50/100
Lowest score : 47/100
Average score: 48.500000/100
****** Player: ******
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <math.h>
int letters = 0;
int words = 0;
int sentences = 0;
int main(void)
{
string text = get_string("Text: ");
printf("\n");
for(int j = 0; j < strlen(text); j++)
{
if((text[j] >= 'a' && text[j] <= 'z') || (text[j] >= 'A' && text[j] <= 'Z'))
{
letters++;
}
if(text[j] == ' ')
{
words++;
}
if(text[j] == '.' || text[j] == '?' || text[j] == '!')
{
sentences++;
}
}
printf("Letters: %i\n", letters);
printf("Words: %i\n", words + 1);
printf("Sentences: %i\n", sentences);
float L = ((float)letters / (float)words) * 100;
float S = ((float)sentences / (float)words) * 100;
float result = round(0.0588 * L - 0.296 * S - 15.8);
printf("%f\n", L);
printf("%f\n", S);
printf("%f\n", result);
if(result < 1)
{
printf("Before Grade 1");
}
if(result == 1)
{
printf("Grade 1");
}
if(result == 2)
{
printf("Grade 2");
}
if(result == 3)
{
printf("Grade 3");
}
if(result == 4)
{
printf("Grade 4");
}
if(result == 5)
{
printf("Grade 5");
}
if(result == 6)
{
printf("Grade 6");
}
if(result == 7)
{
printf("Grade 7");
}
if(result == 8)
{
printf("Grade 8");
}
if(result == 9)
{
printf("Grade 9");
}
if(result == 10)
{
printf("Grade 10");
}
if(result == 11)
{
printf("Grade 11");
}
if(result == 12)
{
printf("Grade 12");
}
if(result == 13)
{
printf("Grade 13");
}
if(result == 14)
{
printf("Grade 14");
}
if(result == 15)
{
printf("Grade 15");
}
if(result >= 16)
{
printf("Grade 16+");
}
printf("\n");
}
Text:
Congratulations! Today is your day. You're off to Great Places! You're off and away!
Result:
Letters: 65
Words: 14
Sentences: 4
500.000000
30.769232
4.000000
Grade 4
I need to calculate the '0.0588 * L - 0.296 * S - 15.8' formula in my code, but for some reason, it is giving me the wrong answer for some text and right for some text. in this particular example, it should give me 'Grade 3' but it is giving me 'Grade 4' instead and if I put a text for grade 10 it will give me 'Grade 10'. And the calculations for 'L' and 'S' are wrong, in this example, they should be L = 464.29 and S = 28.57. I really don't know where I am going wrong, can someone please help me?
The problem is that you count words by counting spaces, so you don't count the last word. You could solve the problem by initialising words to 1.
Also I saw you noticed that problem since you wrote printf("Words: %i\n", words + 1); so why in your calculations didn't you added 1? You print 14 as word count, but then you use 13 to calculate the average length of the words.
Anyway you should absolutely change the final prints with that
if(result < 16) {
printf("Grade %d\n", result);
}
else {
puts("Grade 16+");
}
Or even just
result < 16 ? printf("Grade %d\n", result) : puts("Grade 16+");
It appears you have counted words inadequately. You counted the number of spaces between words, which gives a number that is one fewer than the number of words. This is adjusted for when printing the number, in printf("Words: %i\n", words + 1);, and when calculating S, in printf("Words: %i\n", words + 1);, but not when calculating L, in float L = ((float)letters / (float)words) * 100;.
Changing that last line to float L = ((float)letters / (float)(words+1)) * 100; results in the answer you expect, “Grade 3”.
However, a better solution is likely to initialize words with int words = 1; instead of int words = 0; and change the printf and the calculation of S to use words instead of words + 1.
(Further, a more sophisticated program would consider what happens if spaces appear before any words, after the last word, or multiple times between words.)
I'm trying to make a program, which puts a given amount of players in a given amount of teams. Afterwards they should be randomly chosen (eg. you roll the "dice" and Team 3's Player 42 and shall fight against Team 4's Player 22 (All the players are randomly put in the different teams, which are limited to the choice of the Gamemaster)).
In my code I have the basic output and structure. It says something like:
Team 1 now owns Player 43
Team 2 now owns Player 12
Team 4 now owns Player 1
Team 3 now owns Player 54
But my question is, how - based on the code - I could save this information and how can I (afterwards) let the players randomly fight? Members of the same team should NOT be able to fight each other and after each fight I want the players to be somehow on a "blacklist" where they can't be rolled anymore.
My code so far
#include <stdio.h>
#include <stdlib.h>
int main()
{
int mitglieder, teams, teameins = 0, teamzwei = 0, teamdrei = 0, teamvier = 0;
printf("Teamcreation\n");
printf("\nNumber of Players: ");
scanf("%d", &mitglieder);
printf("\nNumber of Teams: ");
scanf("%d", &teams);
printf("\nThere are ");
printf("%d", mitglieder);
printf(" Player in ");
printf("%d", teams);
printf(" Teams. \n");
int array[mitglieder];
for (int i = 0; i < mitglieder; i++)
{ // fill array
array[i] = i;
}
printf("The Player are in the following Teams: \n ");
for (int i = 0; i < mitglieder; i++)
{ // shuffle array
int temp = array[i];
int randomIndex = rand() % mitglieder;
array[i] = array[randomIndex];
array[randomIndex] = temp;
}
for (int i = 0; i < mitglieder; i++)
{ // print array
int random_number = rand() % teams + 1;
int tp = random_number;
if(tp == 1)
{
teameins+1;
}
else if(tp == 2)
{
teamzwei+1;
}
else if(tp == 3)
{
teamdrei+1;
}
else if(tp == 4)
{
teamvier+1;
}
printf("Team %d - Spieler: %d\n ",random_number,array[i] + 1);
}
if( (teamvier == 0) && (teamdrei == 0) )
{
printf("\n%d Mitglieder in Team 1 und %d Mitglieder in Team2",teameins,teamzwei);
}
else if((teamvier == 0) && (teamdrei < 0))
{
printf("\n%d Mitglieder in Team 1, %d Mitglieder in Team2 und %d Mitglieder in Team3.",teameins,teamzwei,teamdrei);
}
else if(teamvier < 0)
{
printf("\n%d Mitglieder in Team 1, %d Mitglieder in Team2, %d Mitglieder in Team 3 und %d Mitglieder in Team4.",teameins,teamzwei,teamdrei,teamvier);
}
return 0;
}
Before basing something on the given code, you should correct the errors in it, which are within the counting of team members and the printing of the counts. While you're at it, you could make it work also for more than four teams, e. g.:
#include <string.h>
…
int Spielerteam[mitglieder]; // team of the player
int Teamstaerke[teams]; // team member counts
memset(Teamstaerke, 0, sizeof Teamstaerke); // zero the counts
for (int i = 0; i < mitglieder; i++)
{ // print array
int random_number = rand() % teams;
int tp = random_number + 1;
Spielerteam[i] = tp; // save player's team information
Teamstaerke[random_number]++; // count the member
printf("Team %d - Spieler: %d\n ", tp, array[i] + 1);
}
for (int i = 0; i < teams; i++)
printf("%s%d Mitglieder in Team %d",
i ? i+1 == teams ? " und " : ", " : "\n", Teamstaerke[i], i+1);
puts("");
After that, the team each player array[i] + 1 belongs to is saved in Spielerteam[i].
Basically I am supposed to simulate a program that rolls three dice, add it up. Then I will have the user guess if the next roll is higher, lower, same or if they just want to quit. I am having two problems.
The numbers are not random, obviously this is a big mistake on my part but I can't seem to figure it out. I am thinking I do not need the second inclusion of 3 dice pairs? They don't help at all anyways.
Regardless, my program goes through all of the if/else if statements all at once. Obviously I do not want this to happen.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
int diceOne, diceTwo, diceThree, diceSum=0, timesCorrect=0, choice;
int newDiceOne, newDiceTwo, newDiceThree, newDiceSum;
srand( time(NULL) );
diceOne = rand() % 6 + 1;
diceTwo = rand() % 6 + 1;
diceThree = rand() % 6 + 1;
newDiceOne = rand() % 6 + 1;
newDiceTwo = rand() % 6 + 1;
newDiceThree = rand() % 6 + 1;
printf("The three dice rolls: %d, %d, %d", diceOne, diceTwo, diceThree);
diceSum = diceOne + diceTwo + diceThree;
printf("\nTotal sum of dice is %d\n", diceSum);
do {
printf("Guess higher(1), lower(2), same(3) or quit?(4)\n");
printf(" You have been correct %d times\n", timesCorrect);
scanf("%d", &choice);
printf("The three dice rolls: %d, %d, %d", newDiceOne, newDiceTwo, newDiceThree);
newDiceSum= newDiceOne + newDiceTwo + newDiceThree;
printf("\nTotal sum of dice is %d\n", newDiceSum);
if (choice == 1)
{
if (newDiceSum > diceSum);
timesCorrect++;
printf("You are correct!\n");
}
else if (newDiceSum < diceSum);
{
printf("You are incorrect, sorry!\n");
}
if (choice == 2)
{
if (newDiceSum < diceSum);
timesCorrect++;
printf("You are correct!\n");
}
else if (newDiceSum > diceSum);
{
printf("You are incorrect, sorry!\n");
}
if (choice == 3)
{
if (newDiceSum == diceSum);
timesCorrect ++;
printf("You are correct!\n");
}
else if (newDiceSum != diceSum);
{
printf("You are incorrect, sorry!\n");
}
if (choice == 4)
{
printf("Thanks for playing!!!!!!\n");
system("pause");
return 0;
}
} while (choice!= 4 );
}
You have an extra semicolon after the else if conditionals, like here
else if (newDiceSum < diceSum);
/* ^ this should not be here */
If you use a compiler with good diagnostic capabilities and enable warnings, it should warn you about that "typo", if you want to leave the block empty use braces like
else if (newDiceSum < diceSum) {}
Also, you set the first dice with rand() and they are random values, but then you always use the same values in the loop.
The following code:
Handles error conditions
Eliminates unneeded variables
Cleanly compiles
Executes with out failing
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ( void )
{
int diceOne;
int diceTwo;
int diceThree;
int diceSum=0;
int timesCorrect=0;
int choice;
int newDiceSum;
srand( time(NULL) );
diceOne = rand() % 6 + 1;
diceTwo = rand() % 6 + 1;
diceThree = rand() % 6 + 1;
printf("The three dice rolls: %d, %d, %d", diceOne, diceTwo, diceThree);
diceSum = diceOne + diceTwo + diceThree;
printf("\nTotal sum of dice is %d\n", diceSum);
do {
printf("Guess higher(1), lower(2), same(3) or quit?(4)\n");
printf(" You have been correct %d times\n", timesCorrect);
if( 1 != scanf("%d", &choice) )
{ // then scanf failed
perror( "scanf for choice failed" );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
diceOne = rand() % 6 + 1;
diceTwo = rand() % 6 + 1;
diceThree = rand() % 6 + 1;
printf("The three dice rolls: %d, %d, %d", diceOne, diceTwo, diceThree);
newDiceSum = diceOne + diceTwo + diceThree;
printf("\nTotal sum of dice is %d\n", newDiceSum);
switch( choice )
{
case 1:
if (newDiceSum > diceSum)
{
timesCorrect++;
printf("You are correct!\n");
}
else
{
printf("You are incorrect, sorry!\n");
}
break;
case 2:
if (newDiceSum < diceSum)
{
timesCorrect++;
printf("You are correct!\n");
}
else
{
printf("You are incorrect, sorry!\n");
}
break;
case 3:
if (newDiceSum == diceSum)
{
timesCorrect ++;
printf("You are correct!\n");
}
else
{
printf("You are incorrect, sorry!\n");
}
break;
case 4:
printf("Thanks for playing!!!!!!\n");
system("pause");
break;
default:
printf( " invalid choice, valid choices are 1...4\n");
break;
} // end switch
} while (choice!= 4 );
return(0);
} // end function: main