Hello WakelessFoil,
Looking over your code I noticed the are some big parts the need worked on and some little things.
As a start:
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
|
#include <iostream>
#include <string> // <--- Needed for "std::string" and "getline()" along with some of the "cout" statements.
#include <fstream>
using namespace std;
int main()
{
constexpr int MAXSIZE{ 21 };
const int number_of_questions = 20; // <--- Could just use MAXSIZE.
//declare variables
string D1name, D2name, D3name;
string passfail;
char corans[MAXSIZE] = { '-', 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A' };
char D1ans[MAXSIZE]{'-'};
char D2ans[MAXSIZE]{ '-' };
char D3ans[MAXSIZE]{ '-' };
int D1score{}, D2score{}, D3score{};
//initialize scores to 0 // <--- Not needed if you initialize the variables when defined.
//D1score = 0;
//D2score = 0;
//D3score = 0;
//declare and open input files
fstream inFile; //declare input files
//program title
cout << "-----------------------------------------\n";
cout << "Driver's License Exam Grading Application\n";
cout << "-----------------------------------------\n\n";
//table
cout << "Name Answers Correct Incorrect Pass/Fail" << endl;
cout << "---------------------------------------------------------------------------------------------" << endl;
///load answers in from file
///extra input values take up the return character between answers
inFile.open("driver1.txt");
getline(inFile, D1name);
|
You are good to this point with what I have noted in the code.
You have defined "std::string"s and used "getline()" both of which need the header file <string> to work properly.
The
string D1name, D2name, D3name;
may work because of a header that is included that you do not know about, but this does not mean that it will work properly.
Lines 19 - 21 are initialized with the first element being "-" and all the other elements are set to "\0". This eliminates the garbage that the array would initially hold. Also the "\0" can be used later in the program.
I do have a question. If correct answers only has 20 letters and the file only has 20 lines to read, What is the extra space for? Your arrays only need to hold 21 user answers.
Line 22 is OK, but you could use an array especially if the number of files increases in the future.
After this
lastchance's suggestions of using a "std::string is good. The string can be accessed just like your "char" arrays with less work and more advantages.
Next would lines 42 - 81 in your last code. This cries for a for loop. Which you could do in as little as 2 lines with a for loop. Even a while loop could be done in 2 lines.
All the if statements after the first 1 are wrong. With your arrays starting at 1 instead of (0) zero, which is fine, the first if statement makes the correct comparison. All the rest are off because the correct answers array and what is read from the file are set up to match. Comparing
if (D1ans[3] == corans[2])
is more likely not to match and evaluate to "false" than to be "true".
And in the next if statement
if (D1ans[5] == corans[3])
. Now you are off by 2.
These if/else statements scream for a for loop.
Also the else part is not needed because in the last output statement you figure the incorrect answers based on the correct amount.
That alone would shorten the if statements. Also
D1score = D1score + 0;
really does not do anything and the other way
D1score = D1score + 1;
can easily be written as
D1score++;
. It still just adds 1 without all the extra work. Using the "++" or "--" as a prefix or suffix the more preferred way of using this shorthand as is the others "+=", "-=", "*=" and "/=". These are the most used although there are others.
In addition to
lastchance's link This is a good one
https://www.learncpp.com/cpp-tutorial/57-for-statements/
Some where around line 268 the "cout" prints all the odd indices of your array and includes elements that should not be printed because they were never used and you may or may not know what they contain.
You could print the name followed by a short for loop to print the users answers the whatever is left.
Andy