Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

New to C++ - Arrays and Loops

$
0
0
Hey!

I posted earlier in the week showing you guys my first attempt at writing a C++ program (a quiz prep program for my girlfriend - studying for her state-regulated exam in cosmetology).

Many of you commented saying that I need to focus on achieving the same tasks with less code.
This has been a pretty interesting concept for me to grasp, however I understand its importance.

I've been doing some reading most of the week, and have attempted to solve this solution by the use of arrays/loops.

The new program is not functioning correctly.
My idea was for the program to ask a question > request user input > if the user input is = to the correct answer, then reward the user and move on to the next question - if the user input != to the correct answer, then tell the user the correct answer, and move on to the next question.
Right now, the program displays the first question and answer, and closes from there.

If any of you have a free minute to take a look at this, and offer suggestions, I would greatly appreciate the help!
Super noob here, sorry.


Here is the first version of the program:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{

//#######################################################################################
// Introduction to the program - Instructions on how to take the quiz.	

	cout<<"This is a multiple choice quiz in Hair Cut Theory"<<endl<<endl<<endl;
	cout<<"INSTRUCTIONS:"<<endl<<endl;
	cout<<"I will ask you a series of questions, to which you may reply by"<<endl;
	cout<<"pressing 'a', 'b', 'c', or 'd' followed by the 'enter' key."<<endl<<endl<<endl;
	system("pause");
	system("cls");

//#######################################################################################
// Introduces the variable that keeps track of the score.

	int addscore = 0;

//#######################################################################################
// Question 1.

	char answer1 = 'b';     //Introduces the correct answer to the problem.
	char input1;            //The user input variable for the question.
	cout<<"Question 1."<<endl<<endl;
	cout<<"Achieving balance within a design can be accomplished"<<endl;   //The Question.
	cout<<"by understanding the head shape and:"<<endl<<endl;
	cout<<"a. Head Points"<<endl;
	cout<<"b. Reference Points"<<endl;                   //The possible answers.
	cout<<"c. Four Corners"<<endl;
	cout<<"d. Technique Points"<<endl<<endl;
	cin.get(input1);        //Prompt the user for the answer to the question
	cout<<endl;
		if (input1 == answer1)     //If the user input matches the correct answer, then...
		{
			cout<<"You are correct!"<<endl<<endl;    //Display praise to the user...
			addscore++;       //and add +1 to the score count.
		}
		else {           //If the user input is incorrect, then display the correct answer and move on.
			cout<<"I'm sorry, but the correct answer was "<<answer1<<"."<<endl<<endl;
		}
		cin.get();
		system("pause");
		system("cls");

//#######################################################################################
// Question 2.

	char answer2 = 'a';
	char input2;
	cout<<"Question 2."<<endl<<endl;
	cout<<"The location of the four corners signals a change in the:"<<endl<<endl;
	cout<<"a. Head Shape"<<endl;
	cout<<"b. Bone Shape"<<endl;
	cout<<"c. Hair Texture"<<endl;
	cout<<"d. Hair Growth"<<endl<<endl;
	cin.get(input2);
	cout<<endl;
		if (input2 == answer2)
		{
			cout<<"You are correct!"<<endl<<endl;
			addscore++;
		}
		else {
			cout<<"I'm sorry, but the correct answer was "<<answer2<<"."<<endl<<endl;
		}
		cin.get();
		system("pause");
		system("cls");

//#######################################################################################
// Question 3.

	char answer3 = 'c';
	char input3;
	cout<<"Question 3."<<endl<<endl;
	cout<<"The two front corners represent the widest part of the:"<<endl<<endl;
	cout<<"a. Apex Area"<<endl;
	cout<<"b. Parietal Area"<<endl;
	cout<<"c. Bang Area"<<endl;
	cout<<"d. Nape Area"<<endl<<endl;
	cin.get(input3);
	cout<<endl;
		if (input3 == answer3)
		{
			cout<<"You are correct!"<<endl<<endl;
			addscore++;
		}
		else {
			cout<<"I'm sorry, but the correct answer was "<<answer3<<"."<<endl<<endl;
		}
		cin.get();
		system("pause");
		system("cls");

//#######################################################################################

// This is where the other 22 questions go - removed from the code to save space - you get the idea...

//#######################################################################################
// Test Results - The score is added up and presented to the user.

		cout<<"TEST RESULTS"<<endl<<endl<<endl;
		cout<<"You answered "<<addscore<<" of 25 questions correctly!"<<endl<<endl;

		system("pause");
}






Here is my new code:



#include <iostream>
#include <string>

using namespace std;

int main()
{
	
	//Introduces the library of questions.
	const int MAX_QUESTIONS = 5;
	string testQ[MAX_QUESTIONS];
	int question = 0;
	testQ[question++] = "What is 5+5?";
	testQ[question++] = "What is 2+6?";
	testQ[question++] = "What is 7+7?";
	testQ[question++] = "What is 0+0?";
	testQ[question++] = "What is 9+3?";

	//Introduces the library of answers.
	const int MAX_ANSWERS = 5;
	string testA[MAX_ANSWERS];
	int answers = 0;
	testA[answers++] = "10";
	testA[answers++] = "8";
	testA[answers++] = "14";
	testA[answers++] = "0";
	testA[answers++] = "12";

	//Introduces the library of input variables.
	const int MAX_INPUT = 5;
	string testI[MAX_INPUT];
	int userinput = 0;
	testI[userinput++] = "input1";
	testI[userinput++] = "input2";
	testI[userinput++] = "input3";
	testI[userinput++] = "input4";
	testI[userinput++] = "input5";

	//Question Loop.
	int i;
	if (i=0; i<5; i++)
	{
		cout << testQ[i] << endl;  //Display the question.
		cin.get(testI[i]);   //Request an answer.
		cout << endl;
		if (testI[i] == testA[i])  //Test the user input to the answer.
		{
			cout << "You are correct!" << endl;  //Reward if correct.
		}
		else
		{                  //Tell correct answer if the user input != answer.
			cout << "I'm sorry, the correct answer is " << testA[i] << "." << endl;
		}

	}
	
	system("pause");
}




Thanks for your time!

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>