[英]How can I prompt my user to reinput if they use a value outside of the table of values I want them to use?
[英]How can I store all data the user inputs and then show them if they want to see all of them
我是 C++ 新手,一直在制作一個程序,該程序將要求用戶輸入兩個數字和他們想要的操作。 它將顯示結果並詢問他們是否想再試一次。 現在,如果他們嘗試了 5 次或 3 次,則會顯示一些內容,詢問他們是否想查看他們的輸入歷史記錄。 我已經做了 3 天了,但我一直在思考如何正確地做到這一點。 這是我的代碼:
#include<iostream>
using namespace std;
void add(int a, int b, int c);
void subtract(int a, int b, int c);
void multiply(int a, int b, int c);
void divide(double a, double b, double c);
void remainder(int a, int b, int c);
int main(){
Start:
int num[10],c,r=2,i=1;
char x,y,z;
cout<<"Enter Two Integers:\n";
for(i;i<=r;i++){
cin>>num[i];
}
Operation:
cout<<"Choose Operation: \n"
<<" A --- Addition \n"
<<" S --- Subtraction \n"
<<" M --- Multiplication \n"
<<" D --- Division \n"
<<" R --- Remainder \n";
cout<<"Operation: ";
cin>>z;
if(z=='a'||z=='A'){
add(num[1],num[2],c);
}
else if(z=='s'||z=='S'){
subtract(num[1],num[2],c);
}
else if(z=='m'||z=='M'){
multiply(num[1],num[2],c);
}
else if(z=='d'||z=='D'){
divide(num[1],num[2],c);
}
else if(z=='r'||z=='R'){
remainder(num[1],num[2],c);
}else{
system("CLS");
cout<<"Please choose the right option below.\n\n";
goto Operation;
}
cout<<"\nDo you want to try again? (Y or N): ";
cin>>x;
cout<<"\n";
if(x=='Y'||x=='y'){
system("CLS");
goto Start;
i=3;
r=4;
}else{
cout<<"Do you to see input history? (Y or N): ";
cin>>x;
if(x=='Y'||x=='y'){
system("CLS");
cout<<"Input history: \n";
if(z=='a'||z=='A'){
c=num[1]+num[2];
cout<<num[1]<<" + "<<num[2]<<" = "<<c<<endl;
}
else if(z=='s'||z=='S'){
c=num[1]-num[2];
cout<<num[1]<<" - "<<num[2]<<" = "<<c<<endl;
}
else if(z=='m'||z=='M'){
c=num[1]+num[2];
cout<<num[1]<<" * "<<num[2]<<" = "<<c<<endl;
}
else if(z=='d'||z=='D'){
c=num[1]/num[2];
cout<<num[1]<<" / "<<num[2]<<" = "<<c<<endl;
}
else if(z=='r'||z=='R'){
c=num[1]%num[2];
cout<<num[1]<<" % "<<num[2]<<" = "<<c<<endl;
}
}
else{
system("CLS");
cout<<"Thanks for using our program!\n";
return 0;
}
}
}
void add(int a, int b, int c){
c=a+b;
cout<<"The answer is: "<<c<<endl;
}
void subtract(int a, int b, int c){
c=a-b;
cout<<"The answer is: "<<c<<endl;
}
void multiply(int a, int b, int c){
c=a/b;
cout<<"The answer is: "<<c<<endl;
}
void divide(double a, double b, double c){
c=a/b;
cout<<"The answer is: "<<c<<endl;
}
void remainder(int a, int b, int c){
c=a%b;
cout<<"The answer is: "<<c<<endl;
}
如何存儲用戶輸入的所有數據,然后在他們想要查看所有數據時顯示它們?
我認為您可能想要創建一個表示操作的結構。
例如:
struct Operation
{
double left_operand_;
double right_operand_;
char operator_;
double result_;
};
然后,在您的代碼中,您可以添加一個std::vector<Operation>
(用於存儲歷史記錄),每次用戶請求操作時都應該填充該內容。
當用戶詢問歷史時,您只需遍歷向量並顯示每個存儲的Operation
。
正如您提到的,您對 C++ 還很陌生,如果您還不知道什么是std::vector
或如何使用它,您可能有興趣閱讀此參考文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.