存储数据结构
Storing data struct
我运行这个程序很好,但是每次我退出它并再次运行时,矩阵 data[] 中的所有数据都会丢失。有没有办法保留这些数据,以便当我再次运行它时可以检查它?(对不起,如果这个问题太简单了,我几周前就开始编码了)
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <cstdio>
#include <iomanip>
using namespace std;
const int TRUE = 1;
static int n = 0;
struct Restaurant{
char name[30];
char address[50];
float price;
char food[20];
};
Restaurant data[10];
void inputData(void);
void outputData(void);
int main()
{
char option;
cout << "============Welcome to Restaurant Interface!============n" << endl;
while(TRUE)
{
cout << "Type 'A' to add a restaurant data " << endl;
cout << " 'S' to search for restaurants "<< endl;
cout << " or 'E' to exit" << endl;
option = getch();
switch (option)
{
case ('a'):
case ('A'):
inputData();
break;
case ('s'):
case ('S'):
outputData();
break;
case ('e'):
case ('E'):
cout << "nn==============================================================nn";
cout << "Thanks for using Restaurant Interface! See you soon mate!" << endl;
exit(0);
default:
cout << "nInvalid option. please choose againn";
}
cout << "nn==============================================================nn";
}
system("PAUSE");
return 0;
}
void inputData()
{
cout << "nn==============================================================nn";
char temp[80];
cout << "Type the name of your restaurant: "; gets(data[n].name);
cout << "Type the address of your restaurant: "; gets(data[n].address);
cout << "Type the price range: "; gets(temp);
data[n].price = atof(temp);
cout << "Type the style of food: "; gets(data[n].food);;
cout << "New restaurant data added successfully!!!" << endl;
n++;
}
void outputData()
{
cout << "nn==============================================================nn";
if(!n)
{
cout << "Empty list." << endl;
return;
}
cout << "Restaurant list" << endl;
cout << left << setw(20) << "Name";
cout << left << setw(30) << "Address";
cout << left << setw(15) << "Average Price";
cout << left << setw(10) << "Type of cuisine" << endl;
for (int i=0; i<n; i++)
{
cout << left << setw(20) << data[i].name;
cout << left << setw(30) << data[i].address;
cout << "R$" << left << setw(15) << data[i].price;
cout << left << setw(10) << data[i].food << endl;
}
}
程序不保留在不同运行之间存储在其本地内存中的任何可访问数据。
声明全局数据,如
Restaurant data[10];
不能克服此限制。全局将在程序的每次新运行时实例化和初始化,从以前的运行中收集的数据将丢失。
如果要保留该数据,则需要具有某种持久性机制,例如将数据反序列化为文件或数据库表/从文件或数据库表反序列化。
C++程序
完成执行时擦除每个数据。如果要保存程序执行期间创建的信息,最简单和更简单的方法是使用数据文件。以下是有关以下方面的一些信息
相关文章:
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- 哪些存储了不完整类型的 STL 数据结构可以用作类成员?
- 如何避免在数据结构中包含存储为字段的类?
- 如何读取文件并将该文件分类为不同类型的数据,这些数据都存储在结构中的特定数组中
- 用指向文件的指针存储数据结构
- 用于存储分组关系和支持外观的最佳数据结构
- 是否可以将多个结构作为一个数据包存储在一个函数中,然后传递给其他函数并在那里提取?
- unordered_set是否适合存储矢量<int>元素的数据结构?如果是这样,我将如何实现哈希函数?
- 如何将 JSON 键值(我使用 cpp boost 解析)存储在相关数据结构中?
- 用于存储网格(将具有负索引)的数据结构
- 是否可以检查存储在堆栈上的单词是否是回文,而C++中没有任何附加数据结构
- CRTP 模式 但是在数据结构中存储非同构类型
- 使用什么数据结构来存储基于游戏对象的二维单元地图?
- 存储大量会话的最有效数据结构是什么?
- 最佳数据结构存储文本文件中的数字集
- 可用于存储和管理整数集合的最佳C++数据结构是什么
- 什么是存储排列的良好数据结构以及该排列发生的次数
- 从文本中抓取句子,将所有句子分别存储在某个数据结构中
- 文件系统中的数据结构存储
- 以 C++ 数据结构存储多种数据类型