如何使变量等于 C++ 中的字母?
How to make a variable be equal to a letter in C++?
我尝试用 C++ 编写一个简单的程序,因为我是初学者。该程序应该告诉您 "swag value",因此如果您在控制台中输入除 "Nicu" 以外的任何内容,这是我一位朋友的名字,该程序将显示 "Swag level over 9000"。如果你写 "Nicu",虽然它会说 "Sorry, couldn't find swag in the database"。我的问题是如何让程序在我写 "Nicu" 时给我答案?以下是我对程序的看法:
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
char b,a;
a='Nicu';
cout<<"Insert your name: ";
cin>>b;
if (b==a){
cout<<"Sorry ! Couldn't find swag in database... "<<endl;
}
else if (b!=a){
cout<<"Swag level over 9000 "<<endl;
}
system ("PAUSE");
return 0;
}
这是它给我的错误:[Error]Id 返回了 1 个退出状态,它突出显示了代码的 a='Nicu'
部分。
尽管我在这段代码中使用了 char
,但我仍然不知道它的作用,但至少我确定 int
不能与字母一起使用。
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string b, a;
a = "Nicu";
cout<<"Insert your name: ";
cin>>b;
if (b==a){
cout<<"Sorry ! Couldn't find swag in database... "<<endl;
}
else if (b!=a){
cout<<"Swag level over 9000 "<<endl;
}
system ("PAUSE");
return 0;
}
char
用于存储单个字符。
您可以使用std::string
来存储字符串。
重写,
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
string a = "Nicu";
string b;
cout<<"Insert your name: ";
cin>>b;
if (b==a){
cout<<"Sorry ! Couldn't find swag in database... "<<endl;
}
else if (b!=a){
cout<<"Swag level over 9000 "<<endl;
}
system ("PAUSE");
return 0;
}
为什么不使用字符串而不是字符??
要比较字符串,您应该使用 "strcomp"
#include <stdio.h>
#include <string.h>
int main()
{
string other;
string that;
if(strcmp(other, "hi")) // comparing the characters in this string with the text possibility "hi."
{
// do whatever you want if they are equal.
}
//Or.
if(strcmp(other, that)) // comparing both strings rather than a string with a possibly extracted text to compare it with alongside another string comparing both strings at once.
{
// do whatever you want if they are equal.
}
}
我尝试用 C++ 编写一个简单的程序,因为我是初学者。该程序应该告诉您 "swag value",因此如果您在控制台中输入除 "Nicu" 以外的任何内容,这是我一位朋友的名字,该程序将显示 "Swag level over 9000"。如果你写 "Nicu",虽然它会说 "Sorry, couldn't find swag in the database"。我的问题是如何让程序在我写 "Nicu" 时给我答案?以下是我对程序的看法:
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
char b,a;
a='Nicu';
cout<<"Insert your name: ";
cin>>b;
if (b==a){
cout<<"Sorry ! Couldn't find swag in database... "<<endl;
}
else if (b!=a){
cout<<"Swag level over 9000 "<<endl;
}
system ("PAUSE");
return 0;
}
这是它给我的错误:[Error]Id 返回了 1 个退出状态,它突出显示了代码的 a='Nicu'
部分。
尽管我在这段代码中使用了 char
,但我仍然不知道它的作用,但至少我确定 int
不能与字母一起使用。
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string b, a;
a = "Nicu";
cout<<"Insert your name: ";
cin>>b;
if (b==a){
cout<<"Sorry ! Couldn't find swag in database... "<<endl;
}
else if (b!=a){
cout<<"Swag level over 9000 "<<endl;
}
system ("PAUSE");
return 0;
}
char
用于存储单个字符。
您可以使用std::string
来存储字符串。
重写,
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
string a = "Nicu";
string b;
cout<<"Insert your name: ";
cin>>b;
if (b==a){
cout<<"Sorry ! Couldn't find swag in database... "<<endl;
}
else if (b!=a){
cout<<"Swag level over 9000 "<<endl;
}
system ("PAUSE");
return 0;
}
为什么不使用字符串而不是字符?? 要比较字符串,您应该使用 "strcomp"
#include <stdio.h>
#include <string.h>
int main()
{
string other;
string that;
if(strcmp(other, "hi")) // comparing the characters in this string with the text possibility "hi."
{
// do whatever you want if they are equal.
}
//Or.
if(strcmp(other, that)) // comparing both strings rather than a string with a possibly extracted text to compare it with alongside another string comparing both strings at once.
{
// do whatever you want if they are equal.
}
}