C++ study notes two-use basic knowledge points to write small programs

The book ESSential C++ that I bought today has arrived. I have studied the first chapter roughly, mainly reviewing several important knowledge points. The following will introduce the content of today's study.
I wrote a small program, which contains the basic use of some knowledge points of C++, which can be used as a small example to practice.
The program mainly realizes the following functions:
1) Create a series of other series such as Fibonacci series, display two of them, and the user fills in the third number.
2) Determine whether the user's data is correct, and ask whether the user is willing to continue to fill in the data.
3) Store the user's score in the TXT file according to the name. If you want to query the score, enter the name to query.
Here is the code:

# include <iostream>
# include <fstream>
# include <string>
# include <vector>
# include <cstdlib>
# include <ctime>
using  namespace std;
int main()
{
    
        // 初始化数列 包含 Fibonacci ,lucas 数列的前六个数
	const int size = 4;

	vector<int> fibo = {
    
     1, 1, 2, 3, 5, 8 };
	vector<int> luca = {
    
     1, 3, 4, 7, 11,18 };
	vector<int> pell = {
    
     1, 2, 5, 12, 29,70 };
	vector<int> squre = {
    
     1, 4, 9, 16, 25, 36 };
	//将指针初始化
	vector <int> *add[4] = {
    
     &fibo , &luca , &pell ,&squre };
	string name,name1;
	int right = 0, total = 0, value = 0;
	int index = 0;//索引,在四个数列中随机选取一个数列
	int index1 = 0;
	cout << "please input your name" << endl;
	cin >> name;
	char next = 'n';
	char go = 'y';

	while (go == 'y' ||  go == 'Y' )
	{
    
    
	
		srand((unsigned)time(0));//产生何种序列种子
		index = rand() % size;//产生0—3的随机数	
		index1 = rand() % 4;//产生0-3的随机数
	//填写数字
		
			cout << "请按照规律填写数字" << " " << add[index]->at(index1)
				<< " " << add[index]->at(index1 + 1) << endl;
			cin >> value;
			//判断输入的值是否正确
			if (value != add[index]->at(index1 + 2))
			{
    
    
				cout << "your anwser is wrong ,would you like to try again ? y/n" << endl;
				cin >> go;
				total++;

			}
			else
			{
    
    
				cout << "congratulations!,would you like to try again?y/n" << endl;
				cin >> go;
				right++;
				total++;
			}
			//将数据写进txt文件
			ofstream outfile("han.txt", ios_base::app);
			
			if (!outfile)//确认是否打开
				cout << "file is not open" << endl;

			else
			{
    
    
				//文件输入数据
				outfile << name << ' ' << right << ' ' << total << endl;


			}
	


		}

	cout << "game is over ,would you like to know your achievement? y/n" << endl;
	cin >> next;
	if (next == 'y' || next == 'Y')
	{
    
    
		ifstream infile("han.txt");
		if (!infile)
			cout << "error" << endl;
		else
		{
    
      
			cout << "输入你想查询的成绩" << endl;
			cin >> name1;
			
			
			    vector <string> read;
				string read1;
				for (int i = 0;!infile.eof() ; i++)
				{
    
       
					infile >> read1;
					read.push_back(read1);
				}
				//判断文件中是否有名字
				for (int i = 0; i < read.size(); i++)
				{
    
      
					if (read[i] == name1)
						cout << "your achievement is " << ' ' << read[i]
						<< ' ' << read[i + 1] << ' ' << read[i + 2] << endl;
	
				}
		
			//
		}
	}

	}


Guess you like

Origin blog.csdn.net/qq_41803340/article/details/106722127