i having a problem with my code, i don't yet know how to use the do while, but i want to do this with do while.. this program will compute grades.. 1st loop, it will be "prelims", 2nd "midterms" and until "Finals".. i'm having trouble with the loop because it don't have end.. it continues asking the quiz 1, quiz2, CS and ME, repeatedly.. i'm using turbo c++
Just some things I noticed.
1. Line 1; #include <iostream> (Delete the .h).
2. Line 2; delete #include<conio.h> replace with usingnamespace std;
3. Line 3; Put
1 2
int w,x,y,z,w2,num1,x2,y2,z2,total,total2,total3,num,loop;
char op;
inside main.
4. Change void main() to int main()
5. On line 56 replace getch(); with cin.ignore().get();
6. After line 56 and before closing brace put return 0;
7. Get rid of clrscr(); on line 10.
8. Also the curly braces at line 26 and 48, not sure why they are there?
After that I think you may have to rethink the basic logic of your program. Im not sure what you are trying to achieve. For instance loop =1 is always going to mean the program will go to the if statement
1 2 3
if (loop==1)
{cout<<"****** GRADING SYSTEM ******\n";
cout<<"-----------PRELIMS----------\n\n";}
It will never have the option of going to the other else if statements ie; if loop ==2 // ==3 etc because loop is always loop = 1 as you wrote on line 9.
do
{
num1=1;
cout<<"Quiz 1: ";
cin>>w;
cout<<"\nQuiz 2: ";
cin>>x;
cout<<"\nClass standing: ";
cin>>y;
cout<<"\nMajor Exam: ";
cin>>z;
w2=w*0.20;
x2=x*0.20;
y2=y*0.30;
z2=z*0.30;
total=(w2+x2+y2+z2);
}
while (num1<=4);//nu1 = 1, so num1 will never be > 4
I'm not sure what your code shold exactly do, but I suppose you need to put num1=1; and add total = 0; before do, put num1++; inside do ... while (num1<=4); and change total=... to total+=...
Few notes: Do not mix c++ and c libraries that do the same thing, conio.h is not standard, I believe. This isn't pascal, you can declare your variables wherever you need. It is not a good practice to have a lot of globals. Most of them aer used in a small scope anyway.. (loop==4)||(loop<=4) is not right. if loop was < 4, you would never reach this if. One thing you may want to change in your code, is that when loop > 4, you will still be asked for Quiz 1, Quiz 2, etc.
edit: too slow.. Anyway, as AlphaBravo pointed out you should also move loop=1; before the first do. Now loop is reset on every time and will never grow to 2 or more.
The reason AlphaBravo suggested you remove clrscr() was because it makes your code non-portable. In the sense that in a few months time you may want to use this same code on Unix console, or you may want to make a MS windows GUI application, you cannot clear the screen on GUI apps, so it does make your code somewhat limited. It also means people reviewing your code on these forums need to re-write things to get it to work on their systems. So it doesn't help you, if they see that then ignore the thread entirely.
edit; and the same goes for all the other suggestions, void main() is compiler specific, while int main() is standard. #include <iostream> is standard, #include <iostream.h> is not.
conio.h is a windows header file, so additionally it doesn't work on some of our systems and is NON-standard. I believe clrscr() and getch() fall under the conio.h header.
you peoples are to advance and smart..
i'm just a student and beginner in using c++..
the codes i used was what our instructor says..
so, we should use what he had teach..
sorry, i can't use what you teach to me right now..
int main{
int loop = 0;//set before looping
char option;//yay locals
do{
cout<<"****** GRADING SYSTEM ******\n";//avoid writing the same thing several times
if (loop==1) cout<<"-----------PRELIMS----------\n\n";
elseif (loop==2) cout<<"----------MIDTERMS----------\n\n";
elseif (loop==3) cout<<"---------PREFINALS----------\n\n";
elseif (loop==4) cout<<"------------FINALS----------\n\n";
else cout<<"null";
int num = 1;
int sum = 0;
do{
int x, y, z, a;
//ask for some numbers
sum += x*.2+y*.2+z*.3+a*.3;
}while(num <= 4);
//output
loop++;
cin >> option;
}while(option == 'y');
return 0;
}