C++ 创建一个orogram,其中代码必须检查CIN文本中是否有@
我将写一个小程序,用户在其中键入电子邮件地址,代码必须检查电子邮件是否包含@:我刚刚开始编程,这就是我到目前为止想到的:C++ 创建一个orogram,其中代码必须检查CIN文本中是否有@,c++,string,if-statement,find,C++,String,If Statement,Find,我将写一个小程序,用户在其中键入电子邮件地址,代码必须检查电子邮件是否包含@:我刚刚开始编程,这就是我到目前为止想到的: #include <iostream> #include <string> using namespace std; int main() { cout << "Skriv in en Epostadress: \n"; string adress; cin >> adress; if (adress == "
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Skriv in en Epostadress: \n";
string adress;
cin >> adress;
if (adress == "@"){
cout << "Giltig!";
}
else {
cout << "Ogiltig!";
}
return 0;
}
#包括
#包括
使用名称空间std;
int main(){
地址;
如果(地址=“@”){
不能使用std::string类的成员函数find
if ( adress.find( '@' ) != std::string::npos ){
cout << "Giltig!";
}
else {
cout << "Ogiltig!";
}
可能您的问题是,您正在将整个字符串与“@”进行比较,这对您来说从来都不是这样,
然而,如果您试图在输入字符串中找到“@”,那么您将需要使用for循环将其作为char查找,但是您非常幸运,您可以在c中将字符串作为字符数组进行遍历++
试试这个,也许:
for(int i=0; i<adress.size(); i++){
if (adress[i] == '@'){
cout << "Giltig!" << endl;
return 0;
}
}
cout << "Ogiltig!" << endl;
return 0;
for(int i=0;i
The character '@' is found at position 13
for(int i=0; i<adress.size(); i++){
if (adress[i] == '@'){
cout << "Giltig!" << endl;
return 0;
}
}
cout << "Ogiltig!" << endl;
return 0;