需要将测验放入C++数组中

Need to put a quiz into an array in C++

本文关键字:C++ 数组      更新时间:2023-10-16

我为学校写了一个简单的教程,需要将多项选择题和答案放入一个数组中。我已经学会了如何将数组用于整数,而不是字符或字符串,并且我自己为这个项目弄清楚。我有工作代码,但相当确定我不符合要求。有没有更好的方法来创建一个数组来存储这个测验和答案?我对C++很陌生,这是很多代码,我相信它可以做得更好。请只关注数组部分,并提前感谢您的任何输入。

#include <iostream>
#include <string>
#define MAX_LENGTH 100
using namespace std;
void answer(int, int);
int main()
{
char choice1;
int a, b, c, d, input;
char studentName[MAX_LENGTH] = { 0 };
cout << "Hello new student, What is your name? " << endl;
cin.getline(studentName, MAX_LENGTH);
cout << endl;
cout << "Hello " << studentName << " welcome to my tutorial! " << endl;
cout << endl;
cout << "Let me start by defining a variable, a variable is a way to store a value.n"
<< "It is like a container that holds the value until needed. " << endl;
cout << endl;
cout << "Variables are given names so that they can be called on in your program.nWe name variables with a style of writing"
<< " called camel case.nCamel case is using lower case letters for the first word of the name and an"
<< "capitol letter for the second word." << endl;
cout << endl;
cout << "An example of camel case would be variableName or studentName. " << endl;
cout << endl;
cout << "Do you understand what a variable is? " << endl;
cin >> choice1;
if (toupper(choice1) == 'N')
{
cout << "Variables take input data and store it for later use in your code, "
<< " we name the variables and then call them by name. " << endl;
}
else if (toupper(choice1) == 'Y')
{
cout << "Great! Lets move on to inputs and outputs. " << endl;
}
cout << endl;
cout << "Inputs are the commands that tell the program what to do or process,"
<< " Outputs are what is done or displayed to the user" << endl;
cout << endl;
cout << "Please enter 3 numbers, lets have the program do some math for us. " << endl;
cin >> a >> b >> c;
d = a + b + c;
cout << "The sum of these numbers is  " << d << endl;
cout << endl;
cout << "Syntax is the proper wording, spacing and indentation of the code that allows"
<< " the program to compile and run.n " << endl;
cout << endl;
cout << "Indentations keep commands contained within the line they are meant to run. " << endl;
cout << endl;
cout << "Do you understand why syntax and indentations are important? " << endl;
cin >> choice1;
if (toupper(choice1) == 'N')
{
cout << "Without proper syntax and indentations the compiler won't know what to do with your code"
<< " and will return an error. " << endl;
}
else if (toupper(choice1) == 'Y')
{
cout << "Great! Lets move on to an example of a print statement. " << endl;
}
cout << endl;
cout << "Hello World is a simple program that is usually the first coding you will learn. " << endl;
cout << endl;
cout << "Hello World!" << endl;
cout << endl;
cout << "In order to process your program and get an output you must build and debug your code"
<< " using the tools in visual studio. " << endl;
cout << endl;
cout << "I want to test your knowledge! " << studentName << " Here is a short quiz." << endl;
cout << endl;
int choice, score = 0;
char quiz1[] = { "A. What does a variable do in your program?" };
char ans1[] = { "1. Stores a value" };
char ans2[]{ "2. Starts a new line" };
char ans3[]{ "3. Compiles your code" };
char ans4[]{ "4. Breaks out of a loop" };
cout << quiz1 << endl;
cout << ans1 << endl;
cout << ans2 << endl;
cout << ans3 << endl;
cout << ans4 << endl;
cin >> choice;
answer(choice, 1);
char quiz2[] = { "B. What is syntax? " };
char ans5[]{ "1. Syntax is the way you store a value " };
char ans6[]{ "2. Syntax is a conditional statement " };
char ans7[]{ "3. Syntax is the the proper wording and spacing of the program that allows it to run" };
char ans8[]{ "4. Syntax does nothing in C++" };
cout << quiz2 << endl;
cout << ans5 << endl;
cout << ans6 << endl;
cout << ans7 << endl;
cout << ans8 << endl;
cin >> choice;
answer(choice, 3);
char quiz3[] = { "C. When naming variables what is the term for the type of writing used? " };
char ans9[]{ "1. Camel Cares " };
char ans10[]{"2. Dictation "};
char ans11[]{"3. Caligrapy "};
char ans12[]{"4. Camel Case"};
cout << quiz3 << endl;
cout << ans9 << endl;
cout << ans10 << endl;
cout << ans11 << endl;
cout << ans12 << endl;
cin >> choice;
answer(choice, 4);
void answer(int choice, int answer)
{
if (choice == answer)
{
cout << "Well Done!" << endl;
}
else
{
cout << "Sorry! You missed that one..." << endl;
}
}

感谢大家的投入和指导!我听从了您的一些建议,并制定了符合我学校要求的工作代码! 这是我为阵列问题提出的解决方案。

#include <iostream> //pre processor directives
#include <string>
#define MAX_LENGTH 100 // defines char length for student name input
using namespace std;
const int SIZE = 20; // sets size of password input
const int MIN = 8; // sets min size of password
int numCorrect; // int for score total
int ans; // int for score total

int validLength(char pwd[]); // array for password validation
void answer(int, int); // answer input function
int main()
{
char choice1; // lines 22 - 25 sets parameters for tutorial portion of program
int a, b, c, d, input;
char studentName[MAX_LENGTH] = { 0 };
int choice2 = 0;

cout << "Hello new student, What is your name? " << endl; // gets input for student name stores in variable
cin.getline(studentName, MAX_LENGTH);
cout << endl;
char password[SIZE];
while (true) //infinite while loop until password conditions met
{
cout << "Please create a password with at least 8 characters: " << endl;
cin.getline(password, SIZE);
if (validLength(password) == 1)
{
cout << "Password is valid and accepted! " << endl;
cout << endl;
break; // if password is valid breaks out of while loop
}
else;
{
cout << "Invalid Password, Please follow instructions: " << endl; // gives user error message and loops back to input
cout << endl;
}
}
while (choice2 != 3) // infinite while loop until exit program is selected
{
cout << "Program Menun1. Tutorialn2. Take Quizn3. Exit Program " << endl; // menu to user
cin >> choice2;
cout << endl;
if (choice2 == 1) // conditional statement to start tutorial
{
cout << endl;
cout << "Hello " << studentName << " welcome to my tutorial! " << endl;
cout << "Let me start by defining a variable, a variable is a way to store a value.n"
<< "It is like a container that holds the value until needed. " << endl;
cout << endl;
cout << "Variables are given names so that they can be called on in your program.nWe name variables with a style of writing"
<< " called camel case.nCamel case is using lower case letters for the first word of the name and an"
<< "capitol letter for the second word." << endl;
cout << endl;
cout << "An example of camel case would be variableName or studentName. " << endl;
cout << endl;
cout << "Do you understand what a variable is? " << endl;
cin >> choice1;
if (toupper(choice1) == 'N')
{
cout << "Variables take input data and store it for later use in your code, " // conditional statements and outputs
<< " we name the variables and then call them by name. " << endl;
}
else if (toupper(choice1) == 'Y')
{
cout << "Great! Lets move on to inputs and outputs. " << endl;
}
cout << endl;
cout << "Inputs are the commands that tell the program what to do or process,"
<< " Outputs are what is done or displayed to the user" << endl;
cout << endl;
cout << "Please enter 3 numbers, lets have the program do some math for us. " << endl; // simple math calculator
cin >> a >> b >> c;
d = a + b + c;
cout << "The sum of these numbers is  " << d << endl;
cout << endl;
cout << "Syntax is the proper wording, spacing and indentation of the code that allows"
<< " the program to compile and run.n " << endl;
cout << endl;
cout << "Indentations keep commands contained within the line they are meant to run. " << endl;
cout << endl;
cout << "Do you understand why syntax and indentations are important? " << endl;
cin >> choice1;
if (toupper(choice1) == 'N')
{
cout << "Without proper syntax and indentations the compiler won't know what to do with your code" // conditional statements
<< " and will return an error. " << endl;
}
else if (toupper(choice1) == 'Y')
{
cout << "Great! Lets move on to an example of a print statement. " << endl;
}
cout << endl;
cout << "Hello World is a simple program that is usually the first coding you will learn. " << endl;
cout << endl;
cout << "Hello World!" << endl;
cout << endl;
cout << "In order to process your program and get an output you must build and debug your code"
<< " using the tools in visual studio. " << endl;
cout << endl;
cout << "Now I would like to explain an array." << endl;
cout << endl;
cout << "An array is a way to store multiple values like a grocery list, a set of numbers or even text." << endl;
cout << endl;
cout << "Variables can only store one value so arrays are a more efficient way to store values" << endl;
cout << endl;
cout << "Do you understand arrays? " << endl;
cin >> choice1;
if (toupper(choice1) == 'N') // conditional statements
{
cout << "Arrays store many values and the values can be accessed as a whole or individually. " << endl;
cout << endl;
}
else if (toupper(choice1) == 'Y')
{
cout << "Great! How about a short quiz? " << endl;
cout << endl;
}
}
if (choice2 == 2) // conditional statement to start quiz
{
cout << "I want to test your knowledge! " << studentName << " here is a short quiz." << endl;
cout << endl;
int choice, score = 0;
// string arrays for quiz
string quizOne[5] = { "A. What does a variable do in your program?","1. Stores a value","2. Starts a new line",
"3. Compiles your code","4. Breaks out of a loop"};
for (int i = 0; i < 5; i++)
{ 
cout << quizOne[i] << endl;
}
cin >> choice;
answer(choice, 1);
if (choice == 1)
{
ans = 0;
ans = ans + 1; // starts tallying correct answers to quiz
}
cout << endl;
string quizTwo[5] = { "B. What is syntax?", "1. Syntax is the way you store a value","2. Syntax is a conditional statement ",
"3. Syntax is the the proper wording and spacing of the program that allows it to run","4. Syntax does nothing in C++"};
for (int i = 0; i < 5; i++)
{ 
cout << quizTwo[i] << endl;
}
cin >> choice;
answer(choice, 3);
if (choice == 3)
{
ans = ans + 1;
}
cout << endl;
string quizThree[5] = { "C. When naming variables what is the term for the type of writing used?","1. Camel Cares",
"2. Dictation","3. Caligraphy","4. Camel Case"};
for (int i = 0; i < 5; i++)
{ 
cout << quizThree[i] << endl;
}
cin >> choice;
answer(choice, 4);
cout << endl;
if (choice == 4)
{
ans = ans + 1;
}
numCorrect = ans; // stores number of correct answers in variable ans
if (ans == 3)
{
cout << "You got 100 percent correct! Great Job! " << endl; // outputs to user total score
cout << endl;
}
else
cout << "You answered " << ans << " of the questions correctly" << endl;
cout << endl;
}
else if (choice2 == 3) // conditional statement to end program
{
cout << "Exiting Program" << endl;
}
}
system("pause");
return 0;
}
void answer(int choice, int answer) // function call to output if answer to quiz is correct or not
{
if (choice == answer)
{
cout << "Well Done!" << endl;
}
else
{
cout << "Sorry! You missed that one..." << endl;
}
}
int validLength(char pwd[]) // function call to validate password length
{
bool minLength = false;
int length = strlen(pwd);
if (length >= MIN)
{
minLength = true;
}
if (minLength == true)
return 1;
else
return 0;
}