table of Contents
- Preface
- Some problems I have encountered
-
- 1. Errors caused by different pointer types (error: cannot convert'int*' to'std::vector*' in initialization)
- 2. About rand() and srand()
- 3. Problems caused by the higher precedence of subscript operators (error: no match for'operator*' (operand type is'std::vector'))
- 4. Some changes to the content of the book
- Final code
Preface
In the process of reading Essential c++, I found that the teaching fragments used by the author seemed to be extracted from the same file, but the code of the file was not given. So I decided to try to restore it myself. In this process, I benefited a lot and recovered a lot of knowledge points that I had overlooked when reading books. Hope to share these feelings with you.
Some problems I have encountered
1. Errors caused by different pointer types (error: cannot convert'int*' to'std::vector*' in initialization)
The following is the error code:
const int max_seq = 6;
const int seq_size = 18;
int elem_vals[seq_size] = {
1, 2, 3,
3, 4, 7,
2, 5, 12,
3, 6, 10,
4, 9, 16,
5, 12, 22
};
vector<int> *seq_addrs[max_seq] = {
elem_vals, (elem_vals+3), (elem_vals+6),
(elem_vals+9), (elem_vals+12), (elem_vals+15)
};
It can be seen that
//int* can be used as a vector parameter to create a vector object
// but it cannot be used to create a pointer array vector *seq_addrs[max_seq] is
amended as follows:
const int seq_size = 18;
int elem_vals[seq_size] = {
1, 2, 3,
3, 4, 7,
2, 5, 12,
3, 6, 10,
4, 9, 16,
5, 12, 22
};
vector<int> elem_seq(elem_vals,elem_vals + seq_size);
//int* 可以作为 vector<int> 的参数创建vector object
//但却不能用以创建指针数组 vector<int> *seq_addrs[max_seq]
const int max_seq = 6;
string seq_names[max_seq] = {
"Fibonacci",
"Lucas",
"Pell",
"Triangular",
"Square",
"Pentagonal"
};
vector<int> Fibonacci(elem_vals,elem_vals+3),
Lucas(elem_vals+3,elem_vals+6),
Pell(elem_vals+6,elem_vals+9),
Triangular(elem_vals+9,elem_vals+12),
Square(elem_vals+12,elem_vals+15),
Pentagonal(elem_vals+15,elem_vals+18);
vector<int> *seq_addrs[max_seq] = {
&Fibonacci, &Lucas, &Pell, &Triangular, &Square, &Pentagonal
};
2. About rand() and srand()
rand() and srand() are pseudo-random number generators provided by the standard library. srand() is the seed for random number generation. The header file cstdlib is required to use them.
The following is an example:
int num = 1,seq_index = 0;
vector<int> *cur_tuple = 0;
cout << "Please enter a number to reset the random number.\n";
cin >> num;
srand(num);
seq_index = rand() % max_seq;
cur_tuple = seq_addrs[seq_index];
3. Problems caused by the higher precedence of subscript operators (error: no match for'operator*' (operand type is'std::vector'))
Dereference: Get the object located at the memory address pointed to by the pointer,
such as:
int a;
a = *pi;
However, the precedence of the subscript operator is higher, so the following errors may occur:
cout << "The first two elements of the sequence are: "
<< *cur_tuple[0] << ", "// 0
<< (*cur_tuple)[1] // 1
<< "\nWhat is the next element?\n";
//error: no match for 'operator*' (operand type is 'std::vector<int>')
At this time, change the position 0 to 1 position, that is, the withdrawal operation is performed now, and then the subscript operation is performed.
4. Some changes to the content of the book
else
{
string name;
int nt;
int nc;
while(in_file >> name)
{
in_file >> nt >> nc;
if(name == usr_name)
{
num_tries_t = nt;
num_right = nc;
}
}
cout << "Welcome back, " << usr_name
<< "\nYour current score is " << nc
<< " out of " << nt << "\nGood Luck!\n";
}
Final code
/*
"The value 2, 3 for two consecutive
elements of a numerical sequence.
What is the next value?
pineklll
2021/3/27
*/
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <cstdlib>
//rand() and srand() 都是伪随机(pseudo-random)数生成器, 包含于此头文件中
using namespace std;
int main()
{
string usr_name;
int usr_val, num_tries = 0, num_right = 0, num_tries_t = 0;
double usr_score = 0.0;
bool next_seq = true;
int usr_guess = 0;
char usr_rsp;
cout << "Enter your name:\n";
cin >> usr_name;
ofstream out_file("D:\\cpp code\\my blog code_1\\seq_data.txt",ios_base::app);
if(!out_file)
{
cerr << "Oops! Unable to open output file!\n"; return -2;}
ifstream in_file("D:\\cpp code\\my blog code_1\\seq_data.txt");
if(!in_file)
{
cerr << "Nice to meet you!\n"; }
else
{
string name;
int nt;
int nc;
while(in_file >> name)
{
in_file >> nt >> nc;
if(name == usr_name)
{
num_tries_t = nt;
num_right = nc;
}
}
cout << "Welcome back, " << usr_name
<< "\nYour current score is " << nc
<< " out of " << nt << "\nGood Luck!\n";
}
const int seq_size = 18;
int elem_vals[seq_size] = {
1, 2, 3,
3, 4, 7,
2, 5, 12,
3, 6, 10,
4, 9, 16,
5, 12, 22
};
vector<int> elem_seq(elem_vals,elem_vals + seq_size);
const int max_seq = 6;
string seq_names[max_seq] = {
"Fibonacci",
"Lucas",
"Pell",
"Triangular",
"Square",
"Pentagonal"
};
vector<int> Fibonacci(elem_vals,elem_vals+3),
Lucas(elem_vals+3,elem_vals+6),
Pell(elem_vals+6,elem_vals+9),
Triangular(elem_vals+9,elem_vals+12),
Square(elem_vals+12,elem_vals+15),
Pentagonal(elem_vals+15,elem_vals+18);
vector<int> *seq_addrs[max_seq] = {
&Fibonacci, &Lucas, &Pell, &Triangular, &Square, &Pentagonal
};
int num = 1,seq_index = 0;
vector<int> *cur_tuple = 0;
cout << "Please enter a number to reset the random number.\n";
cin >> num;
srand(num);
seq_index = rand() % max_seq;
cur_tuple = seq_addrs[seq_index];
while(next_seq == true)
{
cout << "The first two elements of the sequence are: "
<< (*cur_tuple)[0] << ", "
<< (*cur_tuple)[1]
<< "\nWhat is the next element?\n";
cin >> usr_guess;
++num_tries;
++num_tries_t;
if(usr_guess == (*cur_tuple)[2])
{
++num_right;
cout << "Very good. Yes, "
<< (*cur_tuple)[2]
<< " is the next element in the "
<< seq_names[seq_index] << " sequence.\n";
break;
}
else
{
switch (num_tries)
{
case 1:
cout << "Oops! Nice guess but not quite it.\n";
break;
case 2:
cout << "Hum. Sorry but wrong a second time.\n";
break;
case 3:
cout << "Ah, this is harder than it looks, isn't it?\n";
break;
default:
cout << "It must be getting pretty frustrating by now!\n";
break;
}
cout << "Want to try again?(Y/N)";
cin >> usr_rsp;
if(usr_rsp == 'N' || usr_rsp == 'n')
{
next_seq = false;
cout << "Thanks for playing!";
}
else
{
seq_index = rand() % max_seq;
cur_tuple = seq_addrs[seq_index];
}
}
}
out_file << usr_name << ' '
<< num_tries_t << ' '
<< num_right << endl;
return 0;
}