Welcome! Log In Create A New Profile

Advanced

Assignment 1, Semester 2, Q3

Posted by P00HB33R 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Assignment 1, Semester 2, Q3
July 15, 2009 05:32PM
Hi Guys,

Can anyone please help me with question 3, semester 2, Assignment 1. I can do all the rest but for some reason I cant get the darn thing to work.
Here is my code so far:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
int totalMarks, count;
string studentAnswer, studentID, studentName, answers;
char answer, studentAnswers;

ofstream report_stream;
ifstream answer_stream;
ifstream booklet_stream;

answer_stream.open("answer.dat"winking smiley;
report_stream.open("report.dat"winking smiley;
booklet_stream.open("booklet.dat"winking smiley;

while (! answer_stream.eof())
{
booklet_stream >> answers;
answer_stream >> studentAnswer >> studentID >> studentName;
}
}

I cant seem to grasp how to compare character for character from the answer_stream and the booklet_stream!
Plzzzzzzzz guys I really need to get this thing working.

Thanks
Re: Assignment 1, Semester 2, Q3
July 16, 2009 08:43AM
you can reference specific characters in a string, just like you would in a character array, and you compare characters with ==
Re: Assignment 1, Semester 2, Q3
July 16, 2009 12:45PM
Cool thanx.

But how do I reference a specific character?
Re: Assignment 1, Semester 2, Q3
July 16, 2009 01:09PM
have you used arrays before?
Re: Assignment 1, Semester 2, Q3
July 21, 2009 12:45PM
Hi,

Having problems with this one too.

Can one declare an array of 50 entries and then use a for statement to pull each character from the booklet.dat file to process and compare the marks?

Any help plz.
Re: Assignment 1, Semester 2, Q3
July 23, 2009 12:33PM
Hi All,

I am having the same problem. But i have thought of using strcmp to compare the student results with the correct results.

char correct_results[]={'A' 'B' 'C'.....}
char answer[50]

int j =0;
while (strcmp(correct_results[j],answer[j])
{
????????
????????
}

Anyone with different idea please assisteye rolling smiley
avatar Re: Assignment 1, Semester 2, Q3
July 23, 2009 01:15PM
Nice idea tshax. See if you can work out this one:

Language: C++
char correct_results[] = "ABCD"; char answer[50];   int j = 0; while (strcmp(correct_results, answer) != 0){ // ?????? }
Re: Assignment 1, Semester 2, Q3
July 24, 2009 12:53PM
From the information above, my undertanding is that i should declare answers for "booklet.dat" and compare them with student answers from file "answer.dat".

char memo_results[]="ACBAADDBCDDAACDBACCABDCABCCBDDABCACABABABCBDBAABD";
char student_ans[50];
int i = 0;

ans_booklet>> student_ans;

while (strcmp(memo_results,student_ans) !=0)
{
if (student_ans == ' 'winking smiley;
report_booklet<< "*";
i++
}
report_booklet<<student_num<<student_name<<i;

What confuses me is how to create a file containing correct answers and student answers,
Also how to assign declared variables to the files.confused smiley
Re: Assignment 1, Semester 2, Q3
July 27, 2009 10:34AM
i dont think that u need an array for the answers coz you already have a file that have the answers, all you have to do is use the file eg:

char answers = ' ', studentAnswer = ' ';
//get the answer and the students answer from the files, then
if (answers == studentAnswer)
markCounter++;
//repeat the process above up until the data of the first student is processed



last more thing, u gona need 2 counters or one, it depends on the how u know if its the end of the students answer

--------------------------------------------------------------------
In a long journey, even a matchstick is heavy...
Re: Assignment 1, Semester 2, Q3
July 29, 2009 10:10AM
Okay, I get your point but now how do i get the records printed in the report.data...check my code below...confused smiley

void stud_marks(ifstream& memo, ifstream& ans, ofstream& report)
{
int i =1;

while(!ans.eof()&& !memo.eof())
{
if (memo == ans)
i++;
}
}
Re: Assignment 1, Semester 2, Q3
July 30, 2009 01:19PM
void stud_marks(ifstream& memo, ifstream& ans, ofstream& report)
{
int i =1;// correct answer counter

while(!ans.eof()&& !memo.eof())
{
if (memo == ans)
i++;
//another if statement is needed here to check wether were done with one student
}
}

your going to have to declare a string to store the student name and an integer to store the student ID,e.g
report << studID<< " " << studName << " " << i << endl;
as I have said you going to need another counter so that you know when the answers of one student are done, and then reset both counters to deal with the second students data.

--------------------------------------------------------------------
In a long journey, even a matchstick is heavy...
Re: Assignment 1, Semester 2, Q3
July 30, 2009 04:49PM
Hi
i've been struggling with the question myself. my program can only compare and output the answers for only one student but doesnot output the student ID and student name . i'm trying to get the student id and student name but i don't know how to and even getting the next student. i was trying to store the student id and studentname as a substring but the i get the follwing error: error C:\unisa\devcpp\Ass1Q3try2.cpp:63
'struct std::ifstream' has no member named 'substr'


stud_ID = answer_stream.substr(52,55);
stud_Name = answer_stream.substr(57);

i really need help.
Re: Assignment 1, Semester 2, Q3
July 30, 2009 04:51PM
the below is the coding of substring
stud_ID = answer_stream.substr(52,55);
stud_Name = answer_stream.substr(57);
Re: Assignment 1, Semester 2, Q3
July 31, 2009 10:50AM
O right... I am out of ideas if this code doesn't work ...show me the direction....comrade coders!!!yawning smiley

void stud_marks(ifstream& memo, ifstream& ans, ofstream& report)
{
int i =1;// correct answer counter
char next;
String StudName;
int StudID;
while(!ans.eof()&& !memo.eof())
{

while (ans>>next) //another if statement is needed here to check wether were done with one student
{
if (memo==ans)
i++;
}
report<<StudName<<" "<<StudID<<" "<<i<<endl;
}
Re: Assignment 1, Semester 2, Q3
July 31, 2009 04:23PM
i don't know how you going to do that because i'm stuck with it, but how will the program know which is the student name and student ID? you just declared String StudName; int StudID; and told the program to ouput it but not telling it how to know if that is the right info you looking (how to extract the studentID and studentname). i'm also stuck with that
avatar Re: Assignment 1, Semester 2, Q3
August 02, 2009 11:26AM
@mhld and @tshax

You're confusing the std::ifstream and std::string classes. They are two distinct classes and don't have the same semantics. They are related in the sence that they contain arrays of characters but they are treated differently.

@mhld, the substr method is from the std::string class so you shouldn't use it on the std:ifstream.

If you want to compare answers, you should extract the strings from the streams and then compare the strings themselves. If you wish to compare single characters at a time, you should extract a single character at a time from the stream and compare those.

What I would suggest though is that you seem to have to compare lot of different students' marks with one set of answers is the following algorithm:

Open answer file.
Extract all the answers into an array.
close answer file.

for each student
   open student answers
   for each student answer
      compare each answer with the answer array and count those correct.
   close student file.
Re: Assignment 1, Semester 2, Q3
August 03, 2009 11:25AM
Is that mean i should use for_loop and arrays to compare the answers?
Re: Assignment 1, Semester 2, Q3
August 04, 2009 02:58PM
i can see that every one is strugling with this question, so if no one minds can i just post the code? il do so tomorow if no one minds...

--------------------------------------------------------------------
In a long journey, even a matchstick is heavy...
Re: Assignment 1, Semester 2, Q3
August 04, 2009 02:59PM
@tshax u dont need a for loop jst one while loop and thats it, it will come clear tomorow...

--------------------------------------------------------------------
In a long journey, even a matchstick is heavy...
Re: Assignment 1, Semester 2, Q3
August 04, 2009 03:02PM
@robanaurochs when you extract the answers into an array i gues you gona have to do so for every other student which is going to take a lot of time and coding

--------------------------------------------------------------------
In a long journey, even a matchstick is heavy...
avatar Re: Assignment 1, Semester 2, Q3
August 04, 2009 04:10PM
@crazyj

my pseudocode is only meant to be interpreted as reading the model answers into an array. Thereafter, when the program needs to access the answers, they just get them from the array. Reading from RAM is far quicker than reading from the disk so reading the model answers once for every student is going to be far quicker from memory than from a file.

The student answers can be read in directly from a file however because they're only need once. To read them into an array and then compare that array to the model answers is wasteful.
Re: Assignment 1, Semester 2, Q3
August 05, 2009 02:37PM
//here it is, hope you guys can test it out and change wherever you feel needs to be changed. and please use your own variable naming
//@robanaurochs i agree with you man, the was a need for an array i only noticed it yesterday when i did it

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
ifstream fin1;
ifstream fin2;
ofstream fout;

fin1.open("booklet.dat"winking smiley;
if(fin1.fail())
{
cout << "Input file1 opening failed.\n";
exit(1);
}
fin2.open("answer.dat"winking smiley;
if(fin1.fail())
{
cout << "Input file2 opening failed.\n";
exit(1);
}
fout.open("studentAnswer.dat"winking smiley;
if(fin1.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
char studAnswer;
string name;
int count = 1, answerCounter = 0, studCard;
const int ARRAY_SIZE = 50;
char arrayAnswer[ARRAY_SIZE];

for(int i = 0; i < ARRAY_SIZE; i++)
fin2 >> arrayAnswer;
fin2.close();
while(!fin1.eof())
{
fin1 >> studAnswer;
if(studAnswer == arrayAnswer[count - 1])
answerCounter++;
if(count == ARRAY_SIZE)
{
fin1 >> studCard >> name;
fout << studCard << " " << name << " " << answerCounter << endl;
answerCounter = 0;
count = 0;
}
count++;
}
fin1.close();
fout.close();
return 0;
}

and last more thing, you can split it and turn it into funtions.
feel free to ask questions
NB:this was a rough work, so that means there more than one way of doing this

--------------------------------------------------------------------
In a long journey, even a matchstick is heavy...
avatar Re: Assignment 1, Semester 2, Q3
August 05, 2009 07:55PM
You guys should start using the coding tags before posting, otherwise you're going to lose your indents and get strange effects (see the smileys and italics above).

Above the message box are formatting buttons (bold, italics, underline, ....). The fourth one from the right (a square with squiggly lines) is the code formatting one. Just highlight the code you want formatted, click on the formating button and choose the type of code. Most of the time you'll use C++ so just select that.

If I take Crazy J's code and do just that, it comes up like so (I've had to be a bit inventive with the indents because they were removed):

Language: C++
#include <iostream> #include <fstream> #include <string> using namespace std;   int main(){ ifstream fin1; ifstream fin2; ofstream fout;   fin1.open("booklet.dat"); if(fin1.fail()){ cout << "Input file1 opening failed.\n"; exit(1); } fin2.open("answer.dat"); if(fin1.fail()){ cout << "Input file2 opening failed.\n"; exit(1); } fout.open("studentAnswer.dat"; if(fin1.fail()){ cout << "Input file opening failed.\n"; exit(1); } char studAnswer; string name; int count = 1, answerCounter = 0, studCard; const int ARRAY_SIZE = 50; char arrayAnswer[ARRAY_SIZE];   for(int i = 0; i < ARRAY_SIZE; i++) fin2 >> arrayAnswer[i]; fin2.close(); while(!fin1.eof()){ fin1 >> studAnswer; if(studAnswer == arrayAnswer[count - 1]) answerCounter++; if(count == ARRAY_SIZE){ fin1 >> studCard >> name; fout << studCard << " " << name << " " << answerCounter << endl; answerCounter = 0; count = 0; } count++; } fin1.close(); fout.close(); return 0; }
avatar Re: Assignment 1, Semester 2, Q3
August 05, 2009 09:11PM
@Crazy J: Just a few comments that you might find useful.

In the lower level programming courses you do get marks deducted if your variables are cryptically named or you don't use comments. The file stream variables you've used don't say much about what they represent. I suggest the following:

fin1 -> studentAnswerFile
fin2 -> modelAnswerFile
fout1 -> studentResultsFile

I would also say that arrayAnswer is a bit ambiguous, it could mean the student's answers so it might be better to call it arrayModelAnswers. Don't be scared of long variable names (but then again rethink the naming if you start going over about 30 characters).

Another thing, refrain from using exit(). I've mentioned it before in previous years. You can't guarantee that your stack will be cleaned properly or all your memory will be released. You're gambling that your compiler knows what it's doing. Rather just use return -1 instead.

Last thing - and this is just stylistic - is the concept of variable lifespan. This is the number of lines between the time that you define a variable and the last time you use it. Studies have shown that code is easier to debug, and also that fewer bugs are made, if the lifespan of variables is reduced to a minimum.

For example, fin2 in your code, has a lifespan of 24 lines but can be reduced to 8 lines just by moving the definition to just above the for loop that reads in the model answers.

If I do a few changes(without changing your algorithm), your code becomes:
Language: C++
#include <iostream> #include <fstream> #include <string> using namespace std;   int main(){   // read in all the model answers const int ARRAY_SIZE = 50; char arrayModelAnswers[ARRAY_SIZE]; ifstream modelAnswerFile; modelAnswerFile.open("answer.dat"); if(modelAnswerFile.fail()){ cout << "Failed to open the model answer file.\n"; return -1; // error opening file, abort program } for(int i = 0; i < ARRAY_SIZE; i++) modelAnswerFile >> arrayModelAnswers[i]; modelAnswerFile.close();   ifstream studentAnswerFile; studentAnswerFile.open("booklet.dat"); if(studentAnswerFile.fail()){ cout << "Failed to open the student answer file.\n"; return -1; // error opening file, abort program }   ofstream studentResultsFile; studentResultsFile.open("studentAnswer.dat"); if(studentResultsFile.fail()){ cout << "Failed to open the results file.\n"; return -1; // error opening file, abort program }   // Compare the student';s answers to the model int count = 0, answerCounter = 0; while(!studentAnswerFile.eof()){ char studAnswer; studentAnswerFile >> studAnswer; if(studAnswer == arrayAnswer[count++]) answerCounter++; if(count == ARRAY_SIZE){ // all answers have been read. store the results string name; int studCard; studentAnswerFile >> studCard >> name; studentResultsFile << studCard << " " << name << " " << answerCounter << endl; answerCounter = 0; count = 0; } } studentAnswerFile.close(); studentResultsFile.close(); return 0; }
Re: Assignment 1, Semester 2, Q3
August 07, 2009 01:36PM
thanx alot robanaurochs point taken, aspecially when talking about the variable lifespan

--------------------------------------------------------------------
In a long journey, even a matchstick is heavy...
Re: Assignment 1, Semester 2, Q3
August 21, 2009 09:33AM
Hi All

I've tried the following can anyone help me?

// Assignment 1
// Question 3
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

const int numQuestion = 50;

// to calculate mark
void markTotal ( char stud_answP[], char answP[], int & totalP)
{
for (int i = 0; i < numQuestion; i++)
{

if ( answP == stud_answP)
{
totalP ++;
}

}
}

int main()
{


int total = 0;
char stud_answ [numQuestion];
char answ[numQuestion];
int stud_nr;
string name;

ifstream fromFile, fromFile2;
ofstream toFile;

fromFile.open("booklet.dat"winking smiley;
if (fromFile.fail())
{
cout <<"Input file opening failed." << endl;
exit (1);
}

fromFile2.open("answer.dat"winking smiley;
if (fromFile2.fail())
{
cout <<"Input file opening failed." << endl;
exit (1);
}

toFile.open( "total.dat", ios::app);
if (toFile.fail())
{
cout <<"Input file opening failed." << endl;
exit (1);
}
cout <<"Files opened successful." << endl;
cout << endl;

for (int i = 0; i < numQuestion; i++)
{

fromFile >> answ;
}


cout <<"Data transfered." << endl;

cout <<"Data Printed to file: " << endl;
cout <<"================================================" << endl;
while (!fromFile2.eof())
{
for (int i = 0; i < numQuestion; i++)
{
fromFile2 >> stud_answ;
}

fromFile2 >> stud_nr >> name;

markTotal (stud_answ, answ, total);

// Output
toFile << total << " " << stud_nr << " " << name << endl;
cout << total << " " << stud_nr << " " << name << endl;


}
cout <<"================================================" << endl;
cout << endl;





cout <<"Total outputed to file." << endl;

fromFile.close();
fromFile2.close();
toFile.close();
cout <<"File Created." << endl;
return 0;
}
Re: Assignment 1, Semester 2, Q3
August 21, 2009 03:26PM
When posting code, it is very helpful to put is between code tags. It makes it easier to read and gets rid of the smiley faces. I will do that with your code and then hopefully someone will be able to help. I will try to do my best and let you know how I solved the problem. Hope it helps.

Language: C++
// Assignment 1 // Question 3 #include <fstream> #include <iostream> #include <cstdlib> #include <string>   using namespace std;   const int numQuestion = 50;   // to calculate mark void markTotal ( char stud_answP[], char answP[], int & totalP) { for (int i = 0; i < numQuestion; i++) {   if ( answP == stud_answP) { totalP ++; }   } }   int main() {     int total = 0; char stud_answ [numQuestion]; char answ[numQuestion]; int stud_nr; string name;   ifstream fromFile, fromFile2; ofstream toFile;   fromFile.open("booklet.dat"winking smiley; if (fromFile.fail()) { cout <<"Input file opening failed." << endl; exit (1); }   fromFile2.open("answer.dat"winking smiley; if (fromFile2.fail()) { cout <<"Input file opening failed." << endl; exit (1); }   toFile.open( "total.dat", ios::app); if (toFile.fail()) { cout <<"Input file opening failed." << endl; exit (1); } cout <<"Files opened successful." << endl; cout << endl;   for (int i = 0; i < numQuestion; i++) {   fromFile >> answ; }     cout <<"Data transfered." << endl;   cout <<"Data Printed to file: " << endl; cout <<"================================================" << endl; while (!fromFile2.eof()) { for (int i = 0; i < numQuestion; i++) { fromFile2 >> stud_answ; }   fromFile2 >> stud_nr >> name;   markTotal (stud_answ, answ, total);   // Output toFile << total << " " << stud_nr << " " << name << endl; cout << total << " " << stud_nr << " " << name << endl;     } cout <<"================================================" << endl; cout << endl;           cout <<"Total outputed to file." << endl;   fromFile.close(); fromFile2.close(); toFile.close(); cout <<"File Created." << endl; return 0; }

Chris Botha

=============================
Far and away the best prize that life has to offer is the chance to work hard at work worth doing.
Theodore Roosevelt
Re: Assignment 1, Semester 2, Q3
August 21, 2009 04:44PM
Ok, I have checked your code and made some minor adjustments. Basically adding "i" at your for loops. I have made comments.
Also I removed the append parameter from your totals file, otherwise every time you test, it adds the students to the bottom of the file.
The output is now correct but for one more mistake that for the life of me I cannot find the problem.

The final student's record is printed twice. It must be something with your eof loop, but what exactly, I don't know yet. Will look again and if I find the problem I will post again.

here is the code

Language: C++
// Assignment 1 // Question 3 #include <fstream> #include <iostream> #include <string>   using namespace std;   const int numQuestion = 50;   // to calculate mark void markTotal ( char stud_answP[], char answP[], int & totalP) { totalP = 0; for (int i = 0; i < numQuestion; i++) { if ( answP[i] == stud_answP[i]) //Added i counter variable to move along the arrays totalP ++; } } //------------------------------------------------------   int main() { int total = 0; char stud_answ [numQuestion]; char answ[numQuestion]; int stud_nr; string name;   ifstream fromFile, fromFile2; ofstream toFile;   fromFile.open("booklet.dat"); if (fromFile.fail()) { cout <<"Input file opening failed." << endl; exit (1); }   fromFile2.open("answer.dat"); if (fromFile2.fail()) { cout <<"Input file opening failed." << endl; exit (1); }   toFile.open( "total.dat"); //removed the ios append parameter if (toFile.fail()) { cout <<"Input file opening failed." << endl; exit (1); }   cout <<"Files opened successful." << endl; cout << endl; //---------------------------------------------------------   for (int i = 0; i < numQuestion; i++) fromFile >> answ[i]; //Added i counter variable to move along the arrays     cout <<"Data transfered." << endl;   cout <<"Data Printed to file: " << endl; //This lne is not actually true yet so should be moved cout <<"================================================" << endl;   while (!fromFile2.eof()) { for (int i = 0; i < numQuestion; i++) { fromFile2 >> stud_answ[i]; //Added i counter variable to move along the arrays }   fromFile2 >> stud_nr >> name;   markTotal (stud_answ, answ, total);   // Output toFile << total << " " << stud_nr << " " << name << endl; cout << total << " " << stud_nr << " " << name << endl;     } cout <<"================================================" << endl; cout << endl;   cout <<"Total outputed to file." << endl;   fromFile.close(); fromFile2.close(); toFile.close(); cout <<"File Created." << endl; return 0; }

Hope this helps in the meantime.

Chris Botha

=============================
Far and away the best prize that life has to offer is the chance to work hard at work worth doing.
Theodore Roosevelt
Re: Assignment 1, Semester 2, Q3
August 21, 2009 05:15PM
Sorry for the code lay out, just copied and paste. The i was in the original code.

Thanx for having a look
Re: Assignment 1, Semester 2, Q3
December 04, 2009 03:26PM
Hey All,

I would like to find out if there is someone that is going to sell COS112V textbook?

Please send me an email unisa18@gmail.com if you interested in selling the textbook.

Regards,
Sorry, only registered users may post in this forum.

Click here to login