Need some help.

So I have this code that supposes to look clean and organized. I'm having issues with keeping class, cadets, and price in order. Tell me what you guys think. Thanks for taking the time as well coders.

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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main ()
{
    int totalCreditHours;
    int creditHours1;
    int creditHours2;
    int creditHours3;
    int creditHours4;
    double totalCourseCost;
    double costPerCreditHour;
    double courseCost1;
    double courseCost2;
    double courseCost3;
    double courseCost4;

    cout << fixed << showpoint << setprecision(2);

    cout << "Enter the first course code (with no spaces) " << endl;
    cout << "then tab and enter the credit hours and then" << endl;
    cout << "tab and enter the course cost. Then repeat" << endl;
    cout << "these steps for the other three courses: " << endl;

    string courseCode1;
    getline(cin,courseCode1);
    cin >> creditHours1 >> courseCost1;
    cout << endl;

    string courseCode2;
    getline(cin,courseCode2);
    cin >> creditHours2 >> courseCost2;
    cout << endl;

    string courseCode3;
    getline(cin,courseCode3);
    cin >> creditHours3 >> courseCost3;
    cout << endl;

    string courseCode4;
    getline(cin,courseCode4);
    cin >> creditHours4 >> courseCost4;
    cout << endl;

    cout << "Course Code" << setw( 15 ) << "Credit Hours" << setw( 15 ) << "Course Cost" << endl;
    cout << endl;
    cout << courseCode1 << setw(14) << creditHours1 << setw(18) << courseCost1 << endl;
    cout << courseCode2 << setw(14) << creditHours2 << setw(18) << courseCost2 << endl;
    cout << courseCode3 << setw(14) << creditHours3 << setw(18) << courseCost3 << endl;
    cout << courseCode4 << setw(14) << creditHours4 << setw(18) << courseCost4 << endl;
    cout << endl;

    totalCreditHours = creditHours1 + creditHours2 + creditHours3 + creditHours4;
    cout << "Total Credit Hours: " << totalCreditHours << endl;

    totalCourseCost = courseCost1 + courseCost2 + courseCost3 + courseCost4;
    cout << "Total Course Costs: $" << totalCourseCost << endl;

    costPerCreditHour = totalCourseCost / totalCreditHours;
    cout << "Cost per Credit Hours: $" << costPerCreditHour;
    
    return 0;
}
So, what is your issue?

Things like creditHours1...4 and courseCost1...4 are usually arrays. And since they seems to belong together it might make sense to put them into a struct. With an array you can make a loop instead of that code repetition.
ohhhh, ok. I'm still new at arrays and loops. Could you show me one example please?
Hello Frank5093,

Some thoughts about your program:
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
86
87
88
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
    int totalCreditHours;
    int creditHours1;  // <--- Could change these 4 variables into an array.
    int creditHours2;
    int creditHours3;
    int creditHours4;
    double totalCourseCost;
    double costPerCreditHour;
    double courseCost1;  // <--- Could change these 4 variables into an array.
    double courseCost2;
    double courseCost3;
    double courseCost4;

    cout << fixed << showpoint << setprecision(2);

    cout <<
        "Enter the first course code (with no spaces) \n"
        "then tab and enter the credit hours and then\n"  // <--- Do not need to use "tab" a single space will do.
        "tab and enter the course cost. Then repeat\n"
        "these steps for the other three courses:\n";

    string courseCode1;

    // <<--- Needs a prompt.
    std::cout << "\n Enter cource Code: ";
    getline(cin, courseCode1);

    // <<--- Needs a prompt.
    cin >> creditHours1 >> courseCost1;  // <--- Also would be better to prompt and enter each separately.

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    //std::cin.ignore();  // <--- Could also use this if just the "\n" needs removed. or "std::cin.get();".

    cout << '\n';

    string courseCode2;

    // <<--- Needs a prompt.
    getline(cin, courseCode2);

    // <<--- Needs a prompt.
    cin >> creditHours2 >> courseCost2;

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

    cout << '\n';

    string courseCode3;
    getline(cin, courseCode3);
    cin >> creditHours3 >> courseCost3;
    cout << '\n';

    string courseCode4;
    getline(cin, courseCode4);
    cin >> creditHours4 >> courseCost4;
    cout << '\n';

    cout
        << "Course Code" << setw(15) << "Credit Hours" << setw(15) << "Course Cost\n\n"
        << courseCode1 << setw(14) << creditHours1 << setw(18) << courseCost1 << '\n'
        << courseCode2 << setw(14) << creditHours2 << setw(18) << courseCost2 << '\n'
        << courseCode3 << setw(14) << creditHours3 << setw(18) << courseCost3 << '\n'
        << courseCode4 << setw(14) << creditHours4 << setw(18) << courseCost4 << "\n\n";

    totalCreditHours = creditHours1 + creditHours2 + creditHours3 + creditHours4;
    cout << "Total Credit Hours: " << totalCreditHours << '\n';

    totalCourseCost = courseCost1 + courseCost2 + courseCost3 + courseCost4;
    cout << "Total Course Costs: $" << totalCourseCost << '\n';

    costPerCreditHour = totalCourseCost / totalCreditHours;
    cout << "Cost per Credit Hours: $" << costPerCreditHour;

    // A fair C++ replacement for "system("pause")". Or a way to pause the program.
    // The next line may not be needed. If you have to press enter to see the prompt it is not needed.
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    std::cout << "\n\n Press Enter to continue: ";
    std::cin.get();

    return 0;  // <--- Not required, but makes a good break point.
}

Thoughts about your code for now not that I have run it completely.

As coder777 mentioned I would consider using a struct to hold the information . Then in "main" you could use an array of structs or a vector. I would lean towards the vector.

Using an array and a for loop you could enter directly into the struct of each element without the need to define any variables for your input. A small savings.

Note try to use the new line (\n) over the "endl" as much as you can. "endl" is a function that takes time, so the more you have the slower the program will become. Also code like:
1
2
std::cout << "\n Enter cource Code: ";
getline(cin, courseCode1);

or using formatted input will flush the output buffer before any input is taken. You can use this to your advantage.

After formatted input cin >> creditHours1 >> courseCost1; the "\n" is left in the input buffer. Not a problem if followed by formatted input, but a problem when followed by a "getline", unformatted input, which will extract the "\n" and move on not waiting for any user input.

Using a separate "cout" for each line is OK, but lines 24 - 27 create 1 large string that the "cout" can print and it looks more like what you will see on the screen.

The other "cout", lines 65 - 70, show how to use the insertion operator to chain everything together.

I will work up an example shortly.

Andy
Consider for C++17:

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
#include <iostream>
#include <iomanip>
#include <string>
#include <limits>
#include <vector>

using namespace std;

struct Course {
	string code;
	int hours {};
	double cost {};
};

int main()
{
	vector<Course> courses;
	int totalCreditHours {};
	double totalCourseCost {};

	for (Course c; (cout << "Enter course code (<CR> to finish) :") && getline(cin, c.code) && !c.code.empty(); courses.push_back(c)) {
		cout << "Enter credit hours: ";
		cin >> c.hours;

		cout << "Enter course cost: ";
		cin >> c.cost;

		cin.ignore(numeric_limits<streamsize>::max(), '\n');

		totalCreditHours += c.hours;
		totalCourseCost += c.cost;
	}

	cout << setw(15) << "\nCourse Code" << setw(15) << "Credit Hours" << setw(15) << "Course Cost\n";
	cout << left << fixed << showpoint << setprecision(2);

	for (const auto& [code, cred, cost] : courses)
		cout << setw(14) << code << setw(15) << cred << setw(15) << cost << '\n';

	cout << "\nTotal Credit Hours: " << totalCreditHours << '\n';
	cout << "Total Course Costs: $" << totalCourseCost << '\n';
	cout << "Cost per Credit Hours: $" << totalCourseCost / totalCreditHours;
}

Last edited on
Topic archived. No new replies allowed.