CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 26 of 26
  1. #16
    Join Date
    Jun 2016
    Posts
    29

    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.

  2. #17
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Stuck on one part of code.

    Quote Originally Posted by DTE102 View Post
    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!

    Quote Originally Posted by DTE102 View Post
    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

  3. #18
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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)

  4. #19
    Join Date
    Jun 2016
    Posts
    29

    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;
    	}
    }

  5. #20
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Stuck on one part of code.

    Quote Originally Posted by DTE102 View Post
    This is my current code.
    ...
    And where are the corresponding error messages?

    OK. Let me ask you: why do you define the
    Code:
    void checkAnswers(char answers1[], char TestAnswers1[], int QUESTIONS, int MIN2PASS)
    within the main() function?
    It is not allowed in C/C++
    Victor Nijegorodov

  6. #21
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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)

  7. #22
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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)

  8. #23
    Join Date
    Jun 2016
    Posts
    29

    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.

  9. #24
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Stuck on one part of code.

    See my post #22
    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)

  10. #25
    Join Date
    Jun 2016
    Posts
    29

    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

  11. #26
    Join Date
    Jun 2016
    Posts
    29

    Re: Stuck on one part of code.

    i figured it out finally. thanks for all the help

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured