问题描述
我目前正在开发一个(非常基础的)程序,该程序是编程教程(具有讽刺意味的是,我知道)。我被指示对我的代码进行模块化,以便每个单元都在自己的模块中。我猜这意味着添加标题?我正在使用Visual Studio,如果有帮助的话。我将下面的代码包括在内,以帮助我做出错误的解释。感谢您提供的任何帮助!
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int Total;
int ans;
class Question
{
private:
string Question_Text;
string Answer_one;
string Answer_two;
string Answer_three;
int Correct_Answer;
int Question_score;
public:
void setValues(string,string,int,int);
void askQuestion();
};
int main()
{
string username = "";
char choice=' ';
char c;
int x = 4;
int y = 5;
int z = x + y;
//welcome message
cout << "Hello user,please enter your name:";
cin >> username;
cout << "Welcome to the programming tutorial " << username << "." << endl;
//menu selection
while(choice != '5')
{
cout << "What would you like to do? (Unit 1 - Declaring Variables (1),Unit 2 - Input/ Output (2),Unit 3 - Conditionals (3),Quizzes (4) or Exit (5))";
cin >> choice;
if (choice == '1')
{
cout << "We will begin with defining variables. The first step to doing this is choosing which datatype your variable is.\n";
cout << "The following are a few of the common datatypes used in programming.\n";
cout << "Character ==> char\n";
cout << "Integer ==> int,long,double\n";
cout << "Boolean ==> bool\n";
cout << endl;
cout << "When declaring a variable,you must put its datatype before the variable name.\n";
cout << "An example of this would be if we wanted to declare the value of x as 4.\n";
cout << "We would write this as: \n";
cout << "int x = 4\n";
cout << "The program will Now use the value 4 for the variable name 'x'\n";
cout << endl;
cout << "Now let's assume we assigned the value of 5 to the variable 'y'\n";
cout << "If we wanted to add x and y and assign the sum to the variable 'z',we would write:\n";
cout << "int z = x + y\n";
cout << "Now when we use the variable 'z' in our program,it will perform the calculation given x=4 and y=5 and declare 9 as the value of the variable 'z'.\n";
cout << "To test our code,we would write: " << endl;
cout << "cout<<'x + y'<< z << endl; \n";
cout << "If written correctly,it will display as: \n";
cout << "x + y = " << z << "." << endl;
}
if (choice == '2')
{
cout << "Now that we understand the basics of declaring variables,let's discuss displaying,or output of,information to a user.\n";
cout << "If you wanted to display a welcome message,for example,you would type:\n";
cout << "cout << 'Welcome';\n";
cout << "The line of code would start with 'cout' followed by two less than signs and then the message you wish to display in quotes.\n";
cout << "Using this,you can ask the user for input.\n";
cout << "Enter c to continue...";
cin >> c;
cout << "Let's say we have a program that flips a coin. You may want to ask the user how many times to flip the coin.\n";
cout << "Assuming we prevIoUsly declared this amount variable as 'int timesFlipped',we would 'cout' our question and the next line would read:\n";
cout << "cin>> timesFlipped; \n";
cout << "This will store the users input for the variable 'timesFlipped'\n";
cout << "You almost always end a line of code with a semi colon." << endl;
}
if (choice == '3')
{
cout << "This unit will cover conditional expressions." << endl;
}
if (choice == '4')
{
string Question_Text;
string Answer_one;
string Answer_two;
string Answer_three;
int Correct_Answer;
int Question_score;
Question q1;
Question q2;
Question q3;
cout << username << ",you have chosen to take a quiz." << endl << endl;
int ans,score = 0;
cout << "Unit One Quiz - Variables " << endl << endl;
q1.setValues("How would you declare the value of 'x' as 12? ","x=12()","x==12()","x=12;()",3,1);
q2.setValues("What do you need to put before a variable when declaring it?","a name()","a value()","a datatype()",1);
q3.setValues("Which data type would you use for a number that includes a decimal value?","int()","double()","float()",2,1);
q1.askQuestion();
q2.askQuestion();
q3.askQuestion();
cout << "Your score out of a possible 3 is " << Total << endl;
}
if (choice == 'E')
{
cout << "Have a good day!";
break;
}
}
system("pause");
}
void Question::setValues(string q,string a1,string a2,string a3,int ca,int pa)
{
Question_Text = q;
Answer_one = a1;
Answer_two = a2;
Answer_three = a3;
Correct_Answer = ca;
Question_score = pa;
}
void Question::askQuestion()
{
cout << endl;
cout << Question_Text << endl;
cout << "1. " << Answer_one << endl;
cout << "2. " << Answer_two << endl;
cout << "3. " << Answer_three << endl << endl;
cout << "Please enter your answer: " << endl;
cin >> ans;
if (ans == Correct_Answer)
{
cout << "That is correct!" << endl;
Total = Total + Question_score;
}
else
{
cout << "Sorry,that is incorrect" << endl;
cout << "The correct answer was " << Correct_Answer << endl;
}
}
解决方法
我猜这意味着要添加标题?
差不多就是这个主意。
就您而言,您可能想要:
- 创建标头名称
Question.h
,其中包含类Question
的声明。 - 创建源文件名
Question.cpp
并在其中移动类定义,例如void Question::askQuestion()
等所有函数。 - 创建一个测试文件名
test.cpp
,以放置您的main
函数,并记住包括Question.h
使用Visual Studio时,可以预先创建一个Project,然后在编译/构建之前添加所有这些文件。