0

Kindly debug the following code

#include <iostream> using namespace std; int main() { int total,grade counter,grade,average; total=0; grade counter=1; while(grade counter<=3) { cout<<"enter grade"; cin>>grade; grade counter=grade counter+1; } average=total/3; cout<<average; return 0; }

23rd Oct 2018, 6:32 AM
Usman Zafar
Usman Zafar - avatar
9 Réponses
+ 5
Can't have spaces in variable names.
23rd Oct 2018, 6:34 AM
jay
jay - avatar
+ 5
And you ask for input, but don't use it.
23rd Oct 2018, 6:47 AM
Paul
Paul - avatar
+ 4
You have to initialize all your values: int grade_counter=1; int grade = 0; int average = 0; int total = 0;
25th Oct 2018, 5:00 AM
Paul
Paul - avatar
+ 4
It is printed every time the loop runs. You don't use line ends so it looks odd.
25th Oct 2018, 5:48 AM
Paul
Paul - avatar
+ 3
Hint: average = 0/3
23rd Oct 2018, 6:49 AM
jay
jay - avatar
+ 1
thanks alot sir paul jacob and mr. jay for ur time and kind guidance... God Bless u...
25th Oct 2018, 7:37 AM
Usman Zafar
Usman Zafar - avatar
0
//can you plz compile the code and verify it.. //i still not getting acurate result... #include <iostream> using namespace std; int main() { int grade_counter=1; int grade; int average; int total ; while(grade_counter <=3) { cout<<"enter grade"; cin>>grade; total=total+grade; grade_counter=grade_counter+1; } average=total/3; cout<<average; return 0; }
25th Oct 2018, 4:22 AM
Usman Zafar
Usman Zafar - avatar
0
great it make sense... getting accurate results.. but why it shows enter gradeenter gradeenter gradeenter as a o/p after compiling rather than it should show "average"
25th Oct 2018, 5:11 AM
Usman Zafar
Usman Zafar - avatar
0
//its my final code that is working as i wish #include <iostream> using namespace std; int main() { int grade_counter=1; int grade=0; int average=0; int total=0 ; while(grade_counter <=3) { cin>>grade; total=total+grade; grade_counter=grade_counter+1; } average=total/3; cout<<"average="<<average; return 0; }
25th Oct 2018, 7:33 AM
Usman Zafar
Usman Zafar - avatar