-
June 25th, 2016, 07:10 PM
#16
Re: Stuck on one part of code.
I am having errors on this part of my code. It is saying that the brace is expected a ; .
Code:
void checkAnswers(char answers1[], char TestAnswers1[], int QUESTIONS, int MIN2PASS)
{
int correctAnswers = 0;
cout << "\nTo pass the test you need at least 15 correct." << endl;
Another part of my errors is that it is saying answer1 and TestAnswers are undefined.
Code:
for (int i = 0; i < CorrectQuestions; i++)
{
if (answers1[i] != TestAnswers1[i])
cout << "Question # " << i << " is wrong." << endl;
Last error is saying that correctAnswers is also undefined.
Code:
cout << "\nYou answered a total of " << correctAnswers << " answers correctly." << endl;
cout << "\nYou answered a total of " << CorrectQuestions - correctAnswers << " answers wrong." << endl;
}
I changed the things you told me to change, but for some reason i am still getting the same errors listed above.
-
June 26th, 2016, 04:29 AM
#17
Re: Stuck on one part of code.
Originally Posted by DTE102
I am having errors on this part of my code. It is saying that the brace is expected a ; .
Code:
void checkAnswers(char answers1[], char TestAnswers1[], int QUESTIONS, int MIN2PASS)
{
int correctAnswers = 0;
cout << "\nTo pass the test you need at least 15 correct." << endl;
Then add it!
Originally Posted by DTE102
Another part of my errors is that it is saying answer1 and TestAnswers are undefined.
Code:
for (int i = 0; i < CorrectQuestions; i++)
{
if (answers1[i] != TestAnswers1[i])
cout << "Question # " << i << " is wrong." << endl;
The is not any TestAnswers in this code snippet. There is, however, TestAnswers1...
Anyway, if there are undefined then just define them!
Victor Nijegorodov
-
June 26th, 2016, 04:54 AM
#18
Re: Stuck on one part of code.
I changed the things you told me to change,
Post your complete current code so that we can see actually what has been changed. I got your code in post #9 to compile by adding the missing brace and dealing with the scope of CorrectQuestions and MinQuestions.
It's also useful if you receive compiler errors to state them as shown. Also note that, depending upon the error, the actual error may be in code preceding the line in which the error is reported.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 26th, 2016, 03:18 PM
#19
Re: Stuck on one part of code.
This is my current code.
Code:
#include <iostream>
#include <cctype>
using namespace std;
void checkAnswers(char[], char[], int, int);
int main()
{
const int CorrectQuestions = 10; //Total of correcet answers fr exam.
const int MinQuestions = 8; //User has to get at least 8 questions correct to pass exam.
char answers[CorrectQuestions] = { 'B','D','A','A','C','A','B','A','C','D' }; //Array of correct answers.
char TestAnswers[CorrectQuestions];
cout << "Enter a letter from A, B, C, D for each question " << endl;
for (int studentAnswers = 0; studentAnswers < CorrectQuestions; studentAnswers++) //This loop is for the students answers for the exam.
{
cout << "Please enter your answer for question #" << (studentAnswers + 1) << ": ";
cin >> TestAnswers[studentAnswers];
while (TestAnswers[studentAnswers] != 'A' && TestAnswers[studentAnswers] != 'B' && TestAnswers[studentAnswers] != 'C' && TestAnswers[studentAnswers] != 'D') //This loop makes sure if the student enter the wrong input three times then program will say goodbye and exit.
{
if (studentAnswers > 3)
{
printf("Good Bye."); return -1;
}
}
}
void checkAnswers(char answers1[], char TestAnswers1[], int QUESTIONS, int MIN2PASS){
int correctAnswers = 0;
cout << "\nTo pass the test you need at least 8 correct." << endl;
for (int i = 0; i < CorrectQuestions; i++) //This loop checks the students answers.
{
if (answers1[i] == TestAnswers1[i])
correctAnswers++;
}
if (correctAnswers >= MinQuestions) //Checks to see if student passed or failed.
{
cout << "\nCongratulations! You passed the exam!\n";
}
else
{
cout << "\nSorry. You failed the exam.\n";
}
cout << "\nThese are the questions you missed...\n";
for (int i = 0; i < CorrectQuestions; i++)
{
if (answers1[i] != TestAnswers1[i])
cout << "Question # " << i << " is wrong." << endl;
}
//Display the number of correct and incorrect answers
cout << "\nYou answered a total of " << correctAnswers << " answers correctly." << endl;
cout << "\nYou answered a total of " << CorrectQuestions - correctAnswers << " answers wrong." << endl;
}
}
-
June 26th, 2016, 03:34 PM
#20
-
June 26th, 2016, 03:36 PM
#21
Re: Stuck on one part of code.
You are missing the closing } for main() as you are trying to define a function within a function which is not allowed.
CorrectQuestions and MinQuestions are defined in main() but are also used in checkAnswers() for which they are not defined as they are no in-scope within that function. if you want CorrectQuestions and MinQuestions to be used within main() and checkAnswers() then make then global (define them before main()).
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 26th, 2016, 04:16 PM
#22
Re: Stuck on one part of code.
OK.. For your starter for 10 this is your code changed so that it compiles and produces some output. You now have the logic errors to fix.
Code:
#include <iostream>
//#include <cctype> //Not needed
using namespace std;
const int CorrectQuestions = 10; //Total of correct answers fr exam.
const int MinQuestions = 8; //User has to get at least 8 questions correct to pass exam.
void checkAnswers(char[], char[], int, int);
int main()
{
char answers[CorrectQuestions] = { 'B','D','A','A','C','A','B','A','C','D' }; //Array of correct answers.
char TestAnswers[CorrectQuestions];
cout << "Enter a letter from A, B, C, D for each question " << endl;
for (int studentAnswers = 0; studentAnswers < CorrectQuestions; studentAnswers++) //This loop is for the students answers for the exam.
{
cout << "Please enter your answer for question #" << (studentAnswers + 1) << ": ";
cin >> TestAnswers[studentAnswers];
while (TestAnswers[studentAnswers] != 'A' && TestAnswers[studentAnswers] != 'B' && TestAnswers[studentAnswers] != 'C' && TestAnswers[studentAnswers] != 'D') //This loop makes sure if the student enter the wrong input three times then program will say goodbye and exit.
{
if (studentAnswers > 3)
{
printf("Good Bye.");
return -1;
}
}
}
checkAnswers(answers, TestAnswers, 0, 0);
}
void checkAnswers(char answers1[], char TestAnswers1[], int QUESTIONS, int MIN2PASS)
{
int correctAnswers = 0;
cout << "\nTo pass the test you need at least 8 correct." << endl;
for (int i = 0; i < CorrectQuestions; i++) //This loop checks the students answers.
{
if (answers1[i] == TestAnswers1[i])
correctAnswers++;
}
if (correctAnswers >= MinQuestions) //Checks to see if student passed or failed.
{
cout << "\nCongratulations! You passed the exam!\n";
}
else
{
cout << "\nSorry. You failed the exam.\n";
}
cout << "\nThese are the questions you missed...\n";
for (int i = 0; i < CorrectQuestions; i++)
{
if (answers1[i] != TestAnswers1[i])
cout << "Question # " << i << " is wrong." << endl;
}
//Display the number of correct and incorrect answers
cout << "\nYou answered a total of " << correctAnswers << " answers correctly." << endl;
cout << "\nYou answered a total of " << CorrectQuestions - correctAnswers << " answers wrong." << endl;
}
To avoid using the global variables, the parameters QUESTIONS and MIN2PASS of checkAnswers() should be used.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 26th, 2016, 04:16 PM
#23
Re: Stuck on one part of code.
Thank you for the response. Everything is looking good except my Testanswers1 and answers1 being undefined. When defining these i do define them in main()? And would they be a char or int? If trying int i get an error saying my i expression must have a pointer to object type.
-
June 26th, 2016, 04:18 PM
#24
Re: Stuck on one part of code.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 26th, 2016, 05:58 PM
#25
Re: Stuck on one part of code.
Code is working good. I am just having trouble with it exiting after three wrong inputs. If i enter one wrong input it will not let me enter any more wrong inputs
-
June 26th, 2016, 08:16 PM
#26
Re: Stuck on one part of code.
i figured it out finally. thanks for all the help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|