Simple problem with reading a variable

I need some simple help. I have created this program code that I will show. Here is the problem. The first time through, in the middle of the program it asks for a new number. When you enter the number, it stays that number through the loops. How do I get it to pause and ask the question (iRoll) and read in the new answer each time through the loop? Also, I set it to shut the loop off if you type in "n" at the end; however, it runs the program one more time before shutting down.

I need some help with this as it is probably a simple error, but I'm just a beginner.


*********************************************************************************************
* Name: Date Assigned: 10-16-08
* Course: CSE 1233 Date Due: 10-23-08
*
* File Name: psk29lab5.h
*
* Program Objective: To develop a game using random numbers that represent 1-6 on die and to
* have different values for different combinations.
*********************************************************************************************/

#include<stdio.h>
#include<stdlib.h>


int main()

{
char cYesNo = '\0';
char cRoll = '\0';
int iRoll = 0;
int iRandom1 = 0;
int iRandom2 = 0;
int iRandom3 = 0;
int iRandom4 = 0;
int iRandom5 = 0;
int iRandom6 = 0;
int iScore = 0;
int iScore2 = 0;
int iTotal1 = 0;
int iTotal2 = 0;
srand(time());

system("clear");
printf("Welcome to a game of Quispe. You and the computer will each roll two dice\n");
printf("to determine the winner. There is an option for a re-roll of one die if\n");
printf("the user so desires. A one and a two on the die will result in a Quispe.");
printf("\n1000 points will be awarded for a Quispe. If doubles are rolled, the points\n");
printf("will be added by multiplying the number on the die by 110. If you have two\n");
printf("different numbers, the first die will be multipled by ten and then the second\n");
printf("die number will be added to it. The highest point total wins the game.");

do
{

iRandom1 = rand() % 6 + 1;
iRandom2 = rand() % 6 + 1;
iRandom3 = rand() % 6 + 1;
iRandom4 = rand() % 6 + 1;
iRandom5 = rand() % 6 + 1;
iRandom6 = rand() % 6 + 1;

printf("\n\nComputer Die 1: %d", iRandom1);
printf("\nComputer Die 2: %d", iRandom2);

if(iRandom1 + iRandom2 == 3)
{
iScore = 1000;
printf("\nComputer score is %d\n\n", iScore);
}
else if(iRandom1 == iRandom2)
{
iScore = iRandom1 * 110;
printf("\nComputer score is %d\n\n", iScore);
}
else
{
iScore = iRandom1 * 10 + iRandom2;
printf("\nComputer score is %d\n\n", iScore);
}

printf("\nUser Die 1: %d", iRandom3);
printf("\nUser Die 2: %d", iRandom4);

if(iRandom3 + iRandom4 == 3)
{
iScore2 = 1000;
printf("\nUser score is %d\n\n", iScore2);
}
else if(iRandom3 == iRandom4)
{
iScore2 = iRandom3 * 110;
printf("\nUser score is %d\n\n", iScore2);
}
else
{
iScore2 = iRandom3 * 10 + iRandom4;
printf("\nUser score is %d\n\n", iScore2);
}

printf("Type 1 to reroll die 1, 2 to reroll die 2, or 3 to stay as it is: ");
scanf("%d/", &iRoll);

if(iRoll == 1)
{
printf("\nUser Die 1: %d", iRandom5);
printf("\nUser Die 2: %d", iRandom4);

if(iRandom5 + iRandom4 == 3)
{
iScore2 = 1000;
printf("\nUser score is %d\n\n", iScore2);
}
else if(iRandom5 == iRandom4)
{
iScore2 = iRandom5 * 110;
printf("\nUser score is %d\n\n", iScore2);
}
else
{
iScore2 = iRandom5 * 10 + iRandom4;
printf("\nUser score is %d\n\n", iScore2);
}
}
else if(iRoll == 2)
{
printf("\nUser Die 1: %d", iRandom3);
printf("\nUser Die 2: %d", iRandom6);

if(iRandom3 + iRandom6 == 3)
{
iScore2 = 1000;
printf("\nUser score is %d\n\n", iScore2);
}
else if(iRandom3 == iRandom6)
{
iScore2 = iRandom3 * 110;
printf("\nUser score is %d\n\n", iScore2);
}
else
{
iScore2 = iRandom3 * 10 + iRandom6;
printf("\nUser score is %d\n\n", iScore2);
}
}
else
{
printf("\nUser Die 1: %d", iRandom3);
printf("\nUser Die 2: %d", iRandom4);

if(iRandom3 + iRandom4 == 3)
{
iScore2 = 1000;
printf("\nUser score is %d\n\n", iScore2);
}
else if(iRandom3 == iRandom4)
{
iScore2 = iRandom3 * 110;
printf("\nUser score is %d\n\n", iScore2);
}
else
{
iScore2 = iRandom3 * 10 + iRandom4;
printf("\nUser score is %d\n\n", iScore2);
}
}
if(iScore > iScore2)
{
printf("\nThe computer won this round.\n");
}
else
{
printf("\nYou have won this round. Congratulations!\n");
}
iTotal1 += iScore;
iTotal2 += iScore2;

printf("\nThe computer overall score is %d", iTotal1);
printf("\nYour overall score is %d", iTotal2);

if(iTotal1 > iTotal2)
{
printf("\n\nThe computer is winning.\n");
}
else if(iTotal1 < iTotal2)
{
printf("\n\nYou are winning. Keep it up!\n");
}
else
{
printf("\n\nA tie? Are you kidding?\n");
}

printf("\n\nDo you want to continue? (y or n): ");
scanf("%c ", &cYesNo);

}while
(cYesNo != 'n');
return 0;
}

Please use [code][/code] tags and indentation next time :)

Anyway, it seems somewhat strange that it would do that...you could try turning to loop into just a normal while loop, since cYesNo is != 'n' at the beginning of the program.

And to solve the other problem, you either need to ask each time you want them to be able to change the roll, or you could try just making a function that generates the 2 random numbers and does everything you need to.
Hi

Don't use scanf() for user input, use instead fgets() and convert to whatever numerical value you need (atoi).





Topic archived. No new replies allowed.