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