Hello! I am in a beginning C++ course and a few weeks ago, we were to create a basic degree-auditing program using arrays, loops, and if statements (no functions). In a nutshell, the user is asked how many courses to include (maximum of 10), then the program loops through, asking for the course name/semester taken/etc. After the info is retrieved, the user is displayed a menu with different calculation options. One of the calculation options is to display all of the courses/course information the user has entered thus far. My grade on the project wasn't bad (79%), but I'd like to fix my errors before we move on to the project regarding functions.
My code is looping and storing some of the user-inputted information, but not all of it. For instance, if I run the program for two courses, when I choose to display the courses/course info, my code will only print the information for the second course. I'm sure the issue is mainly concerning my array initialization, but I'm not sure how to fix it. I originally had CourseInfo/CourseTime/CourseNumber as strings instead of chars, but after reading many articles here and on stackoverflow, I changed them. I've also gone back and forth between cin.getline() and std::getline() to gather the information. I'm not sure which is correct to use.
TLDR----Problem with storing, then outputting, user-inputted strings. Is my problem in my array? Am I calling the wrong thing to output? I'm stumped :/
Here is part of the code---I know it's still pretty long. I tried to shorten it, and I'm sorry if it could have been shorter!!
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
// Include statements
#include <iostream>
#include <string>
#include <cstdlib>
#include <limits>
// Main function
int main ()
{
// Variables
int count;
const int size = 1000;
int TotalCourses[10] = {};
double LetterGrade[10];
double CreditHour[10] = {0};
char CourseGrade[10];
char CourseInfo[size];
char CourseTime[size];
char CourseTime2[size];
char CourseNumber[size];
char choice;
// Get TotalCourses[10]
cout << " " << endl;
cout << "How many courses do you have?" << endl;
// TotalCourses[10] Input & Validation
do
{
while(!(cin >> TotalCourses[10]))
{
cout << "Invalid Entry." << endl;
cout << "Please enter the number of courses:" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
if(TotalCourses[10] < 0 || TotalCourses[10] > 10)
{
cout << "Invalid Entry." << endl;
cout<< "Please enter the number of courses, between 0-10:" << endl;
}
}
while(TotalCourses[10] < 0 || TotalCourses[10] > 10);
cin.ignore(1,'\n');
// Get CourseInfo, CourseNumber, CourseGrade[10], CreditHour[10]
for ( count = 1; count <= TotalCourses[10]; count = count + 1 )
{
cout << "Enter the Name of Course " << count << endl;
cin.getline(CourseInfo, size);
cout << "Enter the Semester/Year Course " << count << " was Taken" << endl;
cin.getline(CourseTime, size);
cout << "Enter the Number of Course " << count << endl;
cin.getline(CourseNumber, size);
cout << "Enter the Credit Hours for Course " << count << endl;
// CreditHour[10] Input/Validation
// Help From:
// http://stackoverflow.com/questions/19521320/why-do-i-get-an-infinite-loop-if-i-enter-a-letter-rather-than-a-number
do
{
while(!(cin >> CreditHour[10]))
{
cout << "Invalid Entry." << endl;
cout << "Please enter the number of credit hours:" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
if(CreditHour[10] < 0)
{
cout << "Invalid Entry." << endl;
cout<< "Please enter the number of credit hours:" << endl;
}
}
while(CreditHour[10] < 0);
cin.ignore(1,'\n');
}
{
const string menu = "\nWelcome! GPA & Course Storage Menu"
"\nPlease enter the character next to the choice you wish to pick."
"\nHere are your options:"
"\nB(b). List All Courses"
"\nQ(q). Quit Program\n";
// display menu on screen
cout << menu;
// read user menu selection
cout << "Please Choose one of the Above\n";
cin >> choice;
// processing ---- FIX
while ( choice != 'Q' || 'q')
{
// figuring menu selection
if ( choice == 'B' || choice == 'b')
{
cout << "The following are your courses in input format:" << endl;
for ( count = 0; count < TotalCourses[10]; count = count + 1){
std::cout.write(CourseInfo,100)<<'\n';
std::cout.write(CourseTime,100)<<'\n';
std::cout.write(CourseNumber,100)<<'\n';
cout << CourseGrade[10] << endl;
cout << CreditHour[10] << endl;
}
}
else if ( choice == 'Q' || choice == 'q')
{
cout << "Terminating..." << endl;
exit(0);
}
else if (choice != 'A' || choice != 'a' || choice != 'B' || choice != 'b' || choice != 'C' || choice != 'c' || choice != 'D' || choice!= 'd' || choice != 'E' || choice != 'e' || choice != 'Q' || choice !='q')
{
cout << "\nInvalid Entry.\n";
cout << "Try again\n";
}
// Processing
cout << menu;
cout << "Please Choose one of the Above:\n";
cin >> choice;
}
}
return 0;
}
|
Any help you can provide (or any other resources you think might be useful) would be greatly appreciated! Thank you so much!