do while looping problem

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++

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
#include<iostream.h>
#include<conio.h>
int w,x,y,z,w2,num1,x2,y2,z2,total,total2,total3,num,loop;
char op;
void main()
{
do
{
loop=1;
clrscr();
       if (loop==1)
    {cout<<"****** GRADING SYSTEM ******\n";
    cout<<"-----------PRELIMS----------\n\n";}
       else  if (loop==2)
    {cout<<"****** GRADING SYSTEM ******\n";
    cout<<"----------MIDTERMS----------\n\n";}
       else if (loop==3)
    {cout<<"****** GRADING SYSTEM ******\n";
    cout<<"---------PREFINALS----------\n\n";}
       else if ((loop==4)||(loop<=4))
    {cout<<"****** GRADING SYSTEM ******\n";
    cout<<"------------FINALS----------\n\n";}
       else
     cout<<"null";
    {
     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);
    num1++;
    }

       cout<<"\n\nYour Grade is: "<<total;

       cout<<"\n\nCalculate another Grade? [y] or [n]\n";
       cin>>op;
       }
while((op=='y')||(op=='Y'));

getch();
}

Just some things I noticed.
1. Line 1; #include <iostream> (Delete the .h).

2. Line 2; delete #include<conio.h> replace with using namespace 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.






Last edited on
Here is your inner loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
     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.
Last edited on
i just want to print "prelims" before the quiz 1, quiz 2, CS, and ME..
then prints "Midterms" after inputing values in previous..

like:
=prelims=
quiz 1: 89
quiz 2: 90
CS: 85
ME: 98

then clrscr..

=midterms=
quiz 1: 87
quiz 2: 93
CS: 87
ME: 94

and so on..

after that the average grade,,
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.
Last edited on
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..

i made another 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
#include<iostream.h>
#include<conio.h>
int x,y,z,a,sum,num;
char option;
void main()
{
do
{
       num=1;
     do
      {
    clrscr();
    cout<<"=====Grading System=====\n";
    cout<<"======Enter Grades======\n\n";
    cout<<"Qiuz 1| ";
    cin>>x;
    cout<<"Quiz 2| ";
    cin>>y;
    cout<<"CS    | ";
    cin>>z;
    cout<<"ME    | ";
    cin>>a;
    num++;
          }

     while (num<=4);
      {
      x=(x*.10);
      y=(y*.20);
      z=(z*.30);
      a=(a*.40);
      sum=(x+y+z+a);
}
      sum=((sum+sum+sum+sum)/4);
    clrscr();
cout<<"=====Grading System=====\n\n";
cout<<"The Grade is:  [ "<<sum<<" ]";

cout<<"\n\nCompute another?";
    cin>>option;
}

while(option=='y');
getch();
}


still, i can't make a code that the "prelim" "midterm" "pre-final" or "finals" should appear in every loop once at a time..

anyway, thanks to you guys.. ΓΌ
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
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";
      else  if (loop==2) cout<<"----------MIDTERMS----------\n\n";
      else if (loop==3) cout<<"---------PREFINALS----------\n\n";
      else if (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;
}
Topic archived. No new replies allowed.