C++ Logic Error When does it not ask me two input the name the second time? #inc
ID: 3769850 • Letter: C
Question
C++ Logic Error
When does it not ask me two input the name the second time?
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct studentRecord
{
string name;
int StudentID;
int Scores[3];
double Average;
};
int main()
{
const int NUM_STUDENTS = 3, NUM_TESTS = 3;
int Total = 0;
studentRecord students[NUM_STUDENTS];
cout << "Enter the information for the " << NUM_STUDENTS << " students: " << endl << endl;
for (int index = 0; index < NUM_STUDENTS; index++)
{
cout << "Enter the name of student " << index + 1 << " in this format: Lastname Firstname " << endl;
getline(cin, students[index].name);
cout << "Enter the Student ID of student " << index + 1 << endl;
cin >> students[index].StudentID;
cout << "Enter the three scores of student " << index + 1 << endl;
for (int i = 0; i < NUM_TESTS; i++)
{
cin >> students[index].Scores[i];
Total = Total + students[index].Scores[i];
}
cout << endl;
students[index].Average = Total / NUM_TESTS;
}
cout << "ID" << setw(10) << "Name" << setw(20) << "Score1" << setw(30) << "Score2" << setw(40) << "Score3" << setw(50) << "Average" << endl;
for (int a = 0; a < NUM_STUDENTS; a++)
{
cout << students[a].StudentID << setw(10) << students[a].name << setw(20) << students[a].Scores[0] << setw(30) << students[a].Scores[1] << setw(40) << students[a].Scores[2] << setw(50) << students[a].Average << endl;
}
int HighestValue = 0, ArrayNumber;
if (students[0].Average < students[1].Average)
{
HighestValue = students[1].Average;
ArrayNumber = 1;
if(students[1].Average < students[2].Average)
{
HighestValue = students[2].Average;
ArrayNumber = 2;
}
}
else
{
HighestValue = students[0].Average;
ArrayNumber = 0;
if(students[2].Average > students[0].Average)
{
HighestValue = students[2].Average;
ArrayNumber = 2;
}
}
cout << "Highest Average is " << HighestValue << " scored by ID #" << students[ArrayNumber].StudentID << ", " << students[ArrayNumber].name << endl;
return 0;
}
Explanation / Answer
/*c++ Program to find Average Marks of Students in 3 subject*/
#include<iostream.h>
#include<conio.h>
class student
{
private :
int usn,m[4];
char name[20];
float avg;
public :
void read();
void avgm();
void print();
};
void student :: read()
{
cout<<"Enter the student no. :";
cin>>usn;
cout<<"Enter the name of the student :";
cin>>name;
cout<<name<<endl;
cout<<"Enter the marks of 3 subjects :";
cin>>m[1]>>m[2]>>m[3];
avgm();
}
void student :: avgm()
{
if(m[1]<=m[2] && m[1]<=m[3])
avg=(float)(m[2]+m[3])/2;
if(m[2]<=m[1] && m[2]<=m[3])
avg=(float)(m[1]+m[3])/2;
if(m[3]<=m[1] && m[3]<=m[2])
avg=(float)(m[1]+m[2])/2;
}
void student :: print()
{
cout<<" "<<usn<<" "<<name<<" "<<avg;
}
void main()
{
int n,i;
student s[10];
clrscr();
cout<<"Enter the no of students :";
cin>>n;
for(i=0;i<n;i++)
s[i].read();
cout<<"USN NAME AVVEAGE MARKS ";
for(i=0;i<n;i++)
s[i].print();
getch();
}
output:- run the program in turbo cpp editor
Enter the no of students :2
Enter the student no. :02
Enter the name of the student : sat
Enter the marks of 3 subjects :89
78
76
Enter the student no. :21
Enter the name of the student :ram
Enter the marks of 3 subjects :78
89
76
USN NAME AVVEAGE MARKS
2 sat 83.5
21 ram 83.5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.