Click here to Skip to main content
15,885,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
not being able to find the error, only takes the input of 1 student, and when you enter the name of the second it just skips everything and shows weird numbers as a result of each except for the first one.here's the code(ps I'm a beginner to c++)
C++
#include<iostream>
#include<stdio.h>
using namespace std;
struct student{
    int age, marks[5], clas, avg;
    char name[10], res;
};
int main(){
    student p[5];
    cout<<"enter name, class, age, and marks in 5 subjects :\n";
    for(int i=0; i<5; i++){                   //input data for each student
        cout<<"student "<<i+1<<"\n";
        gets(p[i].name);
        cin>>p[i].clas;
        cin>>p[i].age;
        for(int j=0; j<5; j++){
            cin>>p[i].marks[j];
        }
    }
    for(int k=0; k<5; k++){     //calculate average                                           
        p[k].avg=0;
        for(int f=0; f<5; f++){
            p[k].avg+=p[k].marks[f];
        }
        p[k].avg/=5;
    }
    for(int x=0; x<5; x++){           //calculate result on basis of average                              
        if(p[x].avg>=40){
            p[x].res='P';
        }
        else{
            p[x].res='F';
        }
    }
    cout<<"RESULT :\n";     //output data for each student
    for(int m=0; m<5; m++){                                     
        cout<<"STUDENT "<<m+1<<"\n";
        puts(p[m].name);
        cout<<"age "<<p[m].age;
        cout<<"class "<<p[m].clas;
        for(int n=0; n<5; n++){
            cout<<p[m].marks[n]<<endl;
        }
        cout<<"average "<<p[m].avg;
        cout<<"result "<<p[m].res;
    }
}


What I have tried:

at first, i changed cin.getline to gets but still does not work properly, it does not take the input properly and the result is random numbers, except for the first student whose result displayed is correct
Posted
Updated 15-Mar-18 0:04am
Comments
Peter_in_2780 15-Mar-18 0:50am    
Mixing gets() and "cin >> ..." can lead to some weird results, depending on buffering and so on. I suggest you stick to one means of input.
Patrice T 15-Mar-18 2:13am    
"i changed cin.getline to gets but still does not work properly"
Choose 1, never mix.

Hey man, you have C++, don't you?
C++
#include <iostream>
#include <vector>
#include <array>
#include <sstream>
using namespace std;


class Student
{
  string name;
  int clas;
  int age;
  array<int, 5> mark;

public:
  double average();
  char result(double average);

  bool fromUser(const string & prompt);
  void toUser(const string & header);
};

bool Student::fromUser(const string & prompt)
{
  cout << prompt << endl;
  cout << "name: ";
  cin >> name;

  cout << "class: ";
  cin >> clas;
  cout << "age: ";
  cin >> age;
  cout << "now enter 5 marks\n";

  for ( auto & m : mark)
    cin >> m;

  return cin.good();
}

double Student::average()
{
  double avg = 0.0;
  for (const auto &  m : mark)
    avg += m;

  avg /= mark.size();
  return avg;
}

char Student::result(double average)
{
  return (average >= 40.0 ? 'P': 'F');
}

void Student::toUser(const string & header)
{
  cout << header << endl;
  cout << "name: " << name << endl;
  cout << "class: " << clas << endl;
  cout << "age: " << age << endl;
  cout << "marks: " << endl;
  for (const auto & m : mark)
    cout << "  " << m << endl;

  double avg = average();
  cout << "average: " << avg << endl;
  cout << "result: " << result(avg) << endl;
}

int main()
{
  vector <Student> student;
  constexpr int STUDENTS=2;

  for (int i=0; i<STUDENTS; ++i)
  {
    ostringstream oss;
    oss << "please enter data for student " << (i+1) ;
    Student s;
    s.fromUser(oss.str());
    student.push_back(s);
  }

  cout << "******************************" << endl;

  for (int i=0; i<student.size(); ++i)
  {
    ostringstream oss;
    oss << endl << "data of student " << (i+1);
    student[i].toUser(oss.str());
  }
}
 
Share this answer
 
v2
Comments
Ketan2305 17-Mar-18 4:54am    
well we have not learned class and bool
yes i have c++
CPallini 17-Mar-18 5:21am    
OK. So
Don't mix up C I/O (printf, scanf, fgets) with C++ streams (cin, cout).
NEVER use gets.
Ketan2305 17-Mar-18 4:55am    
not vectors or the sstream library
im a beginner
You need to work with the debugger. Search some online tutorials for the usage in your development IDE.

I guess that you mismatched the types in your code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900