system("pause") in Dev-C++

Pages: 12
Hello Everyone. This is my first big program. I don't know if the issue is the system("pause") statement or what.The exercise:
Write a program that reads a file consisting of students’ test scores in the
range 0–200. It should then determine the number of students having
scores in each of the following ranges: 0–24, 25–49, 50–74, 75–99,
100–124, 125–149, 150–174, and 175–200. Output the score ranges
and the number of students. (Run your program with the following input
data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200,
175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189.)

My code:
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;

const int maxCategory=7;
int getCategory(int);
void initialize(int student[]);
void getStudent(int category, int student[]);
void printArray(int category, int student[]);

int main()
{
int category;
int score;
int student[maxCategory];
ifstream infile;
infile.open("c:\\stuScore.txt");
/*    infile.open("stuScore.txt");    */

if (!infile)
{
cout<<"Cannot open input file. "<< endl;
system("pause");
return 1;
}
initialize(student);
infile>>score;

while (!infile.eof())
{ 
category = getCategory(score);
getStudent(category, student);

infile>>score;
} 

infile.close();
printArray(category, student);


return 0;
}

void initialize(int student[])
{
int j;
for (j=0;j<=maxCategory;j++)
{
student[j]=0;
}
}

void getStudent(int c, int s[])
{
if (c<=maxCategory)
{
s++;      
	}
}

void printArray(int category, int student[])
{
	string scoreRange;
	cout<<"Score Range"<<"          ";
	cout<<"Number of Student"<<endl;

	for (category=0; category<=maxCategory;category++)
	  {
		  switch (category)
		  {
		  case 0:
			  scoreRange= "0-24";
			  break;
                            case 1:
			  scoreRange= "25-49";
			  break;
	               case 2:
			  scoreRange= "50-74";
			  break;
		  case 3:
			  scoreRange= "75-99";
	                            break;	                                                                                                                       case 4:
			  scoreRange= "100-124";
			  break;
		  case 5:
			  scoreRange= "125-149";
			  break;
		  case 6:
			  scoreRange= "150-174";
			  break;
		  case 7:
			  scoreRange= "175-200";
			  break;
		  }

	     cout<< scoreRange<<"                 ";
	      cout <<student[category]<<endl;	  
      }
}

int getCategory(int score)
{
    int scoreRange;

    if (score >=0 && score <25)
	scoreRange=0;
	
   else if (score >=25 && score <50)
	scoreRange=1;
	
   else if (score >=50 && score <75)
	scoreRange=2;

   else if (score >=75 && score < 100)
	scoreRange=3;

   else if (score >=100 && score <125)
	scoreRange=4;

   else if (score >=125 && score <150)
	scoreRange=5;

   else if (score >=150 && score <175)
	scoreRange=6;

   else if (score >=175 && score <=200)
	scoreRange=7;

   else
     cout<<"Invalid score";
     return scoreRange;
}

If I put the system("pause"); return 0; statements in my int main block, the program goes into a loop. I tried putting it in the printArray(category, student); block, then it won't pause.
I've used it in my other C++ programs, just fine. But this is the first time I've used it in a program this big. I do not know what to do. Please help.
Isolde
the system("pause"); we use it to enable us to stop output window of our code when compile.
not in function
ur porgram work fine without system("pause");.

just add it before return 0; only ....

I tried putting it in the printArray(category, student); block, then it won't pause.

why u want o pause function. if u what see what happen across function work use Watch and run step by setp porgram for ur code complier or IDE.

it can work fine if u use it in dos or *.cmd files ...
Many thanks L.B. But Dev-C++ is what the instructor gave to students who did not have a C++ compiler.

Auda, If I don't use system("pause"); the dev-c++ application does not stop long enough for me to see the program output. Many thanks for the Watch suggestion. I will keep trying.

I am greatly thankful to you both.
You can get a C++ compiler a lot easier than most people think. Check out www.codeblocks.org for a good cross-platform IDE that can come pre-packaged with a compiler, depending on which version you download. For Windoze, look in to the package that includes MinGW. That's the one that has a compiler in it.
Thank you ciphermagi. The program is now in a loop. Can anyone see the error of my ways? :-(
It appears that you're doing a logical comparison on an un-initialized variable "category".
OK. I seemed to have gotten rid of the loop. But still having problems withthe system pause. (??)
Current code, please.
OK. I got the system pause to work, BUT now my number of students for each grade category is outputting 0. (??)
Thanks
Current code, please.
I wonder, am I the only who tests my program as I write it? Or does everyone just write everything and hope for the best?
closed account (zb0S216C)
ResidentBiscuit wrote:
am I the only who tests my program as I write it?

Nope. I write a function/method, then test it with a dummy console. I then weed out any potential performance bottlenecks, then test it again.

Wazzak
Last edited on
Here is the current code:
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;

const int maxCategory=7;
int getCategory(int);
void initialize(int student[]);
void getStudent(int category, int student[]);
void printArray(int category, int student[]);

int main()
{
int category;
int score;
int student[maxCategory];
ifstream infile;
infile.open("c:\\stuScore.txt");
/*    infile.open("stuScore.txt");    */

if (!infile)
{
cout<<"Cannot open input file. "<< endl;
return 1;
}
initialize(student);
infile >> score;

while (!infile.eof())
{ 
category = getCategory(score);
getStudent(category, student);

infile >> score;
} 

infile.close();
printArray(category, student);
	system ("pause");

return 0;
}

void initialize(int student[])
{
int j;
for (j=0;j<=maxCategory;j++)
{
student[j]=0;
}
}

void getStudent(int c, int s[])
{
if (c<=maxCategory)
{
s++;      
	}
}

void printArray(int category, int student[])
{
	string scoreRange;
	cout<<"Score Range"<<"          ";
	cout<<"Number of Student"<<endl;

	for (category=0; category<=maxCategory;category++)
	  {
		  switch (category)
		  {
		  case 0:
			  scoreRange= "0-24";
			  break;
                            case 1:
			  scoreRange= "25-49";
			  break;
	               case 2:
			  scoreRange= "50-74";
			  break;
		  case 3:
			  scoreRange= "75-99";
	                            break;	                                                                                                                       case 4:
			  scoreRange= "100-124";
			  break;
		  case 5:
			  scoreRange= "125-149";
			  break;
		  case 6:
			  scoreRange= "150-174";
			  break;
		  case 7:
			  scoreRange= "175-200";
			  break;
	  
		  }

	     cout<< scoreRange<<"                 ";
	      cout <<student[category]<<endl;	  
      }
}

int getCategory(int score)
{
    int scoreRange;

    if (score >=0 && score <25)
	scoreRange=0;
	
   else if (score >=25 && score <50)
	scoreRange=1;
	
   else if (score >=50 && score <75)
	scoreRange=2;

   else if (score >=75 && score < 100)
	scoreRange=3;

   else if (score >=100 && score <125)
	scoreRange=4;

   else if (score >=125 && score <150)
	scoreRange=5;

   else if (score >=150 && score <175)
	scoreRange=6;

   else if (score >=175 && score <=200)
	scoreRange=7;

   else
     cout<<"Invalid score";
     return scoreRange;
}
@ResidentBuscuit: I write my program in stages of meaningful operation. If meaningful operation involves writing the whole program, I'll write the whole program before testing.

@Isolde: I can't tell which part of your program has the problem of the categories printing 0. Could you tell us which lines are involved?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void initialize(int student[])
{
int j;
for (j=0;j<=maxCategory;j++)
{
student[j]=0;
}
}

void getStudent(int c, int s[])
{
if (c<=maxCategory)
{
s++;      
	}
}
Ciphermagi, thank you. The compiler did not give me an error on "category".
Oops. Ciphermagi. "category" is initialized here: category = getCategory(score);
Let's first start by giving the code proper tabbing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void initialize(int student[])
{
	int j;
	for (j=0;j<=maxCategory;j++)
	{
		student[j]=0;
	}
}

void getStudent(int c, int s[])
{
	if (c<=maxCategory)
	{
		s++;      
	}
}
Now, let's look at what it does.

initialize sets all the values to 0. (This could have been done in the declaration of the array being initialized...)

getStudent first checks if the first parameter passed to it is less than or the same as the maximum number of categories, then increments its local pointer to an integer array. Then it returns.

Now, let's check the logic.

initialize seems ok, although it is uneeded because arrays can be declared as such to be filled with 0:
int MyArray[100] = {}; //MyArray is now filled with 0s

getStudent is definitely not working as intended. It was probably meant to actually return an int rather than returning nothing at all, and the if structure was probably meant to be a for loop that loops through the students until it finds one that meets certain criteria. This criteria should also probably have been given to the function.
Thank you so very much L B. Class is starting now. I will look at this part of the code again. Thanks again.
Pages: 12