我正在尝试用c ++创建一个简单的疯狂libs程序,我想检查并查看用户输入的单词是否以元音开头,如果是,则更改单词前面的“a”,到一个“一个”。我已经能够存储第一个字符,但它不会与If语句中的其他字符进行比较。我这样做完全错了吗?
#include <string>
#include <iostream>
using namespace std;
int main() {
string adj_3;
string anN;
char firstChar;
// GETTING USER'S WORD
cout << "ADJECTIVE: " << endl;
getline(cin, adj_3);
// GETTING FIRST CHARACTER
firstChar = adj_3[0];
// SEEING IF IT'S A VOWEL (not working)
if(firstChar == ('a' || 'e' || 'i' || 'o' || 'u' || 'A' || 'E' || 'I' || 'O' || 'U')) {
anN = "n";
}
else {
cout << "not working" << endl;
}
cout << "I am having a" << anN << " " << adj_3 << " time at camp." << endl;
}
答案 0 :(得分:3)
||
运算符需要应用于两个参数,如下所示:
if (firstChar == 'a' || firstChar == 'e' || firstChar == 'i' || ...)
firstChar == 'a'
计算为布尔值。 firstChar == 'a' || firstChar == 'e'
获取由这两个操作产生的两个布尔值,并返回另一个布尔值,然后将其作为第一个参数提供给下一个||
操作。通过这种方式,您可以“链接”||
操作,直到其中一个为真,或直到它们全部为假。
有关示例和说明,请参阅here。
答案 1 :(得分:1)
hnefatl的答案是单向的。
你也可以使用没有break语句的switch case来检查元音。类似的东西:
switch(firstChar)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U': cout<<"Vowel";
}
除switch-case
之外,if-else ladder
优于 <module name="ConstantName">
<property name="id" value="ConstantNameMain"/>
<property name="severity" value="error"/>
<property name="applyToPrivate" value="false"/>
<property name="format" value="^[A-Z][A-Za-z0-9]*(_[A-Za-z0-9]+)*$"/>
</module>
<module name="ConstantName">
<property name="id" value="ConstantNameTest"/>
<property name="severity" value="error"/>
<property name="applyToPrivate" value="false"/>
<property name="format" value="^[A-Z][A-Za-z0-9]*(_[A-Z0-9]+)*$"/>
</module>
,如https://stackoverflow.com/a/1028463/6594779所述。
答案 2 :(得分:1)
逻辑运算符a==0 || b==1
结合了两个布尔表达式,例如: true
,如果两个操作数中的任何一个是'a'
,则返回true。如果您将true
之类的单个字符作为操作数传递,则会将其解释为'a'
,因为97
的值为97 != 0 => true
和('a' || 'e' || 'i' || 'o' || 'u' || 'A' || 'E' || 'I' || 'O' || 'U')
。因此,您的表达式true
始终为firstchar == (....)
,而firstchar == true
与false
相同,后者可能会if (firstChar == 'a' || firstChar == 'e' || firstChar == 'i' || ...)
。
你可以写......
if (strchr(firstChar, "aeiouAEIOU") != NULL)) ...
...或
Build command failed.
Error while executing process /opt/android-sdk/cmake/3.6.4111459/bin/cmake with arguments {-H/home/max/Documents/AndroidStudioProjects/HelloJNI1/app/src/main/cpp -B/home/max/Documents/AndroidStudioProjects/HelloJNI1/app/.externalNativeBuild/cmake/arm8Release/arm64-v8a -GAndroid Gradle - Ninja -DANDROID_ABI=arm64-v8a -DANDROID_NDK=/opt/android-sdk/ndk-bundle -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/home/max/Documents/AndroidStudioProjects/HelloJNI1/app/build/intermediates/cmake/arm8/release/obj/arm64-v8a -DCMAKE_BUILD_TYPE=Release -DCMAKE_MAKE_PROGRAM=/opt/android-sdk/cmake/3.6.4111459/bin/ninja -DCMAKE_TOOLCHAIN_FILE=/opt/android-sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-23 -DANDROID_TOOLCHAIN=clang}
-- Check for working C compiler: /opt/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
-- Check for working C compiler: /opt/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang -- broken
-- Configuring incomplete, errors occurred!
See also "/home/max/Documents/AndroidStudioProjects/HelloJNI1/app/.externalNativeBuild/cmake/arm8Release/arm64-v8a/CMakeFiles/CMakeOutput.log".
See also "/home/max/Documents/AndroidStudioProjects/HelloJNI1/app/.externalNativeBuild/cmake/arm8Release/arm64-v8a/CMakeFiles/CMakeError.log".
CMake Error at /opt/android-sdk/cmake/3.6.4111459/share/cmake-3.6/Modules/CMakeTestCCompiler.cmake:61 (message):
The C compiler
"/opt/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /home/max/Documents/AndroidStudioProjects/HelloJNI1/app/.externalNativeBuild/cmake/arm8Release/arm64-v8a/CMakeFiles/CMakeTmp
Run Build Command:"/opt/android-sdk/cmake/3.6.4111459/bin/ninja"
"cmTC_0053d"
[1/2] Building C object CMakeFiles/cmTC_0053d.dir/testCCompiler.c.o
FAILED:
/opt/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
--target=aarch64-none-linux-android
--gcc-toolchain=/opt/android-sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64
--sysroot=/opt/android-sdk/ndk-bundle/sysroot -isystem
/opt/android-sdk/ndk-bundle/sysroot/usr/include/aarch64-linux-android
-D__ANDROID_API__=23 -g -DANDROID -ffunction-sections -funwind-tables
-fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat
-Werror=format-security -fPIE -o
CMakeFiles/cmTC_0053d.dir/testCCompiler.c.o -c
/home/max/Documents/AndroidStudioProjects/HelloJNI1/app/.externalNativeBuild/cmake/arm8Release/arm64-v8a/CMakeFiles/CMakeTmp/testCCompiler.c
/opt/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang:
error while loading shared libraries: libncurses.so.5: cannot open shared
object file: No such file or directory
ninja: build stopped: subcommand failed.
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt
答案 3 :(得分:0)
您也可以使用数组,其中存储所有元音然后进行比较。如下所示:
char vowels[10]={'a','e','i','o','u','A','E','I','O','U'};
int flag=0;
for(int i=0;i<10;i++)
{
if(vowels[i]==firstChar)
{
flag=1;
anN="n";
}
}
if(flag==1)
cout << "I am having a" << anN << " " << adj_3 << " time at camp." << endl;
else
cout << "not working" << endl;