What's wrong with my code?
Mar 13, 2019 at 3:57am UTC
I'm trying to code a program that asks a student for class , year, unit, grade and then displays those on a table then asks again for the next class unless they quit the program. I can't seem to figure out how to fix the errors. Thanks in advance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#include <string>
using std::string;
#include <iomanip>
using std::setw;
struct Course
{
string courseName;
int year;
double unit;
char grade;
};
Course cinOneCourse(int seq);
void coutAllCourses(Course[] courses, int size);
int main()
{
Course courses[100];
courses[0] = cinOneCourse(1);
int coursescount = 0;
for (int i = 0; i < 100; i++)
{
string status;
courses[i] = cinOneCourse(i + 1);
coursescount++;
cout << "Enter course #" << i + 2 << "[Q to exit]: " << endl;
cin >> status;
if (status == "Q" || status == "q" )
{
break ;
}
}
coutAllCourses(courses);
return 0;
}
Course cinOneCourse(int seq)
{
Course newCourse;
newCourse.courseName;
newCourse.year;
newCourse.unit;
newCourse.grade;
cout << "What year for ? [e.g., 2016]:" << endl;
cin >> newCourse.year;
cout << "How many units is ?" << endl;
cin >> newCourse.unit;
cout << "And what was your grade [? for in-progress or planned]: " << endl;
cin >> newCourse.grade;
return newCourse;
}
void coutAllCourses(Course[] courses, int size)
{
cout << "Course" << setw(10) << "Year" << setw(10) << "Unit" << setw(10) << "Grade" << endl;
cout << "------------" << setw(10) << "-------" << setw(10) << setw(10) << setw(10) << setw(10) << endl;
for (int i = 0; i < size; i++)
{
cout << courses[i].courseName << setw(10) << courses[i].year << setw(10) << courses[i].unit << setw(10) << courses[i].grade << endl;
}
}
Mar 13, 2019 at 4:21am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#include <string>
using std::string;
#include <iomanip>
using std::setw;
struct Course
{
string courseName;
int year;
double unit;
char grade;
};
Course cinOneCourse(int seq);
void coutAllCourses(Course courses[], int size); //!! [] in the wrong place
int main()
{
Course courses[100];
// courses[0] = cinOneCourse(1); this is done inside the loop
int coursescount = 0;
for (int i = 0; i < 100; i++)
{
string status;
courses[i] = cinOneCourse(i + 1);
coursescount++;
cout << "Enter course #" << i + 2 << "[Q to exit]: " << endl;
cin >> status;
if (status == "Q" || status == "q" )
{
break ;
}
}
coutAllCourses(courses,coursescount); //!! you promised to send a size
return 0;
}
Course cinOneCourse(int seq)
{
Course newCourse;
//!! bar.cpp:53:12: warning: statement has no effect [-Wunused-value]
//!! newCourse.courseName;
//!! newCourse.courseName;
//!! newCourse.year;
//!! newCourse.unit;
//!! newCourse.grade;
cout << "What year for ? [e.g., 2016]:" << endl;
cin >> newCourse.year;
cout << "How many units is ?" << endl;
cin >> newCourse.unit;
cout << "And what was your grade [? for in-progress or planned]: " << endl;
cin >> newCourse.grade;
return newCourse;
}
void coutAllCourses(Course courses[], int size) //!! [] in the wrong place
{
cout << "Course" << setw(10) << "Year" << setw(10) << "Unit" << setw(10) << "Grade" << endl;
cout << "------------" << setw(10) << "-------" << setw(10) << setw(10) << setw(10) << setw(10) << endl;
for (int i = 0; i < size; i++)
{
cout << courses[i].courseName << setw(10) << courses[i].year << setw(10) << courses[i].unit << setw(10) << courses[i].grade << endl;
}
}
Mar 13, 2019 at 5:02am UTC
Thank you so much! It runs now and works. Except one thing. After I enter the first class and it's details, I get prompted to enter course #2, after I enter course #2 I get prompted to enter Course #1 then the program runs as it's supposed to.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#include <string>
using std::string;
#include <iomanip>
using std::setw;
struct Course
{
string courseName;
int year;
double unit;
char grade;
};
Course cinOneCourse(int seq);
void coutAllCourses(Course courses[], int size);
int main()
{
Course courses[100];
int coursescount = 0;
for (int i = 0; i < 100; i++)
{
string status;
courses[i] = cinOneCourse(i + 1);
coursescount++;
cout << "Enter course #" << i + 2 << "[Q to exit]: " << endl;
cin >> status;
if (status == "Q" || status == "q" )
{
break ;
}
}
coutAllCourses(courses, coursescount);
return 0;
}
Course cinOneCourse(int seq)
{
Course newCourse;
cout << "Enter course #1 [Q to exit]: " << endl;
cin >> newCourse.courseName;
cout << "What year for " << newCourse.courseName << "? [e.g., 2016]:" << endl;
cin >> newCourse.year;
cout << "How many units is " << newCourse.courseName << "?" << endl;
cin >> newCourse.unit;
cout << "And what was your grade [? for in-progress or planned]: " << endl;
cin >> newCourse.grade;
return newCourse;
}
void coutAllCourses(Course courses[], int size)
{
cout << "Course" << setw(10) << "Year" << setw(10) << "Unit" << setw(10) << "Grade" << endl;
cout << "-------" << setw(10) << "-----" << setw(10) << "-----" << setw(10) << "------" << endl;
for (int i = 0; i < size; i++)
{
cout << courses[i].courseName << setw(8) << courses[i].year << setw(8) << courses[i].unit << setw(8) << courses[i].grade << endl;
}
}
Mar 13, 2019 at 5:46am UTC
> cout << "Enter course #1 [Q to exit]: " << endl;
Maybe change this to
cout << "Enter course #" << seq << endl;
There's no point asking about Q in cinOneCourse, because you don't handle Q in that function.
Mar 13, 2019 at 5:51am UTC
So I did that but now instead of asking for Course #1 it just duplicates the question each time except for the first time.
Topic archived. No new replies allowed.