Using "sizeof" here seems problematic. I don't know where you are declaring answer, but remember that sizeof is not the number of elements in an array, but instead is the amount of storage required. I'd have to see how/where you declare everything, but I'm guessing you don't want "sizeof". A regular array size variable might be better:
const int NUM_QUESTIONS = 10;
int key[NUM_QUESTIONS];
int answer[NUM_QUESTIONS];
// initialize whatever you need to initialize and read in data
for(int i = 0; i < NUM_QUESTIONS; i++)
{
if(answer[i] == key[i] && answer[i] != ' ')
correct++
}
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Code tags. Please repost the code with them. It's really hard to read without them.
[code]
// code here
[/code]
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
>>inQuiz.seekg(0);//set position to begining of file
Delete that line. When the file is opened the file pointer will already be set to the beginning of the file.
>>inQuiz.seekg(1 - 1, ios::cur);
Why? what purpose does that serve?
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
You don't need to know whether answer is 'T' or 'F'. Just test if( answer[i] == key[i]) You can then delete one of those two loops.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
if(!inQuiz)//if file did not open correctly
{
cerr <<"File could not be opened." <<endl;
exit(1);
}
There is no reason to call a function to exit from main() . Just return 0;
inQuiz.seekg(0);//set position to begining of file
When you open a file you are at the beginning of the file. No need to seek.
inQuiz.seekg(1 - 1, ios::cur);
inQuiz.getline(answer, sizeof(answer));
inQuiz.seekg(1 - (sizeof(answer)), ios::cur);
Again, why do you have to seek? You read the answers and the name is read next. It's automatic. Seeking is only necessary when moving non-linearly through a file. You are making file reading way too complicated.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
I havent put the other code in for the other loops but i need to be able to count the amount that were correct wrong or missing
Well then just do it in one loop. Try to use as few loops as possible because they are very time expensive.
if( answer[i] == ' ')
missing++;
else if( answer[i] == key[i] )
correct++;
else
wrong++;
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Line 61:
average = (correct / 15) * 100;
If correct is less than 15, you are going to get 0. Integer division truncates. http://en.wikipedia.org/wiki/Division_%28mathematics%29#Division_of_integers
See point 4:
Give the integer quotient as the answer, so 26 / 10 = 2. This is sometimes called integer division.
Try multiplying, THEN dividing.
average = (correct * 100) / 15;
Regarding sizeof, it turns out here since you are dealing with characters and since characters take one byte, I think you were OK. But if it's any other type (i.e. integer) that doesn't take a byte per element, you are going to have potential problems.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Time to do some debugging. You need to find out exactly what you are reading in. Look at your file and decide how many times you are supposed to go through the while loop.
Comment out lines 51 through 59. Compile and run the program again? Do you still get the error?
Now add these lines at the very top of your while loop:
cout << "Top of while loop\n";
cout << answer << "\t" << fstNm << "\n";
cout << "Press Enter to continue\n";
std::cin.get ();
Make sure that it displays what you think it is supposed to display. See exactly how far you get. Find out exactly when it stops working by sprinkling some more cout statements and pauses in there. You'll delete these later. Also, now might be a good time to learn how to use a debugger too. It's faster than sprinkling all of these cout statements in there, plus you don't have to remember to take them out.
You need to find out exactly where and when this error occurs.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
use getline() instead of >> so that the entire line is read into a single variable.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
cin get() and even getline() all delimit at the whitespace when it comes to the gap on the answer
getline doesn't treat a space as a delimiter (unless you specifically tell it to). It delimits at '\n'. It "gets" a line, spaces and all (hence "getline"). Post your code.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
At this moment I need to figure out how to read in a char array including the white spaces cause cin get() and even getline() all delimit at the whitespace when it comes to the gap on the answer
what?? I think you may be confused. getline() ignores spaces and stops reading then either '\n' is encountered or the input buffer fills up (assuming you are using character array).is it possible to use a cast??
No.maybe to take it in like a string but store in a char array cause the ifstream wont like me input like a string
This might be a lot easier if you would use std::string instead of character arrays
std::string answers;
std::string key;
std::string line;
ifstream in("datafile.txt");
getline(in, key);
while( getline(in, line) )
{
answers = line.substr(0,15);
name = line.substr(15);
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343