![](/img/trans.png)
[英]How can I stop my records from overwriting when i close the program and run it again in c++
[英]how to make my program run again C++
我已經提出了以下問題。
編寫具有以下選項的菜單驅動程序:
- 增值
- 搜索值
- 修改值
- 印刷價值
- 打印所有值的總和
- 退出/終止
您必須創建5個選項作為5個功能。 添加另一個功能以顯示菜單選項。
這是我的代碼:
#include<iostream>
using namespace std;
float f[100]={0};
//1st option
void AddNewValue(){int input;
cout<<"Enter a value\n";
cin>>f[input];
}
//2nd option
void SearchValue(){int i, search;
cout<<"Enter a value to search\n";
cin>>search;
int match=0;
for (int i=1;i<=100;i++)
{if (f[i]==search)
{match=1;
break;}
}
if (match==1){cout<<"Matched value found\n";
}
else {cout<<"No match found\n";}
}
//3rd option
void ModifyValue(){int input1;
cout<<"Enter the position at which you want to modify value\n";
cin>>input1;
cout<<"Enter a value\n";
cin>>f[input1-1];
}
//4th option
void PrintValue(){int i;
for (i=1;i<=100;i++)
{cout<<f[i]<<' ';}
}
//5th option
void PrintSum(){int i,sum;
for(i=1;i<=100;i++)
{sum=f[i]+f[i+1];}
cout<<"Sum is : "<<sum;
}
//starting Function
void menu(){int x;
cout<<"Enter an option: \n";
cout<<"1- Add new value\n2- Search Value\n3- Modify value\n4- Print Value\n5- Print sum of all values\n6- Quit/terminate\n";
cin>>x;
if(x==1){
AddNewValue();
}
else if (x==2){
SearchValue();
}
else if(x==3){
ModifyValue();
}
else if(x==4){
PrintValue();
}
else if(x==5){
PrintSum();
}
else{
}
}
int main(){
menu();
}
我想讓我的完整程序一次又一次地運行,直到用戶輸入錯誤的選項為止。
您可以使用的一個選項是創建一個while循環來封裝menu()
函數的主體,並使用break;
如果用戶輸入6。
void menu(){
int x = 0;
while(1){
cin >> x;
//your code here
if(x==6)
break;
}
}
此示例將導致您的菜單重復直到用戶輸入6。
break;
的目的break;
是要從while循環中“打破”。 在這種情況下,中斷將結束menu()
並返回到main()
。
此外,優良作法是將return 0;
加起來return 0;
到您的主要功能的末尾。
您可以使用循環並使其運行,直到用戶提供特定輸入為止。 在下面的代碼中,一旦程序運行,它將要求用戶輸入5退出或再次運行任何其他鍵,如果用戶輸入5作為輸入,則它將停止,或者如果用戶輸入其他任何輸入,則程序將運行再次。
希望它清楚並為您提供幫助。
#include<iostream>
using namespace std;
float f[100]={0};
//1st option
void AddNewValue(){int input;
cout<<"Enter a value\n";
cin>>f[input];
}
//2nd option
void SearchValue(){int i, search;
cout<<"Enter a value to search\n";
cin>>search;
int match=0;
for (int i=1;i<=100;i++)
{if (f[i]==search)
{match=1;
break;}
}
if (match==1){cout<<"Matched value found\n";
}
else {cout<<"No match found\n";}
}
//3rd option
void ModifyValue(){int input1;
cout<<"Enter the position at which you want to modify value\n";
cin>>input1;
cout<<"Enter a value\n";
cin>>f[input1-1];
}
//4th option
void PrintValue(){int i;
for (i=1;i<=100;i++)
{cout<<f[i]<<' ';}
}
//5th option
void PrintSum(){int i,sum;
for(i=1;i<=100;i++)
{sum=f[i]+f[i+1];}
cout<<"Sum is : "<<sum;
}
//starting Function
void menu(){int x;
cout<<"Enter an option: \n";
cout<<"1- Add new value\n2- Search Value\n3- Modify value\n4- Print Value\n5- Print sum of all values\n6- Quit/terminate\n";
cin>>x;
if(x==1){
AddNewValue();
}
else if (x==2){
SearchValue();
}
else if(x==3){
ModifyValue();
}
else if(x==4){
PrintValue();
}
else if(x==5){
PrintSum();
}
else{
}
}
int main(){
int repeater;
do{
menu();
cout<<"Enter 5 to exit or any other key to run the program again :";
cin>>repeater;
}while(repeater != 5);
}
如果用戶選擇了正確的選項,請再次調用菜單功能。 否則,將控制權返回給main。 我認為這可能有效。
int menu(){int x;
cout<<"Enter an option: \n";
cout<<"1- Add new value\n2- Search Value\n3- Modify value\n4- Print Value\n5- Print sum of all values\n6- Quit/terminate\n";
cin>>x;
if(x!=1||x!=2||x!=3||x!=4||x!=5) return 0;
if(x==1){
AddNewValue();
}
else if (x==2){
SearchValue();
}
else if(x==3){
ModifyValue();
}
else if(x==4){
PrintValue();
}
else if(x==5){
PrintSum();
}
menu();
}
您還可以將代碼包裝在do-while循環中。
void menu(){
do{
int x;
cout<<"Enter an option: \n";
cout<<"1- Add new value\n2- Search Value\n3- Modify value\n4- Print Value\n5- Print sum of all values\n6- Quit/terminate\n";
cin>>x;
if(x==1){
AddNewValue();
}
else if (x==2){
SearchValue();
}
else if(x==3){
ModifyValue();
}
else if(x==4){
PrintValue();
}
else if(x==5){
PrintSum();
}
}while(x==1||x==2||x==3||x==4||x==5);
}
把事情簡單化。 負責顯示菜單的功能應僅顯示菜單並返回用戶的選擇。
enum choice
{
ADD_NEW_VALUE,
SEARCH_VALUE,
// ...
QUIT
};
choice menu()
{
choice result = QUIT;
// display menu
// input choice
// (the more I think about it, the more I'd split it in two separate functions)
return result;
}
int main()
{
while (true) {
switch (menu())
{
case ADD_NEW_VALUE: AddNewValue(); break;
// ...
case QUIT: return 0;
}
}
}
這有助於menu()
的測試,並使添加新選項和新功能更加容易。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.