I cannot run this c++ coding. Please fix the errors I made. #include <iostream>
ID: 3839276 • Letter: I
Question
I cannot run this c++ coding.
Please fix the errors I made.
#include <iostream>
#define size 20
using namespace std;
struct details
{
char name[40];
int student_ID;
float GPA;
};
void main()
{
details data[20];
int i;
for (i=0;i<size;i++);
{
cout<<"Please enter the student name:"<<i+1<<".";
cin.getline(data[i].name,40);
cout<<"Please enter student's ID:"<<i+1<<".";
cout <<"Please enter student's GPA:"<<i+1<<".";
cin>>data[1].GPA;
cout<<"The student's name is:"<<data[i].name<<endl;
cout<<"The student's ID is:"<<data[i].student_ID<<endl;
cout<<"Please enter the student's GPA:"<<data[i].GPA<<endl;
}
}
Explanation / Answer
I have fixed the code . Here are the details about it
1. You have put a semi-colon at the end of for loop line which is incorrect, so removed that
2. Return type of main should be int not void, changed that
3. Since you want to ask the user to enter details for student no. i , I have modified code to do that .
4. Also normally we would want to accept the data first and display later. so modified code to display data after input of all data.
5. For the purpose of testing , I have changed size to 3 so its easy to test the code. You can change it whatever value you want.
Output attached. Please don't forget to rate the answer if it helped. Thank you very much.
#include <iostream>
#define size 3
using namespace std;
struct details
{
char name[40];
int student_ID;
float GPA;
};
int main()
{
details data[20];
int i;
cout<<"Enter details for "<<size<<" students"<<endl<<endl;
//get all student details one by one
for (i=0;i<size;i++)
{
cout<<"Enter details for student "<<(i+1)<<endl;
cout<<" Student ID: ";
cin>> data[i].student_ID;
cin.ignore();
cout<<" Student name: ";
cin.getline(data[i].name,40);
cout <<" Student's GPA: ";
cin>>data[i].GPA;
}
//now display all student details one by one
cout<<" The details of students are "<<endl<<endl;
for(i=0; i<size; i++)
{
cout<< data[i].student_ID << " ";
cout<< data[i].name<< " ";
cout<< data[i].GPA<<endl;
}
}
output
Enter details for 3 students
Enter details for student 1
Student ID: 111
Student name: John
Student's GPA: 89
Enter details for student 2
Student ID: 222
Student name: Alice
Student's GPA: 78
Enter details for student 3
Student ID: 333
Student name: Bob
Student's GPA: 67
The details of students are
111 John 89
222 Alice 78
333 Bob 67
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.