haha ( excuse me for chuckling) now you know why the "infinite loop" was not happening?
1. as post #19 the_jackass pointed out as well your checks for 50 or greater are in the wrong place, or would be better served being somewhere else within your loop,
you're relying on all of your conditions to get you out of your while loop no matter what. Making your if ( quit == 'q' ) obsolete
You are using an algorithm that has the ability to reset the a value to zero.
So it is not necessarily going into an infinite loop. it might just be that 50 or greater has never been met yet.
It was main return 0 that was giving you false readings. it'd loop once then hit the return 0 then do just that return 0 then exit the function.
if you look at post #29 I give you an idea that can be used to set your other condition for exiting the while loop and preventing it from going any further in rolling the die when it is no longer needed. If you go back and read #19 you will hopefully see why you're needing to check the total score for each player each where needed before rolling , or allowing another roll of the dice to take place.
nulling char
works , as even I have had to change them each time along with your usage of printf_s and scanf_sCode:char mychar = '\0';
because I have already complete this on my side, and I know because of that, it takes awhile for one of the players to reach 50, I am going to give you that little piece of the missing puzzle. to stop that loop and make it wait while inside of it.
getchar() waits for input just hit enter to get it to go past it.Code:printf("\nplayer 1 hit enter to roll\n"); getchar();
returns the char that is pressed. that should get you to be able to look at your code results to help you see if the rest of it is ok, by preventing it from looking like it is in an infinite loop, when really it just has not gotten a total score of 50 yet so it just spins around running your code, and resetting it to zero again, and again, and again, as it is coded to do when it gets double 3's.Code:quit = getchar()
and check out post # 15 as well
Last edited by userxbw; 10-25-2017 at 08:21 PM.
I see that I had return 0 in the wrong place so thank you for that. I've moved it and made a few other changes like changing the condition for the while loop and adding a check for score total right after each player's roll. However, I still do not understand how to end the loop when a player reaches 50 or more.
Here's what it looks like now:
Code:int main() { //declare variables int My_dice1; int My_dice2; int Your_dice1; int Your_dice2; int My_result = 0; int Your_result = 0; int total; char quit = NULL; char start; char mychar = '\0'; srand((unsigned)time(NULL)); //Seed random function with current time //Introductory message explaining what the program does and how the game is played. printf_s("This program will simulate the dice game Fifty which requires 2 players with each player taking turns to roll 2 die.\n" "The goal of Fifty is to be the first player to reach 50 points. You get points by rolling doubles.\n" "All doubles except 3s and 6s score 5 points. Double 6s are worth 25 points and double 3s wipe out the players entire score\n" "and the player must start again at 0. Non-double rolls are 0 points.\n" "Press q to quit.\n"); //Start game rules printf("Player 1's turn. Press 's' to start.\n"); scanf_s("%c", &start); if (start == 's') //check if player actually pressed 's', only then execute the loop { while (quit != 'q') { printf("\nPlayer 1's turn\n"); My_dice1 = rand() % 6 + 1; printf("Player 1's first die is %d\n", My_dice1); My_dice2 = rand() % 6 + 1; printf("Player 2's second die is %d\n", My_dice2); total = (My_dice1 + My_dice2); //Check if players has doubles if (My_dice1 == My_dice2) { //Check for double sixes if (total == 12) { My_result = My_result + 25; //Add 25 points to the sum }//Check for double sixes end brace //Check for doule threes else if (total == 6) { My_result = 0; printf("You rolled double 3s and your score has been reset to 0.\n"); //Assign zero to the sum }//else if end brace else { My_result = My_result + 5; //Executes for remaining other doubles //Add 5 points to the sum }//else end brace }//end brace for the doubles check if statement for player1 printf("Player 1's score is %d \n", My_result); if (My_result < 50) { printf("\nPlayer 2's turn\n"); Your_dice1 = rand() % 6 + 1; printf("Player 2's first die is %d\n", Your_dice1); Your_dice2 = rand() % 6 + 1; printf("Player 2's second die is %d\n", Your_dice2); total = (Your_dice1 + Your_dice2); }//end brace for if less than 50 check else { printf("Contratulations, Player 1 is the winner!\n"); } //Check if players has doubles if (Your_dice1 == Your_dice2) { //Check for double sixes if (total == 12) { Your_result = Your_result + 25; //Add 25 points to the sum }//Check for double sixes end brace //Check for doule threes else if (total == 6) { Your_result = 0; printf("You rolled double 3s and your score has been reset to 0.\n"); //Assign zero to the sum }//else if end brace else { Your_result = Your_result + 5; //Executes for remaining other doubles //Add 5 points to the sum }//else end brace }//end brace for the doubles check if statement for player2 printf("Player 2's score is %d \n", Your_result); if (Your_result > 50) { printf("Contratulations, Player 2 is the winner!\n"); } }//while loop end brace }//if start end brace return 0; }//main end brace
Code:#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> int main() { //declare variables int My_dice1; int My_dice2; int Your_dice1; int Your_dice2; int My_result = 0; int Your_result = 0; int total; char quit = '\0'; // <-- equals NULL char start; // char mychar = '\0'; // this was an example how to NULL your char srand((unsigned)time(NULL)); //Seed random function with current time //Introductory message explaining what the program does and how the game is played. printf("This program will simulate the dice game Fifty which requires 2 players with each player taking turns to roll 2 die.\n" "The goal of Fifty is to be the first player to reach 50 points. You get points by rolling doubles.\n" "All doubles except 3s and 6s score 5 points. Double 6s are worth 25 points and double 3s wipe out the players entire score\n" "and the player must start again at 0. Non-double rolls are 0 points.\n" "Press q to quit.\n"); //Start game rules printf("Player 1's turn. Press 's' to start.\n"); scanf("%c", &start); if (start == 's') //check if player actually pressed 's', only then execute the loop { while (quit != 'q') { printf("\nPlayer 1's turn\n"); My_dice1 = rand() % 6 + 1; printf("Player 1's first die is %d\n", My_dice1); My_dice2 = rand() % 6 + 1; printf("Player 2's second die is %d\n", My_dice2); total = (My_dice1 + My_dice2); //Check if players has doubles if (My_dice1 == My_dice2) { //Check for double sixes //Add 25 points to the sum if (total == 12) { My_result = My_result + 25; }//Check for double sixes end brace //Check for doule threes else if (total == 6) { My_result = 0; //Assign zero to the sum printf("You rolled double 3s and your score has been reset to 0.\n"); }//else if end brace else //Executes for remaining other doubles //Add 5 points to the sum { My_result = My_result + 5; }//else end brace }//end brace for the doubles check if statement for player1 printf("Player 1's score is %d \n", My_result); ////////////////////////////////////////////////////////////// /************************************************************ * * put this in you code too so you can see what is does * * ***********************************************************/ printf("Hit Enter to roll dice\n"); getchar(); /********************************************************** * ********************************************************* * ***************************************************** * ******************************************************** * *********************************************************/ if (My_result < 50) { printf("\nPlayer 2's turn\n"); Your_dice1 = rand() % 6 + 1; printf("Player 2's first die is %d\n", Your_dice1); Your_dice2 = rand() % 6 + 1; printf("Player 2's second die is %d\n", Your_dice2); total = (Your_dice1 + Your_dice2); }//end brace for if less than 50 check else { printf("Congratulations, Player 1 is the winner!\n"); // Here game is over // you're missing the assignment to quit // so it will quit // what letter is quit looking for? // quit = ?? } //Check if players has doubles if (Your_dice1 == Your_dice2) { //Check for double sixes if (total == 12) { Your_result = Your_result + 25; //Add 25 points to the sum }//Check for double sixes end brace //Check for double threes else if (total == 6) { Your_result = 0; printf("You rolled double 3s and your score has been reset to 0.\n"); //Assign zero to the sum }//else if end brace else { Your_result = Your_result + 5; //Executes for remaining other doubles //Add 5 points to the sum }//else end brace }//end brace for the doubles check if statement for player2 printf("Player 2's score is %d \n", Your_result); if (Your_result > 50) { printf("Contratulations, Player 2 is the winner!\n"); // quit = ?? //<------------------------ } }//while loop end brace }//if start end brace // your data is held within the main function // so the program will see it values // so when loops finishes print out results here printf("look what I can print here\n"); if( My_result < Your_result) printf("Player 2 is the winner with %d points\n", Your_result); else printf("Player 1 is the winner with %d points\n", My_result); printf("totals are player 1 %d, plyer 2 %d\n", My_result, Your_result); // that is an example it can be changed // to suit the needs of the assignment. // or not used at all. return 0; }//main end brace
Last edited by userxbw; 10-26-2017 at 05:47 PM.
nothing
Last edited by userxbw; 10-26-2017 at 05:35 PM.
Met again with my teacher tonight and while I wasn't a whole lot wrong in terms of needing to rewrite tons of code I did have a fundamental error in the while condition. It should have been joined with AND instead of OR.
Here's my current code which plays the game for the 2 players (which is still an issue) but the loop is now functioning correctly.
Code:int main() { //declare variables int My_dice1; int My_dice2; int Your_dice1; int Your_dice2; int My_result = 0; int Your_result = 0; int total; char quit = NULL; char start; srand((unsigned)time(NULL)); //Seed random function with current time //Introductory message explaining what the program does and how the game is played. printf_s("This program will simulate the dice game Fifty which requires 2 players with each player taking turns to roll 2 die.\n" "The goal of Fifty is to be the first player to reach 50 points. You get points by rolling doubles.\n" "All doubles except 3s and 6s score 5 points. Double 6s are worth 25 points and double 3s wipe out the players entire score\n" "and the player must start again at 0. Non-double rolls are 0 points.\n" "Press q to quit.\n"); //Start game rules printf("Player 1's turn. Press 's' to start.\n"); scanf_s("%c", &start); if (start == 's') //check if player actually pressed 's', only then execute the loop { while (My_result < 50 && Your_result < 50) { printf("\nPlayer 1's turn\n"); My_dice1 = rand() % 6 + 1; printf("Player 1's first die is %d\n", My_dice1); My_dice2 = rand() % 6 + 1; printf("Player 1's second die is %d\n", My_dice2); total = (My_dice1 + My_dice2); //Check if players has doubles if (My_dice1 == My_dice2) { //Check for double sixes if (total == 12) { My_result = My_result + 25; //Add 25 points to the sum }//Check for double sixes end brace //Check for doule threes else if (total == 6) { My_result = 0; printf("You rolled double 3s and your score has been reset to 0.\n"); //Assign zero to the sum }//else if end brace else { My_result = My_result + 5; //Executes for remaining other doubles //Add 5 points to the sum }//else end brace }//end brace for the doubles check if statement for player1 printf("Player 1's score is %d \n", My_result); if (My_result < 50) { printf("\nPlayer 2's turn\n"); Your_dice1 = rand() % 6 + 1; printf("Player 2's first die is %d\n", Your_dice1); Your_dice2 = rand() % 6 + 1; printf("Player 2's second die is %d\n", Your_dice2); total = (Your_dice1 + Your_dice2); //Check if players has doubles if (Your_dice1 == Your_dice2) { //Check for double sixes if (total == 12) { Your_result = Your_result + 25; //Add 25 points to the sum }//Check for double sixes end brace //Check for doule threes else if (total == 6) { Your_result = 0; printf("You rolled double 3s and your score has been reset to 0.\n"); //Assign zero to the sum }//else if end brace else { Your_result = Your_result + 5; //Executes for remaining other doubles //Add 5 points to the sum }//else end brace }//end brace for the doubles check if statement for player2 printf("Player 2's score is %d \n", Your_result); }//end brace for if less than 50 check else { printf("Contratulations, Player 1 is the winner!\n"); } if (Your_result >= 50) { printf("Contratulations, Player 2 is the winner!\n"); } }//while loop end brace }//if start end brace return 0; }//main end brace
it would have still worked:
resultsCode:#include <stdio.h> int main (void) { int a = 0; char quit; while (quit != 'q') { printf("%d: quit != 'q'\n", a); if (a == 5) quit = 'q'; a++; } printf("quit = %c\n", quit); return 0; }
and in your codeCode:userx@slackwhere:~/bin userx@slackwhere:~/bin $ ./while_not_q 0: quit != 'q' 1: quit != 'q' 2: quit != 'q' 3: quit != 'q' 4: quit != 'q' 5: quit != 'q' quit = q
Code:// char quit = '\0'; <-- no longer being used. So get rid of it right?Code:}//end brace for if less than 50 check else { printf("Contratulations, Player 1 is the winner!\n"); quit = 'q'; } if (Your_result >= 50) { printf("Contratulations, Player 2 is the winner!\n"); quit = 'q'; }
is it suppose to stop between users/players?
Last edited by userxbw; 10-26-2017 at 08:53 PM.
So I'm really close. Here's my updated code. I've got the back and forth in there but it's not working quite right. The player 1 button-press doesn't work but the player 2 button-press is working perfectly. I built them the same way so I'm struggling to see why one works but the other doesn't.
Code:int main() { //declare variables int My_dice1; int My_dice2; int Your_dice1; int Your_dice2; int My_result = 0; int Your_result = 0; int total; char quit = NULL; char start; char myKey = NULL; char yourKey = NULL; srand((unsigned)time(NULL)); //Seed random function with current time //Introductory message explaining what the program does and how the game is played. printf_s("This program will simulate the dice game Fifty which requires 2 players with each player taking turns to roll 2 die.\n" "The goal of Fifty is to be the first player to reach 50 points. You get points by rolling doubles.\n" "All doubles except 3s and 6s score 5 points. Double 6s are worth 25 points and double 3s wipe out the players entire score\n" "and the player must start again at 0. Non-double rolls are 0 points.\n" "Press q to quit.\n"); //Start game rules printf("Player 1's turn. Press 's' to start.\n"); scanf_s("%c", &start); if (start == 's') //check if player actually pressed 's', only then execute the loop { while ((My_result < 50 && Your_result < 50) || (quit != 'q')) { printf("Player 1, press 'a' to roll your dice or press 'q' to quit.\n"); scanf_s("%c", &myKey); if (myKey == 'a') { printf("\nPlayer 1's turn\n"); My_dice1 = rand() % 6 + 1; printf("Player 1's first die is %d\n", My_dice1); My_dice2 = rand() % 6 + 1; printf("Player 1's second die is %d\n", My_dice2); total = (My_dice1 + My_dice2); //Check if players has doubles if (My_dice1 == My_dice2) { //Check for double sixes if (total == 12) { My_result = My_result + 25; //Add 25 points to the sum }//Check for double sixes end brace //Check for doule threes else if (total == 6) { My_result = 0; printf("You rolled double 3s and your score has been reset to 0.\n"); //Assign zero to the sum }//else if end brace else { My_result = My_result + 5; //Executes for remaining other doubles //Add 5 points to the sum }//else end brace }//end brace for the doubles check if statement for player1 printf("Player 1's score is %d \n", My_result); }//if myKey end brace if (My_result < 50) { printf("Player 2, press 'b' to roll your dice or press 'q' to quit.\n"); scanf_s("%c", &yourKey); if (yourKey == 'b') { printf("\nPlayer 2's turn\n"); Your_dice1 = rand() % 6 + 1; printf("Player 2's first die is %d\n", Your_dice1); Your_dice2 = rand() % 6 + 1; printf("Player 2's second die is %d\n", Your_dice2); total = (Your_dice1 + Your_dice2); //Check if players has doubles if (Your_dice1 == Your_dice2) { //Check for double sixes if (total == 12) { Your_result = Your_result + 25; //Add 25 points to the sum }//Check for double sixes end brace //Check for doule threes else if (total == 6) { Your_result = 0; printf("You rolled double 3s and your score has been reset to 0.\n"); //Assign zero to the sum }//else if end brace else { Your_result = Your_result + 5; //Executes for remaining other doubles //Add 5 points to the sum }//else end brace }//end brace for the doubles check if statement for player2 }//if yourKey end brace printf("Player 2's score is %d \n", Your_result); }//end brace for if less than 50 check else { printf("Contratulations, Player 1 is the winner!\n"); } if (Your_result >= 50) { printf("Contratulations, Player 2 is the winner!\n"); } }//while loop end brace }//if start end brace return 0; }//main end brace
all them changes are unneeded, you just needed to add your scanf("%c", start); because that char can be used again. and remove your char quit.
then see where you're at after you run it.
I was just showing you how quit could have been used had it been used.
you just needed to make it == equal q by assigning the value of q to it.
if you still HAVE to make it have the option of q for quit then the loop condition only needs to beCode:#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> int main() { //declare variables int My_dice1; int My_dice2; int Your_dice1; int Your_dice2; int My_result = 0; int Your_result = 0; int total; char start; srand((unsigned)time(NULL)); //Seed random function with current time //Introductory message explaining what the program does and how the game is played. printf("This program will simulate the dice game Fifty which requires 2 players with each player taking turns to roll 2 die.\n" "The goal of Fifty is to be the first player to reach 50 points. You get points by rolling doubles.\n" "All doubles except 3s and 6s score 5 points. Double 6s are worth 25 points and double 3s wipe out the players entire score\n" "and the player must start again at 0. Non-double rolls are 0 points.\n" "Press q to quit.\n"); //Start game rules printf("Player 1's turn. Press 's' to start.\n"); scanf("%c", &quit); // use quit if you're going to quit if (start == 's') //check if player actually pressed 's', only then execute the loop { while (My_result < 50 && Your_result < 50) { // player one is already going to roll // he started the game // it blast past here on the first // time around. but besides that it works. printf("\nPlayer One: Hit enter to roll\n"); scanf("%c", &start); printf("\nPlayer 1's turn\n"); My_dice1 = rand() % 6 + 1; printf("Player 1's first die is %d\n", My_dice1); My_dice2 = rand() % 6 + 1; printf("Player 1's second die is %d\n", My_dice2); total = (My_dice1 + My_dice2); //Check if players has doubles if (My_dice1 == My_dice2) { //Check for double sixes if (total == 12) { My_result = My_result + 25; //Add 25 points to the sum }//Check for double sixes end brace //Check for doule threes else if (total == 6) { My_result = 0; printf("You rolled double 3s and your score has been reset to 0.\n"); //Assign zero to the sum }//else if end brace else { My_result = My_result + 5; //Executes for remaining other doubles //Add 5 points to the sum }//else end brace }//end brace for the doubles check if statement for player1 printf("\nPlayer Two: Hit enter to roll\n"); scanf("%c", &start); printf("Player 1's score is %d \n", My_result); if (My_result < 50) { printf("\nPlayer 2's turn\n"); Your_dice1 = rand() % 6 + 1; printf("Player 2's first die is %d\n", Your_dice1); Your_dice2 = rand() % 6 + 1; printf("Player 2's second die is %d\n", Your_dice2); total = (Your_dice1 + Your_dice2); //Check if players has doubles if (Your_dice1 == Your_dice2) { //Check for double sixes if (total == 12) { Your_result = Your_result + 25; //Add 25 points to the sum }//Check for double sixes end brace //Check for doule threes else if (total == 6) { Your_result = 0; printf("You rolled double 3s and your score has been reset to 0.\n"); //Assign zero to the sum }//else if end brace else { Your_result = Your_result + 5; //Executes for remaining other doubles //Add 5 points to the sum }//else end brace }//end brace for the doubles check if statement for player2 printf("Player 2's score is %d \n", Your_result); }//end brace for if less than 50 check else { printf("Contratulations, Player 1 is the winner!\n"); } if (Your_result >= 50) { printf("Contratulations, Player 2 is the winner!\n"); } }//while loop end brace }//if start end brace return 0; }//main end brace
then put them quit = 'q' where I showed you on top of that one changed that asked for a q the you used the scanf to get one. leave the rest alone. and that should work that way as well.Code:while ( quit != q) { printf("hit enter to roll or q to quit\n); scanf("%c", start); // or quit as long as all of them match the char being used. ...... // then down here can this can be added BUT ONLY IF YOU NEED THAT OPTION. // the assigning the q to quit takes care of stopping the loop also when one gets 50 or greater. else { printf("Contratulations, Player 1 is the winner!\n"); quit = 'q'; } if (Your_result >= 50) { printf("Contratulations, Player 2 is the winner!\n"); quit = 'q'; }
Last edited by userxbw; 10-26-2017 at 09:43 PM.
Reusing varitables
ResultsCode:#include <stdio.h> int main (void) { int a = 0; char start; printf("enter s to start\n"); scanf("%c", &start); if ( start == 's') while (start != 'q') { printf("%d: start != 'q'\n", a); if (a == 5) start = 'q'; a++; } else printf("what no want to start?\n"); printf("start = %c\n", start); return 0; }
just hitting enter gets this resultCode:userx@slackwhere:~/bin $ ./while_not_q enter s to start s 0: start != 'q' 1: start != 'q' 2: start != 'q' 3: start != 'q' 4: start != 'q' 5: start != 'q' start = q userx@slackwhere:~/bin
Code:userx@slackwhere:~/bin $ ./while_not_q enter s to start what no want to start? start =
Last edited by userxbw; 10-26-2017 at 09:40 PM.
If you had used parentheses properly the code would be much easier to understand:
As it stands, your poor indentation illustrates why a style of always using parentheses is better. That said, you could have written the empty for loop bodies without parentheses and still have them be readable with suitable indentation, e.g.,Code:for (; left > 0; left -= newW) { /* do nothing here */ } for (; top > 0; top -= newH) { /* do nothing here */ } for (x = left; x < img.screenW; x += newW) { for (y = top; y < img.screenH; y += newH) { imlib_blend_image_onto_image (imgdata.image, 0, 0, 0, h,w, x, y, newW / space_tile_gap((rand() % 3)) , newH / space_tile_gap((rand() % 3))); } }
Although it is possible to argue that the semi-colon at the end might be obscured by the comment, so maybe omitting the comment would be better.Code:for (; left > 0; left -= newW) /* do nothing here */; for (; top > 0; top -= newH) /* do nothing here */;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
the last time I tried putting brackets around it (the brackets assignment may have been different,) but, it just screwed it up so I put it back the way it was then it worked again. so I left it alone then got on with the rest of what I had to do. but thanks. I'll try that again and let you know in a minute or so...
back.. ok smarty ...... it worked... thanks
I didn't put that much thought into it.Code:for (; left > 0; left -= newW); {} for (; top > 0; top -= newH); {} for (x = left; x < img.screenW; x += newW) { for (y = top; y < img.screenH; y += newH) imlib_blend_image_onto_image (imgdata.image, 0, 0, 0, h,w, x, y, newW, newH); }
Last edited by userxbw; 10-26-2017 at 10:02 PM.