制作一个程序,创建一个队列并将其写入文件,但不幸的是它只能写数字。如何记录撒布式数据。我认为改变"值的类型就足够了#34;从int到string的变量。但这还不够。
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
class Queue
{
private:
std::vector<int> queue;
public:
void createNewQueue(int amountElement)
{
queue.resize(queue.size() + amountElement);
std::cout << "System: the queue was created from " << amountElement << "
elements!" << std::endl;
}
void initZero()
{
for (int i = 0; i < queue.size(); i++) {
queue[i] = 0;
}
std::cout << "System: the queue was initialized with zeros!" <<
std::endl;
}
void changeValue(int index, int value)
{
if (index <= queue.size()) {
queue[index] = value;
std::cout << "SYstem: Ready. The element at index " << index << " It
was changed to " << value << std::endl;
}
else {
std::cout << "System: change error!" << std::endl;
}
}
void createFile(std::string name) {
std::ofstream file;
file.open(name);
for (int i = 0; i < queue.size(); i++) {
file << queue[i] << std::endl;
}
file.close();
std::cout << "System: queue file write successful!" << std::endl;
}
};
int main()
{
int selection = 0, amountElement = 0, index = 0, value = 0;
std::string name;
std::cout << "===================================================" <<
std::endl;
std::cout << "1. Create a new queue." << std::endl;
std::cout << "===================================================" <<
std::endl;
std::cout << "Enter your choice: ";
std::cin >> selection;
std::cout << "===================================================" <<
std::endl;
Queue q;
if (selection == 1) {
std::cout << "Enter the number of items in the queue: ";
std::cin >> amountElement;
std::cout << "===================================================" <<
std::endl;
q.createNewQueue(amountElement);
std::cout << "===================================================" <<
std::endl;
while (true) {
std::cout << "1. Initialize with zeros." << std::endl;
std::cout << "2. Change the line item." << std::endl;
std::cout << "3. Save all to file." << std::endl;
std::cout << "4. Exit." << std::endl;
std::cout << "==================================================="
<< std::endl;
std::cout << "Enter your choice: ";
std::cin >> selection;
if (selection == 1) {
std::cout <<
"===================================================" << std::endl;
q.initZero();
std::cout <<
"===================================================" << std::endl;
}
if (selection == 2) {
std::cout <<
"===================================================" << std::endl;
std::cout << "Enter the index queue element: ";
std::cin >> index;
std::cout << "Enter value: ";
std::cin >> value;
std::cout <<
"===================================================" << std::endl;
q.changeValue(index, value);
std::cout <<
"===================================================" << std::endl;
}
//I am very ashamed that I'm asking such elementary things
if (selection == 3) {
std::cout <<
"===================================================" << std::endl;
std::cout << "Enter name file: ";
std::cin >> name;
std::cout <<
"===================================================" << std::endl;
q.createFile(name);
std::cout <<
"===================================================" << std::endl;
}
if (selection == 4) {
break;
}
}
}
return 0;
}
我非常惭愧,我在问这些基本的事情。
答案 0 :(得分:0)
实际上,你可能无处可改变:
1. std::vector<int> queue;
至std::vector<std::string> queue;
2. queue[i] = 0;
类似于queue[i] = "";
3. void changeValue(int index, int value)
至void changeValue(int index, std::string value)
main()
中的
4. int selection = 0, amountElement = 0, index = 0, value = 0;
更改为
int selection = 0, amountElement = 0, index = 0;
std::string value;
换句话说,您的代码将使用字符串,只需在适用的情况下将类型从int
更改为std::string
。
顺便说一句,更正您的范围检查if (index <= queue.size())
至if (index < queue.size())