i have written this program, it's nearly finished, but i didn't found out right away that my code has bug..

once i enter number of students i wish to add, the value goes to the for loop, i think i coded that just fine, but the problem is, for example, i entered 2 as the number of student, then it goes to the for loop, then the for loop runs just once instead of twice since i entered 2 as the limit.. its like

for(i=0;i<number;i++)

the number value is 2.. then the loop is supposed to run twice, but it only runs once.. help!

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
#include<iomanip.h>
#include<string.h>
#define VAL 100
struct students
{
   char name[30];
   int quiz[2],ass,act,exam;
   float aveq[2],aveex,cs,pg,totaveq;
}grade[VAL];
void prompt();
void menu();
void option();
void addrecord();
void viewrecord();
int count=0,counts=1;
float totex=0,totq[2]={0,0};
char chr;
int main()
{
  // prompt();
  menu();
   getch();
   return 0;
}
void prompt()
{
   int s;
a: clrscr();
   char user[]="michael",pass[]="jokershrek";
   while(counts<=3)
   {
      cout<<"username: "; cin>>user;
      cout<<"password: "; cin>>pass;
      if((strcmp(user,"michael")==0)&&(strcmp(pass,"jokershrek")==0))
      {
	 cout<<"Redirecting. Please wait";
	 for(s=0;s<4;s++)
	 {
	    cout<<'.';
	    delay(300);
	 }
	 menu();
      }
      else
      {
	 cout<<"\nACCESS DENIED!";
	 delay(300);
	 counts++;
	 goto a;
      }
   }
   delay(300);
   cout<<"\n\nYou have entered 3 consective incorrect U&P\nTerminating program";
   for(s=1;s<4;s++)
   {
      cout<<'.';
      delay(500);
   }
   exit(0);
}
void menu()
{
   clrscr();
   cout<<"\t\tSTUDENT RECORD SYSTEM\n\n";
   cout<<"[A] Add record(s)\n"
       <<"[V] View record(s)\n"
       <<"[X] Exit program \t";
   cin>>chr;
   if(chr=='A'||chr=='a')
      addrecord();
   else if(chr=='V'||chr=='v')
      viewrecord();
   else if(chr=='X'||chr=='x')
      exit(0);
   else
      menu();
}
void option()
{
   cout<<"\n[1] Add another entry"
       <<"\n[2] Return to menu\t";
   cin>>chr;
   if(chr=='1')
      addrecord();
   else if(chr=='2')
      menu();
   else
      option();
}
void addrecord()
{
   clrscr();
   int number,i;
   [B]cout<<"How many entries do you want to add? ";
   cin>>number;               //defines the numbers of student's grade input[/B]
   if(totex==0)    //shows only if the value of totalexam is 0
   {
      cout<<"Exam perfect score: ";
      cin>>totex;
   }
   else if(totex>0)  //shows only if there are value stored in totex>0
      cout<<"Exam perfect score: "<<totex;
   if(totq[0]==0&&totq[1]==0) // shows only if the value
   {                          // of 2 quizzes PS is 0
      for(int x=0;x<=1;x++)
      {
	 cout<<"Quiz #"<<x+1<<" perfect score: ";
	 cin>>totq[x];
      }
   }
   else if(totq[0]>0&&totq[1]>0) //shows the value of PS of two
   {                             //quizzes if totq is greater than 0
      for(int x=0;x<=1;x++)
      {
	 cout<<"\nQuiz #"<<x+1<<" perfect score: "<<totq[x];
      }
   }
   [B]for(i=0;i<number;i++)[/B]   //this is the line
   {
      cout<<"\n\n||STUDENT #"<<count+1<<"||\n\n";
      cout<<"NAME: ";
      cin>>grade[count].name;
      cout<<"   Prelim Exam: ";
      cin>>grade[count].exam;
      for(int j=0;j<=1;j++)
      {
	  cout<<"   Quiz #"<<j+1<<": ";
	  cin>>grade[count].quiz[j];
      }
      cout<<"   Activity ave: ";
      cin>>grade[count].act;
      cout<<"   Assignment ave: ";
      cin>>grade[count].ass;
      //computation part start
      /* this part of the code computes the inputs
	 and stores the computed values in the stucture
	 named students */
      grade[count].aveex=grade[count].exam/totex*50+50;
      for(i=0;i<=number;i++)
      {
	 grade[count].aveq[i]=grade[count].quiz[i]/totq[i]*50+50;
	 grade[count].totaveq+=grade[count].aveq[i];
      }
      grade[count].cs=((grade[count].totaveq/2)+grade[count].ass+grade[count].act)/3;
      grade[count].pg=(grade[count].cs*.6)+(grade[count].aveex*.4);
      //computation part end
      count++;  //for the next input of student's grade
   }
   clrscr();
   menu();
}
void viewrecord()
{
   clrscr();
   int l,k;
   if(count>0)
   {
      cout<<"\t\tSTUDENT RECORDS\n\n"
	  <<"   NAME          P.EXAM      Q1      Q2      ACT      ASS      PRELIM GRADE\n";
      for(l=0;l<count;l++)
      {
	 cout<<l+1<<"  "
	     <<grade[l].name;
	 gotoxy(18,4+l);
	 cout<<setprecision(2)<<grade[l].aveex;
	 for(int j=0,k=0;j<=1,k<=8;j++,k+=8)
	 {
	     gotoxy(k+30,4+l);
	     cout<<setprecision(2)<<grade[l].aveq[j];
	 }
	 gotoxy(46,4+l);
	 cout<<setprecision(2)<<grade[l].act;
	 gotoxy(55,4+l);
	 cout<<setprecision(2)<<grade[l].ass;
	 //  <<setprecision(2)<<grade[i].cs<<"\n"
	 gotoxy(64,4+l);
	 cout<<setprecision(2)<<grade[l].pg<<"\n";
      }
   }
   else
      cout<<"NO ENTRY FOUND!\n";
   option();
}

Recommended Answers

All 2 Replies

for (i=0;i<=number;i++)

Let me just check, I am seeing goto in a program that you want to submit for marking. The only time I have ever seen that was a stunningly bright student in an advanced class showing how scheme could be used to optimize a mathematical C++ program -- and he wrote about 500 words to justify it.

Anyway the error is highighted by Comatose but in the most obtuse way possible. It is actually that you use a global variable int i, rather than for(int i=0;i<number;i++) and then proceed to have TWO loops one in the other that both use i.
Starting from your code line 122, and continuing

for(i=0;i<number;i++)
{
// Your stuff....
 grade[count].aveex=grade[count].exam/totex*50+50;
      for(i=0;i<=number;i++)
      {
	 grade[count].aveq[i]=grade[count].quiz[i]/totq[i]*50+50;
	 grade[count].totaveq+=grade[count].aveq[i];
      }
//more of your stuff
}

Note that i as ALWAYS number at line 10, and your outer loop can only run once.

I think that you have the closure of the loop in the wrong place, but the code is so platform dependent (clrsrn / delay and such) that I simply can't be bothered to run it.

You can also avoid the problem by writing for(int i=0;i<=number;i++) on line 143 BUT again you really don't want i to get to number since you will have an array error.

In short the code is a mess and you have made a mistake by using a variable in too large a scope (i is in ALL of addrecord) when it should only be in the loops required.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.