whitespaces are killing me...

I've been working on this all night and for some reason I can't seem to get the output to come out right. here is my code:



#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;

const int maxAnswers = 20;
const int NUMBER_OF_STUDENTS = 4;

char getGrade(int percent);




int main()
{
char answers[maxAnswers];

char stuResponse[maxAnswers];
char studentAnswers[NUMBER_OF_STUDENTS][maxAnswers];
string stuName;
int i;

ifstream inFile;
inFile.open"grades.txt");
//ofstream outFile;
//outFile.open"output.txt");

// Store the answers in a variable
for (i = 0; i < maxAnswers; i++)
{
inFile.get(answers[i]);
cout << answers[i];
}

while (!inFile.eof())
{
int score = 0;
double possiblePoints = 40.0;
double percent = 0.0;

// Pull the students name or id number from start of line
inFile >> stuName;

// Pull the space from start of answers
inFile.get();

// Pull the students answers
for (i = 0; i < maxAnswers + 1; i++)
{
inFile.get(stuResponse[i]);

}


// Check students choices with the answers and adjust score accordingly

for (i = 0; i < maxAnswers; i++)
{

if (answers[i] == stuResponse[i])
{
score = score + 2;
}
else if (answers[i] = ' ')
{
score = score;
}
else if (answers[i] != stuResponse[i])
{
score = score - 1;
}
}

// Output the student's ID, followed by the answers,
// followed by test score, followed by test grade
cout << "Student: " << stuName << endl;
cout << "Answers: ";
for (i = 0; i < maxAnswers; i++)
{
cout << stuResponse[i];
}
cout << endl;
cout << "Answers Sheet: ";
for (i = 0; i < maxAnswers; i++)
{
cout << answers[i];
}
cout << endl << "Test Score: " << score << "/40" << endl;
percent = (score / possiblePoints) * 100;
cout << "Percent: " << percent << "%" << endl;
cout << "Grade: " << getGrade(percent) << endl;
cout << endl;
}

inFile.close();
//outFile.close();

return 0;
}


char getGrade(int percent)
{
if (percent >= 90)
return 'A';
else if (percent >= 80)
return 'B';
else if (percent >= 70)
return 'C';
else if (percent >= 60)
return 'D';
else
return 'F';
}





Results print out looks like this:


student: ABC54302
Answers: FTFFFFTFFTFFTFTF
answer sheet: FTFFFFTFFTFFTFTF
test score: 40/40

student: ABC54304
Answers: FTFFFFTFFTFFTFTF
answer sheet: .............FTFFFFTFFTFFTFTF

. = spaces (the website appears to know how to handle them whitespaces.......)


For some reason in the code it's creating white spaces which of course causes issues on the grading said of things...

Anyone got any ideas on what I'm doing wrong, or if I just need to start over and if so any pointers.. ARRAYS I really don't understand yet...


Last edited on
Link file you test your program with.
Also this is a certain problem: } else if (answers[i] = ' ') {. Honestly, your compiler should issue a warning in this place.
I don't understand how that is an issue if there is a whitespace there it means that the student didn't answer the question. BUT YOU are correct and I guess it really doesn't matter if it counts it as it doesn't alter the grade at all they just receive no credit for the question.

Thank you MiiNiPaa!!







Last edited on
Well, What the difference between a = b and a == b? When you will use one or other?
well a = b is setting the value a to b and a == b is checking to see if a is in fact the same.

so bad example would be if I was adding the b to a, a being the total sum of score

I would use the a == b in an if statement stating.

if a == b
than add two points

else if a != b
subtract one

sooo in code the space is just simply discarded as it doesn't fit the conditions.

Am I getting close?

I can't thank you again for saving my sanity..........
Here you did not check if answer is space. } else if (answers[i] = ' ') {
You assigned space to annswer replacing it. So this is where spaces come from.
lol I guess this is why this worked :)

1
2
3
4
5
6
7
8
9
10
11
12
		for (i = 0; i < questions; i++)
		{

			if (answersSheet[i] == studentAnswers[i])
			{
				score = score + 2;
			}
			else if (answersSheet[i] != studentAnswers[i] && studentAnswers[i] != ' ')
			{
				score = score - 1;
			}
		}
Last edited on
I can't believe I missed that all this time... missing one = :(
<picks up bat... takes a deep breath.... then repeatedly hit's head with bat>


thanks again!!
Topic archived. No new replies allowed.