Hey Guys need Help creating three functions setCorrectAnswers, inputAnswers, correctNumbers

closed account (9G8MoG1T)
This is what I got: Not sure if it is correct. I am sure it is the void setCorrectAnswers is where I am messing up

#include <iostream>
#include <iomanip>

using namespace std;
void setCorrectAnswers(char correctAnswers[]);
void inputAnswers(char givenAnswers[]);
int numCorrect(char correctAnswers[], char givenAnswers[]);
int main()
{
char correctAnswers[10];
setCorrectAnswers(correctAnswers);
char givenAnswers[10];
inputAnswers(givenAnswers);
int numRight = numCorrect(correctAnswers, givenAnswers);
double grade = numRight / 10.0;
setprecision(2);
cout << "Your quiz grade is " << (grade * 100) << "%" << endl;
system("pause");
return 0;
}

void setCorrectAnswers(char correctAnswers[])
{
{
'B', 'C', 'A', 'D', 'B', 'A', 'D', 'C', 'A', 'B';
}

}

void inputAnswers(char givenAnswers[]){
int i;
for (i = 0; i<10; i++){
cout << "Please enter your answer for question #" << (i + 1) << ": ";
cin >> givenAnswers[i];
}
}

int numCorrect(char correctAnswers[], char givenAnswers[]){
int numRight = 0;
int i;
for (i = 0; i<10; i++){
if (correctAnswers[i] == givenAnswers[i]){
numRight++;
}
}
return numRight;
}


This is the output--Quiz grade is 0%
even after entering correct answers
Last edited on
Well,
1
2
3
4
5
6
void setCorrectAnswers(char correctAnswers[])
{
  {
    'B', 'C', 'A', 'D', 'B', 'A', 'D', 'C', 'A', 'B';
  }
}

Does absolutely nothing.
It's the same as doing...

1
2
3
4
5
void f()
{
   42; // I'm just a number, or in your case, a character.
        // I'm not doing anything!
}

Do you see why that isn't doing anything? It isn't assigning anything, there's no =, nothing is being set.

For an array, you have to set each element individually except if you're initializing an array (which you aren't if you're passing an already-existing array into a function).
1
2
3
4
5
6
7
void setCorrectAnswers(char correctAnswers[])
{
    correctAnswers[0] = 'B';
    correctAnswers[1] = 'C';
    // ... fill the rest in 2-8
    correctAnswers[9] = 'B';
}


A less tedious alternative would be:
1
2
3
4
5
6
7
8
void setCorrectAnswers(char correctAnswers[])
{
    char answers[10] = {'B', 'C', 'A', 'D', 'B', 'A', 'D', 'C', 'A', 'B'};
    for (int i = 0; i < 10; i++)
    {
        correctAnswers[i] = answers[i];
    }
}


It would be so much easier if you just initialized the array from the beginning:
1
2
3
4
5
int main()
{
    char correctAnswers[10] = {'B', 'C', 'A', 'D', 'B', 'A', 'D', 'C', 'A', 'B'};
    // ...
}
Last edited on
closed account (9G8MoG1T)
Hey thanks for the advice. I was confused on how to actually have the main call the char correctAnswers procedure. True it would be, but this program has to have three functions and not two. I'll post the new void setCorrectAnswers and see if anything happens.
I edited my post with another alternative that is a hell heck (Greetings, Messire!) of a lot let tedious, FYI.
Last edited on
closed account (9G8MoG1T)
DUDE IT WORKED! THANK YOU! If I were a millionaire I'd give you a 1mil
No problem, main thing to take away from this would be that the { ... } initializer list only works when you first declare an array.
Topic archived. No new replies allowed.