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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  /********************************************* 
* 
* SumStop.c 
* Sums input values until 1000 
* Written by Andrew Lui 
* On 7/11/2007 
* 
*********************************************/ 
#include <stdio.h> 

  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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  /********************************************* 
* 
* SumStop.c 
* Sums input values until 1000 
* Written by Andrew Lui 
* On 7/11/2007 
* 
*********************************************/ 
#include <stdio.h>
#include <iostream>
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.