### the bug of my programmar

SumStopGame is a simple game played between two players. Each player will enter an integer (between 1 and 200) in turns, and the player loses if the sum of all integers (of both players) has reached or exceeded 1000. Let's write the game step-by-step.

1. SumStop is a simple program that reads integers and sums them up until the sum reaches 1000. There is no input validation. Add a WHILE structure around the input statement to check the validity of input (between 1 and 200).

2. Before the input statement, add an output statement to print either "Anders' turn" or "Betsy's turn" (the names of
the two players). A variable turn is added to the program to control the turn. It is 0 for "Anders" and 1 for "Betsy".

However,when i entered 300 in the Betsyrs' turn,i can not enter any number afterward.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` `````` /********************************************* * * SumStop.c * Sums input values until 1000 * Written by Andrew Lui * On 7/11/2007 * *********************************************/ #include int main() { int sum = 0; /* store the sum of integers */ int number = 0; /* initialize number to zero for entry into while loop */ int countNumber = 0; int t=1; printf("Enter floating point values: "); while (sum < 1000) { while(1){ if(t==1){ printf("Anders' turn\n"); break; } else if(t==0){ printf("Betsyrs' turn\n"); t=2; break; }} scanf("%d", &number); if(number>=1&&number<=200){ sum += number; printf("The sum is %d\n", sum); countNumber++; t--; printf("Enter floating point values\n"); } else{ printf("please enter 1-200 values"); } } if(sum>=1000) printf("Number of entered values = %d\n", countNumber); }``````
Last edited on
your program has a lot of problems I think mostly due to line 20.

You should be able to debug your own code.
Learning how is not that hard, just put in some comments and see if what happens is what you expect to happen like this:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071`` `````` /********************************************* * * SumStop.c * Sums input values until 1000 * Written by Andrew Lui * On 7/11/2007 * *********************************************/ #include #include using namespace std; int main() { int sum = 0; /* store the sum of integers */ int number = 0; /* initialize number to zero for entry into while loop */ int countNumber = 0; int t=1; cout << "Test Point 0" << endl; printf("Enter floating point values: "); cout << "Test Point 1" << endl; while (sum < 1000) { cout << "Test Point 2" << endl; while(1) { cout << "Test Point 3" << endl; if(t==1) { cout << "Test Point 4" << endl; printf("Anders' turn\n"); break; } else if(t==0) { cout << "Test Point 5" << endl; printf("Betsyrs' turn\n"); t=2; break; } } cout << "Test Point 6" << endl; scanf("%d", &number); cout << "Test Point 7" << endl; if(number>=1&&number<=200) { cout << "Test Point 8" << endl; sum += number; printf("The sum is %d\n", sum); countNumber++; t--; printf("Enter floating point values\n"); } else { cout << "Test Point 9" << endl; printf("please enter 1-200 values"); } } if(sum>=1000) { cout << "Test Point 10" << endl; printf("Number of entered values = %d\n", countNumber);} }``````
Topic archived. No new replies allowed.