I need to write a program using arrays and strings.Here is the question.
A class of students takes a 20 question multiple-choice exam;each question has 5 choices(a,b,c,d,e)only one of them are correct.In "tests.dat"Each record of which consists of a student id, followed by a blank, followed by students 20 responses. In "answers.dat" which consists of a single record, namely the correct answers to the exam.Then It needs to produce a grade summary report like this.If the answer is correct it should put a "*" by the correct answer
student-id number correct
1231231212312 12
1233424325435 25
.... ...
question A B C D E
1 5 1 13* 3 1
2 4 7* 5 12 7
.
.
...
are you using spaces in variable names? (object name for input stream)
don't think ifstream fin ans,fin tests; will work, but I could be wrong. how about an underscore...
fin_ans fin_tests or camel case... finAns, & finTests
cout<<id<<correct<<<endl<<endl;
there's one too many "<" in there.
and that block of code is virtually unreadable, if you have problems using copy paste, simple do a quick edit on code please.
Personally I wouldn't be using that many arrays, possibly two, 1 to store answers, and 1 to read in correct results.
also I wouldn't be using abort() without the proper namespace. To add on that, abort is equal to exit(1); so you should use exit as it is more generic and useful.
cout<<count<<"A B C D E"<<
instead of adding so many spaces to output statements you can use escape character '\t'
which will put a tab between each letter, by default 8 spaces.
#include <iostream>
#include <cstdlib>
#include <fstream>
using std::cout;
using std::cin;
using std::endl;
using std::ifstream;
using std::exit;
int main()
{
ifstream finAns; // need to have correct variables names
ifstream finTests;
char key[21];
char id[21];
char response[21]; // indented properly
int count[20][5];
finAns >> key; // this isn't going to work because you have not declared the file
finTests >> id >> response; // same deal.
for(int i=0 ; i < 20 ; i++) // i was not initialized so you need to declare it
// also you were using commas instead of semi-colon
{
if(key[i] ==responses[i])
{
num correct++; // what is num correct ? you can't do this, variable names can't contain spaces
// and they need to be initialized eg: int numCorrect=0;
count[i][responses[i]-'A']++; // what is this statement trying to do ?
// cout << count[i][responses[i]-'A'] &&"*" << endl; dont use && operator to append stuff use <<.
cout << count[i][responses[i]-'A'] << "*" << endl;
} // <-- added close of 'if statement'
else ( key[i] != responses[i] )
{
count[i][responses[i]-'A']++;
cout << count[i][responses[i]-'A'] << endl;
}
}
// return 0; // <--- returned an integer for return type "int" main()
//} i'm guessing this is not the end of main, but because you added extra '}' it is
finAns.open("answers.dat");
// if(fin.fail("answers.dat")) // <---- this makes no sense what is fin.fail? needs to be the variabels name
if (! finAns.is_open())
{
cout << "Error opening stream" << endl;
// abort();
exit (1); // exit is sexier because it's more useful and generic.
}
finTests.open("tests.dat");
// if(fin.fail("tests.dat")) // same deal... as before
if (! finTest.is_open())
{
cout << "Error opening stream" << endl;
exit (1);
}
cout << id << correct << endl << endl;
// cout<<count<<"A B C D E"<<
// <<key<<count[i][responses[i]-'A']
// you are using two sets of '<<' here and the begining of next line
cout << count << "A\tB\tC\tD\tE" << key
<< count[i][responses[i]-'A'] << endl;
// not quite sure what this last output is supposed to be,
// there is a missing '[' which makes no sense, and also 2 array elements, however you not
// traversing an array anywhere so this will only ever be one value which the compiler may even
// complain that there variable is not initialized. no << endl; at end, and/or no flush()
return 0;
}
compile this, then write a bit more code and compile, write a bit more and compile. so on and so forth until you are done and the program is completely written you will find that your code works.
The easiest way to program even from early on is using pseudo-code. write down in bullet format what the program needs to do. Then you find the code can simply mimic english.
eg:
1. declare variables
2. open input files
3. check if file opening succeeded.
4. store input files into arrays.
5. compare results.
6 ... etc.
I dont know what i am suppose to do with this part
i know it is confusing
1 2 3 4 5 6
num correct ++;
count[i][responses[i]-'A']++;
cout<<count[i][responses[i]-'A'] &&"*"<<endl;
else(key[i] !=responses[i]){
count[i][responses[i]-'A']++;
cout<<count[i][responses[i]-'A']<<endl;
Explain what you are trying to do in english, then you might find you will be able to write the code, or if you are unsure of syntax then I can, or someone else can help you with it.
// Student Grading system
//int i is used as a local variable alot to keep track of stuff
#include <iostream> // Mandatory
#include <fstream> // File I/O
#include <string> // Mandatory for strings
void getInfo(); // Function getInfo
void findAvg(); // Function findAvg
void giveGrade(); // Function giveGrade
void output(); // Function output
usingnamespace std; //Standard Namespace
//Declarations
float avg = 0;
int ID[200];
string name[200];
float score[200];
string grade[200];
signedint pk=-1; //PK is the incrementer
ofstream file;
string filename;
//Main
int main(void)
{
//START OF PROGRAM
system("color 0a"); // Changes system text to green/0a
system("cls"); // Clears the screen
// Introduction
//Make it look good
cout << "-------------------------------------------" << endl;
cout << "|Welcome to my Student grading organizer!!|" << endl;
cout << "-------------------------------------------" << endl << endl;
// Notifies where file will be saved
cout << "This program will create a file called 'Grades.txt'" << endl;
cout << "in the ORIGINAL folder it was placed in." << endl;
system("pause"); // Give time to read
// Calls getInfo
getInfo ();
// Calls findAvg
findAvg ();
// Calls giveGrades
giveGrade();
// Calls output
output();
// This is for after output.. Notification of saving
cout << "Saving file 'Grades.Txt'" << endl;
// Give Time to Read
system("Pause"); // I know system calls are bad, but.. what the heck
//END OF PROGRAM
return 0;
}
void getInfo() //Used to Gather information
{
string more = "y"; //While More = True.... Loop
int i = 0; //To ensure Score is actually integer
while (more=="y")
{
pk++; //Incremente
ID[pk] = pk + 1; //ID Count = PK..
system("cls");
cin.sync(); // Clear buffer for each loop
cout << "What is the name of the student? "; //Get name
getline( cin, name[pk] ); //Put into name[pk]
cout << "What is the score of the student? "; //Get score
//Ensure that score is actually an integer
while (!(cin >> i))
{
cin.clear();
cin.ignore(1000,'\n');
//If not an integer, repeat until so
cout << "What is the score of the student (integer value's only): ";
}
score[pk] = i; //If so.. Put score in here
cout << "Would you like to add another student? ";
cin >> more; //Ask if they'd like to add another student
while (more != "y" && more != "n")
//While more is not y & not n
{
cout << "(y or n) only please: ";
cin >> more;
}
/*If it's n... it goes back to main.. to continue next function
Otherwise it will go back to the beginning of this function*/
}
}
void findAvg() //Used to calculate the average
{
for (int i=0; i<pk + 1; i++) /* As long as i is less than pk
score[1], score[2], and so on
will be added to avg*/
{
avg = avg + score[i];
}
avg = avg / (pk + 1); /*To find the average..
Take the total number of scores (currently in avg)
and divide it by pk + 1 because pk starts at -1*/
cout << "Average = " << avg << "%" << endl; /*Display avg and add %
so it looks professional*/
system("pause"); //Give user time to see average
}
void giveGrade()
{
/*Standards are as follows
A student within 5 points of the average gets a B
A student with more than 5 points above the average gets an A
A student with more than 5 and within 15 points below the average gets a C
A student with more than 15 points below the average gets a D
*/
for(int i = 0; i < pk + 1;i++) //While i is less than pk.. do this
{
if (score[i] >= avg + 5) //Standards for an A
{ //Greater than or = to the average + 5
grade[i] = "A";
}
elseif (score[i] <= avg + 4 && score[i] >= avg - 4)
{ //Less than or = to +4 & greater than or = to -4
grade[i] = "B"; //Standards for a B
}
elseif (score[i] <= avg -5 && score[i] >= avg - 14)
{
grade[i] = "C"; //Standards for a C
}
elseif (score[i] < avg - 15)
{
grade[i] = "D"; //If none of the above apply.. Give D
}
}
}
void output()
{
//ofstream file declared above
//Open a file called Grades.txt
//If non-existant it will be created
file.open("Grades.txt");
//Display average in text file
file << "The Average is: " << avg << "%" << endl << endl;
//Local Variable i is used again
//while i is less than pk put the array into the textfile
//For example
/*
ID[1] Produces 1
name[1] Produces Name that was put first
score[1] Produces score that was put in first
grade[1] Produces first grade calculated
<< ends the line
the "" are to make it look neater
*/
for(int i = 0; i < pk + 1;i++)
{
file << ID[i] << ", " << name[i] << ", " << score[i] << "%, " << grade[i] << endl;
cout << ID[i] << ", " << name[i] << ", " << score[i] << "%, " << grade[i] << endl;
}
//Save and close the file.. Go Back to main
file.close();
}
legend do i need to use void??
I did something like this C and C++ mix.I get the first part of the question right but not the second part.Do you have any idea I should do?