尝试使用 "while (COM != "Y" || "N" )" 完成由用户输入确定的循环
Trying to acomplish a Loop determined by user input using "while (COM != "Y" || "N" )"
所以我遇到的问题是在我的 Class IOutro
中,在它的函数 commandInput()
中,我的目的是创建一个简单的基于命令的循环,询问用户是否想再次使用该程序或关闭它。
我对这个程序的目标是 类 仅用于存储函数,我在 main 中唯一想要的是调用这些函数的对象。第一次尝试 OOP。
因此,当我尝试 运行 时出现错误。
||=== Build: Debug in Tutorials (compiler: GNU GCC Compiler) ===|
C:\Users\Jason\Documents\Tutorials\main.cpp||In function 'int main()':|
C:\Users\Jason\Documents\Tutorials\main.cpp|57|error: could not convert 'COM.std::basic_string<_CharT, _Traits, _Alloc>::operator=<char, std::char_traits<char>, std::allocator<char> >(((const char*)"Y"))' from 'std::basic_string<char>' to 'bool'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
我不确定我应该看什么,我知道这是我使用的字符串的问题,它也给了我一次错误,然后一些更改说它无法识别 !=
运算符,我在想 !=
意味着 "Is Not Equal To" 而 ||
意味着 "Or" ((基本上,如果左边不是真的,检查右边的值,如果左边的值是true,忽略正确的值。))
我确定我遗漏了一些简单的东西,或者我没有查找正确的信息。有哪位大神能赐教吗
这不是作业,我不是学生,我是自学 C++,像这样的地方基本上是我唯一能找到答案的地方。
#include <iostream> // This is a PreProcessor Directive
#include <string> // Do I need this?
using namespace std; // This includes a library???
int A; // Global A
int B; // Global B
string COM = "Y"; // Global used to check if they want to keep going, not sure how to make this not global.
class Arith{ // Class for the calculator stuff.
public: // Class is public.
int sum(int A, int B){ // Sum, the actually calculation.
int C = A + B; // Just math.
return C; // Return value of C.
}
void calc(){ // Calc, not a calculation.
cout << "Enter a number for A: "; // Ask for A.
cin >> A; // Get A.
cout << endl; // Make it look neat.
cout << "Enter a number for B: "; // Ask for B.
cin >> B; // Get B.
cout << endl; // Make it look neat.
cout << "The sum is: " << sum(A, B); // Print out the sum.
}
};
class IOutro{ // Class for all informative prompts.
public:
void goodbye(){ // The goodbye class
cout << "Thank you!" << endl; // Display a goodbye.
}
void welcome(){ // The welcome class.
cout << "Welcome!" << endl; // Display a welcome.
}
void commandInput(){ // The FML check if they want to keep adding numbers loop of doom...
cout << "Would you like to continue?" << endl; // Goal is to check if they want to keep going, Y or N.
cout << "Please type 'Y' for Yes, and 'N' for No." << endl; // They are given options
cin >> COM; // ???? Get COM
while (COM != "Y" || "N" ){ // Trying to make it assure they type Y or N, and if they don't it keeps asking.
cout << "Would you like to continue?" << endl; // Copied the text.
cout << "Please type 'Y' for Yes, and 'N' for No." << endl; // For this loop.
cin >> COM; // ???? Get COM, I am pretty sure it's suppose to be getline, but that doesn't work...
}
}
};
int main(){ // Indicates a Main Function which returns an Integer?
IOutro IObject; // Declare the IOutro IObject Object?
Arith ArithObject; // Same with Arith?
while (COM = "Y"){ // If they chose Y, run loop
IObject.welcome(); // Run IObject welcome
ArithObject.calc(); // Run ArithObject calc
ArithObject.sum(A,B); // Run ArithObject sum
IObject.commandInput(); // Run IObject commandInput
}
IObject.goodbye(); // If COM != "Y" run IObject goodbye
return 0; // Return the value 0 to main?
}
让我们看看您的代码中的问题。
首先,你不需要COM
是一个字符串,将它声明为一个字符就足够了,比如
char COM = 'Y';
接下来,您应该将 commandInput()
中的 while 循环条件从
更改为
while (COM != "Y" || "N" )
至
while (COM != 'Y' && COM != 'N' ) // if you don't want it to be case sensitive, then you will have to add small letters to the condition as well
你的条件不对,你需要像上面给出的那样检查。
现在您可能想知道为什么我将 ||
更改为 &&
。这是因为如果它是 ||
,如果 COM
不是 'Y'
或者 COM
不是 'N'
,循环将继续。现在就这样想,如果COM
是'Y'
,那么就不是'N'
,如果不是'N'
,那么你的while循环中的条件就满足了,因此,它只是继续。
现在,让我们看下一个问题。在你的main()
中,你有
while (COM = "Y")
其中=
是赋值运算符,如果要检查是否相等,则需要使用==
。即
while (COM == 'Y' )
所以简而言之,你的代码的固定版本是(我已经删除了你的评论并在我进行了更改的地方添加了评论,有三行更改)
#include <iostream>
using namespace std;
int A;
int B;
char COM = 'Y'; // changed to char
class Arith{
public:
int sum(int A, int B){
int C = A + B;
return C;
}
void calc(){
cout << "Enter a number for A: ";
cin >> A;
cout << endl;
cout << "Enter a number for B: ";
cin >> B;
cout << endl;
cout << "The sum is: " << sum(A, B)<<endl;
}
};
class IOutro{
public:
void goodbye(){
cout << "Thank you!" << endl;
}
void welcome(){
cout << "Welcome!" << endl;
}
void commandInput(){
cout << "\nWould you like to continue?" << endl;
cout << "Please type 'Y' for Yes, and 'N' for No." << endl;
cin >> COM;
while ( ( COM != 'Y' && COM != 'y' ) && ( COM != 'N' && COM != 'n' ) ) { // made changes here
cout << "\nWould you like to continue?" << endl;
cout << "Please type 'Y' for Yes, and 'N' for No." << endl;
cin >> COM;
}
}
};
int main(){
IOutro IObject;
Arith ArithObject;
while (COM == 'Y' || COM == 'y' ){ // made changes here
IObject.welcome();
ArithObject.calc();
ArithObject.sum(A,B);
IObject.commandInput();
}
IObject.goodbye();
return 0;
}
请注意,对于此代码,您不需要 #include<string>
并且还使代码不区分大小写(即 'Y'
或 'y'
和 'N'
或 'n'
)
这将消除您遇到的令人讨厌的错误。
好吧,希望能解决问题(好吧,这对我来说已经解决了)
所以我遇到的问题是在我的 Class IOutro
中,在它的函数 commandInput()
中,我的目的是创建一个简单的基于命令的循环,询问用户是否想再次使用该程序或关闭它。
我对这个程序的目标是 类 仅用于存储函数,我在 main 中唯一想要的是调用这些函数的对象。第一次尝试 OOP。
因此,当我尝试 运行 时出现错误。
||=== Build: Debug in Tutorials (compiler: GNU GCC Compiler) ===|
C:\Users\Jason\Documents\Tutorials\main.cpp||In function 'int main()':|
C:\Users\Jason\Documents\Tutorials\main.cpp|57|error: could not convert 'COM.std::basic_string<_CharT, _Traits, _Alloc>::operator=<char, std::char_traits<char>, std::allocator<char> >(((const char*)"Y"))' from 'std::basic_string<char>' to 'bool'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
我不确定我应该看什么,我知道这是我使用的字符串的问题,它也给了我一次错误,然后一些更改说它无法识别 !=
运算符,我在想 !=
意味着 "Is Not Equal To" 而 ||
意味着 "Or" ((基本上,如果左边不是真的,检查右边的值,如果左边的值是true,忽略正确的值。))
我确定我遗漏了一些简单的东西,或者我没有查找正确的信息。有哪位大神能赐教吗
这不是作业,我不是学生,我是自学 C++,像这样的地方基本上是我唯一能找到答案的地方。
#include <iostream> // This is a PreProcessor Directive
#include <string> // Do I need this?
using namespace std; // This includes a library???
int A; // Global A
int B; // Global B
string COM = "Y"; // Global used to check if they want to keep going, not sure how to make this not global.
class Arith{ // Class for the calculator stuff.
public: // Class is public.
int sum(int A, int B){ // Sum, the actually calculation.
int C = A + B; // Just math.
return C; // Return value of C.
}
void calc(){ // Calc, not a calculation.
cout << "Enter a number for A: "; // Ask for A.
cin >> A; // Get A.
cout << endl; // Make it look neat.
cout << "Enter a number for B: "; // Ask for B.
cin >> B; // Get B.
cout << endl; // Make it look neat.
cout << "The sum is: " << sum(A, B); // Print out the sum.
}
};
class IOutro{ // Class for all informative prompts.
public:
void goodbye(){ // The goodbye class
cout << "Thank you!" << endl; // Display a goodbye.
}
void welcome(){ // The welcome class.
cout << "Welcome!" << endl; // Display a welcome.
}
void commandInput(){ // The FML check if they want to keep adding numbers loop of doom...
cout << "Would you like to continue?" << endl; // Goal is to check if they want to keep going, Y or N.
cout << "Please type 'Y' for Yes, and 'N' for No." << endl; // They are given options
cin >> COM; // ???? Get COM
while (COM != "Y" || "N" ){ // Trying to make it assure they type Y or N, and if they don't it keeps asking.
cout << "Would you like to continue?" << endl; // Copied the text.
cout << "Please type 'Y' for Yes, and 'N' for No." << endl; // For this loop.
cin >> COM; // ???? Get COM, I am pretty sure it's suppose to be getline, but that doesn't work...
}
}
};
int main(){ // Indicates a Main Function which returns an Integer?
IOutro IObject; // Declare the IOutro IObject Object?
Arith ArithObject; // Same with Arith?
while (COM = "Y"){ // If they chose Y, run loop
IObject.welcome(); // Run IObject welcome
ArithObject.calc(); // Run ArithObject calc
ArithObject.sum(A,B); // Run ArithObject sum
IObject.commandInput(); // Run IObject commandInput
}
IObject.goodbye(); // If COM != "Y" run IObject goodbye
return 0; // Return the value 0 to main?
}
让我们看看您的代码中的问题。
首先,你不需要COM
是一个字符串,将它声明为一个字符就足够了,比如
char COM = 'Y';
接下来,您应该将 commandInput()
中的 while 循环条件从
while (COM != "Y" || "N" )
至
while (COM != 'Y' && COM != 'N' ) // if you don't want it to be case sensitive, then you will have to add small letters to the condition as well
你的条件不对,你需要像上面给出的那样检查。
现在您可能想知道为什么我将 ||
更改为 &&
。这是因为如果它是 ||
,如果 COM
不是 'Y'
或者 COM
不是 'N'
,循环将继续。现在就这样想,如果COM
是'Y'
,那么就不是'N'
,如果不是'N'
,那么你的while循环中的条件就满足了,因此,它只是继续。
现在,让我们看下一个问题。在你的main()
中,你有
while (COM = "Y")
其中=
是赋值运算符,如果要检查是否相等,则需要使用==
。即
while (COM == 'Y' )
所以简而言之,你的代码的固定版本是(我已经删除了你的评论并在我进行了更改的地方添加了评论,有三行更改)
#include <iostream>
using namespace std;
int A;
int B;
char COM = 'Y'; // changed to char
class Arith{
public:
int sum(int A, int B){
int C = A + B;
return C;
}
void calc(){
cout << "Enter a number for A: ";
cin >> A;
cout << endl;
cout << "Enter a number for B: ";
cin >> B;
cout << endl;
cout << "The sum is: " << sum(A, B)<<endl;
}
};
class IOutro{
public:
void goodbye(){
cout << "Thank you!" << endl;
}
void welcome(){
cout << "Welcome!" << endl;
}
void commandInput(){
cout << "\nWould you like to continue?" << endl;
cout << "Please type 'Y' for Yes, and 'N' for No." << endl;
cin >> COM;
while ( ( COM != 'Y' && COM != 'y' ) && ( COM != 'N' && COM != 'n' ) ) { // made changes here
cout << "\nWould you like to continue?" << endl;
cout << "Please type 'Y' for Yes, and 'N' for No." << endl;
cin >> COM;
}
}
};
int main(){
IOutro IObject;
Arith ArithObject;
while (COM == 'Y' || COM == 'y' ){ // made changes here
IObject.welcome();
ArithObject.calc();
ArithObject.sum(A,B);
IObject.commandInput();
}
IObject.goodbye();
return 0;
}
请注意,对于此代码,您不需要 #include<string>
并且还使代码不区分大小写(即 'Y'
或 'y'
和 'N'
或 'n'
)
这将消除您遇到的令人讨厌的错误。
好吧,希望能解决问题(好吧,这对我来说已经解决了)