2016-03-04 55 views
-3

我正在运行该程序,但每次退出并再次运行时,矩阵数据[]中的所有数据都将丢失。有没有办法保存这些数据,所以当我再次运行它时,我可以检查它吗? (很抱歉,如果这个问题太简单了,我已经开始编码几个星期前)存储数据struct

#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 << "\n\n==============================================================\n\n"; 
       cout << "Thanks for using Restaurant Interface! See you soon mate!" << endl; 
       exit(0); 
      default: 
       cout << "\nInvalid option. please choose again\n";     
     } 

     cout << "\n\n==============================================================\n\n"; 
    } 

    system("PAUSE"); 
    return 0; 
} 

void inputData() 
{ 
    cout << "\n\n==============================================================\n\n"; 
    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 << "\n\n==============================================================\n\n"; 
    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; 
    } 
} 
+1

所以你的意思是你想要将你的输入序列化到一个文件,并从你的程序的另一个运行中恢复(反序列化)它们? –

+1

每次启动一个程序并初始化一个变量时,它都会存储在您的内存中。当变量超出范围或者你简单地退出程序时,你的变量将从内存中清除。如果你想保留你的数据,你需要将它存储在一个文件中。 – ppsz

+0

请发布[mcve],重点放在*“最小”*。 –

回答

0

程序不保留任何数据存储在不同运行之间的局部存储器进行访问。

声明像

Restaurant data[10]; 

全局数据不克服这个限制。全球将在该计划的每一次新运行中实例化和初始化,从前运行收集的数据丢失。

如果你想保留这些数据,你需要有某种持久化机制,比如将数据从文件或数据库表中去除/序列化。

0

C++在程序结束执行时擦除每个数据。如果您想保存程序执行期间创建的信息,最简单最简单的方法就是使用数据文件。 Here is some information about