Do-while loop only executing when it feels like it in C++? -


so have do-while loop supposed repeat asking if want enter info student if press y , enter info via input() function data stored in vector. if press q, program supposed print info contained in vector , exit loop.

for reason loop executes first , second student enter. instead of repeating loop asking if want enter third student seems execute input() function. doesn't ask 'enter y continue or q quit' , student info enter here not seemed stored. intermittently changing between executing full loop , input() function. i'm wondering if knows why happening , can fix it.

cheers

#include <iostream> #include <iomanip> #include <cmath> #include <cstring> #include <string> #include <vector>  using namespace std;  const int no_of_test = 4;  struct studenttype {     string studentid;     string firstname;     string lastname;     string subjectname;     string coursegrade;      int arraymarks[4];      double avgmarks; };  studenttype input(); double calculate_avg(int marks[],int no_of_test); // returns average mark string calculate_grade (double avgmark); // returns grade   void main() {     unsigned int n=0; // no. of student     vector<studenttype> vec; // vector store student info     studenttype s;      char response;         {         cout << "\nenter y continue or q quit... ";         cin >> response;         if (response == 'y')         {             n++;             for(size_t i=0; i<n; ++i)             {                 s = input();                 vec.push_back(s);             }         }          else if (response == 'q')         {             (unsigned int y=0; y<n; y++)             {                 cout << "\nfirst name: " << vec[y].firstname;                 cout << "\nlast name: " << vec[y].lastname;                 cout << "\nstudent id: " << vec[y].studentid;                 cout << "\nsubject name: " << vec[y].subjectname;                 cout << "\naverage mark: " << vec[y].avgmarks;                 cout << "\ncourse grade: " << vec[y].coursegrade << endl << endl;             }         }     }     while(response!='q'); }  studenttype input() {        studenttype newstudent;      cout << "\nplease enter student information:\n";      cout << "\nfirst name: ";     cin >> newstudent.firstname;      cout << "\nlast name: ";     cin >> newstudent.lastname;      cout << "\nstudent id: ";     cin >> newstudent.studentid;      cout << "\nsubject name: ";     cin >> newstudent.subjectname;      (int x=0; x<no_of_test; x++)     {   cout << "\ntest " << x+1 << " mark: ";         cin >> newstudent.arraymarks[x];     }      newstudent.avgmarks = calculate_avg(newstudent.arraymarks,no_of_test );     newstudent.coursegrade = calculate_grade (newstudent.avgmarks);      return newstudent; }  double calculate_avg(int marks[], int no_of_test) {      double sum=0;       for( int i=0; i<no_of_test; i++)      {          sum = sum+ marks[i];      }       return sum/no_of_test;  }   string calculate_grade (double avgmark) {     string grade= "";      if (avgmark<50)     {         grade = "fail";     }      else if (avgmark<65)     {         grade = "pass";     }      else if (avgmark<75)     {         grade = "credit";     }             else if (avgmark<85)     {         grade = "distinction";     }      else     {         grade = "high distinction";     }      return grade; } 

i think code it:

        n++;         for(size_t i=0; i<n; ++i)         {             s = input();             vec.push_back(s);         } 

it asks 2 students second time, 3 student third time, etc.

so, make it

        vec.push_back(input()); 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -