C++;密码程序,遇到空格问题 #包括 #包括 #包括//不介意 #包括//也不要介意 使用名称空间std; int main(){ const string password=“hello”//密码 char word[100];//如果这是字符串,我在密码中输入两个单词,它将输出两次“无效密码” 做{ cin>>单词; cin.getline(word,100); 如果(word==password){//如果word等于password,则中断并继续通过循环 打破 } 我很高兴我已经解决了你的问题,但让我详细说明一下我之前所说的,因为这将有助于学习 #include <iostream> #include <string> #include <sstream> // don't mind this #include <ctime> //don't mind this either using namespace std; int main() { const string password = "hello"; //password char word[100]; //if this were string and I put 2 words in my password it would output "invalid password" twice do { cin >> word; cin.getline(word, 100); if (word == password) { //if word is equal to password, break and move on past the loop break; } cout << "Password invalid" << endl; //password not equal, it will move on to this and repeat until password is true } while (true); cout << "Password valid" << endl; return 0; }

C++;密码程序,遇到空格问题 #包括 #包括 #包括//不介意 #包括//也不要介意 使用名称空间std; int main(){ const string password=“hello”//密码 char word[100];//如果这是字符串,我在密码中输入两个单词,它将输出两次“无效密码” 做{ cin>>单词; cin.getline(word,100); 如果(word==password){//如果word等于password,则中断并继续通过循环 打破 } 我很高兴我已经解决了你的问题,但让我详细说明一下我之前所说的,因为这将有助于学习 #include <iostream> #include <string> #include <sstream> // don't mind this #include <ctime> //don't mind this either using namespace std; int main() { const string password = "hello"; //password char word[100]; //if this were string and I put 2 words in my password it would output "invalid password" twice do { cin >> word; cin.getline(word, 100); if (word == password) { //if word is equal to password, break and move on past the loop break; } cout << "Password invalid" << endl; //password not equal, it will move on to this and repeat until password is true } while (true); cout << "Password valid" << endl; return 0; },string,visual-c++,char,passwords,spaces,String,Visual C++,Char,Passwords,Spaces,此处[^\n]是scanf函数的扫描集说明符,这意味着scanf函数将扫描stdin中的字符,直到到达换行符。这基本上是一个c函数 cpp中的并行程序是getline(stream,char*)函数,但是几乎所有的C代码都在C++中工作,所以可以使用.< /P>因为字符串不是C++中的内置数据类型,所以我们使用C++包含在C++程序中使用字符串类。我们将字符串变量声明为字符串S;把输入作为“CIN>s”,但是这只会把字符串取到第一个空白。字符串输入函数getline作为字符串s;getline(

此处[^\n]是scanf函数的扫描集说明符,这意味着scanf函数将扫描stdin中的字符,直到到达换行符。这基本上是一个c函数


cpp中的并行程序是getline(stream,char*)函数,但是几乎所有的C代码都在C++中工作,所以可以使用.< /P>因为字符串不是C++中的内置数据类型,所以我们使用C++包含在C++程序中使用字符串类。我们将字符串变量声明为字符串S;把输入作为“CIN>s”,但是这只会把字符串取到第一个空白。字符串输入函数getline作为字符串s;getline(cin,s);您可以始终使用scanf(“%[^\n]%*c”,string);输入带空格的字符串。非常感谢!这很有效!
scanf("%[^\n]%*c", string );