can you sugest some good code for this bad 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
#include<iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
	int gradeCounter,grade,average,total;
	
	total=0;
	gradeCounter=1;

	while (gradeCounter<=10)
	{
		cout<<"Enter grade subject:";
		cin>>grade;
		total=grade+total;
		gradeCounter=gradeCounter+1;
	}
		
	average=total/10;
	cout<<"Class average is"<<average<<endl;
    return 0;
}
The program is working successfully. How good the program is supposed to be? Do you mean in term of the simplicity or is it about upgrading the program?
If simplicity is the main concern, using the for loop would make the program simpler,i.e:
for(gradeCounter=0;gradeCounter<10;gradeCounter++)

Last edited on
Assuming that the program should work exactly like what you posted, I suggest:
- declare variables when needed
- define named constants instead of putting literal numbers in the code
- simplify mainline logic using functions (a function is useful if you reduce the number of variables needed in the calling function, e.g. main)
- put a space between output text and values

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
#include<iostream>

using std::cout;
using std::cin;
using std::endl;
using std::istream;

const int NUMBER_OF_GRADES = 10;

int readGrade(istream& stream) 
{
  int grade;
  stream >> grade;
  return grade;
}

int main()
{
  int total=0;
  for (int c = 0; c < NUMBER_OF_GRADES; c++) {
    cout << "Enter grade subject: ";
    total += readGrade(cin);
  }
  int average = total / NUMBER_OF_GRADES;
  cout << "Class average is " << average << endl;

  return 0; // this is optional
}
Last edited on
tnX ropez .....are you a filipino.....?
do you remember recently...
Im the one who posted .....tht you said that using .h is not nescessity your absolutely right....

your great tnx pal.
Last edited on
No, I'm not a filipino :-)
a shorcut instead of using all of
using std::cout;
using std::cin;
using std::endl;
using std::istream;
these is just in the header put
using namespace std;
closed account (z05DSL3A)
Putting 'using namespace std;' in the file would put all 'names' from the std namespace into a more global name space (depending on what namespace you put the call in). It would be better to only put the names that you require in the namespace, as Ropez and takemewithyou have done, to reduce the possibility of name clashing from other sources. It would be even better to not use the ‘using …’ method and qualify your names as they are used (but that is more typing).
Last edited on
>> It would be even better to not use the ‘using …’ method and qualify your names as they are used

I agree, and the result would be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>

const int NUMBER_OF_GRADES = 10;

int readGrade(std::istream& stream) 
{
  int grade;
  stream >> grade;
  return grade;
}

int main()
{
  int total=0;
  for (int c = 0; c < NUMBER_OF_GRADES; c++) {
    std::cout << "Enter grade subject: ";
    total += readGrade(std::cin);
  }
  int average = total / NUMBER_OF_GRADES;
  std::cout << "Class average is " << average << std::endl;

  return 0; // this is optional
}
Topic archived. No new replies allowed.