C++ &引用;cin.get";停止另一个输入函数,如;cin"。。c++;

C++ &引用;cin.get";停止另一个输入函数,如;cin"。。c++;,c++,C++,我想向6个用户请求用户名和密码, 然后将此用户名和密码保存到我桌面上的文本文件中。 我构建了一个结构,其中有一个用于存储密码的变量和一个长度为25的char数组 struct users { int password; char username[25]; } 当我想在main()函数中请求用户名时,我使用cin.get()函数 cin.get(username,25); 当存储密码时,程序在第二次cin功能后停止。为什么呢 #include <iostream>

我想向6个用户请求用户名和密码, 然后将此用户名和密码保存到我桌面上的文本文件中。 我构建了一个结构,其中有一个用于存储密码的变量和一个长度为25的
char
数组

struct users {
    int password;
    char username[25];
}
当我想在
main()
函数中请求用户名时,我使用
cin.get()
函数

cin.get(username,25);
当存储密码时,程序在第二次
cin
功能后停止。为什么呢

#include <iostream>
#include <fstream> 

struct users {
    char username[25];
    int password;
} user[6]; 

using namespace std;
int main() {  
    cout << "Sign up x6" << endl;
    for(int i=0;i<6;i++){
        cout << "Username:";
        cin.get(user[i].username,20);
        cout << "Password:";
        cin >> user[i].password;
    }
    std::ofstream file;
    file.open("C:/Users/Programmer/Desktop/sa.txt",ios::app);
    for(int i=0;i<6;i++){
        file << "first user\n" << user[i].username << endl << user[i].password << endl;
        cout << "\n \n \n";
    }
}
#包括
#包括
结构用户{
字符用户名[25];
int密码;
}用户[6];
使用名称空间std;
int main(){

不能用std::cin.getline(用户[i].username,20)代替。

我找到了使用获取函数的解决方案 函数“获取”将请求值的用户,然后存储变量的值。(用户名)
字符用户名[25];
获取(用户名[25])

但是这个函数“获取”只获取“char”类型变量的值,所以我将“password”变量类型更改为char 字符密码[25];
获取(密码[25]);

#包括
#包括
#包括
结构用户
{
字符用户名[25];
字符密码[25];
}用户[6];
使用名称空间std;
int main()
{

严格来说,这不是一个重复,但迪特玛·库尔的回答解释了问题并提供了解决方案:我找到了解决方案。…@YasserA.ElShbrawy你能用你找到的解决方案回答你自己的问题吗?它将在将来帮助有同样问题的人。@SeanDawson Done。
谢谢你的帮助。。遭受几乎完全相同的pr问题在于询问者的代码。在
cin>>用户[i].password;
中断解析器后,“\n”很可能留在流中。在我这样做之后,cin.getline函数在“for loop”中只运行一次,因此询问我密码6次(true),但仅询问我用户名一次!(false)
    #include<iostream>
    #include <fstream> 
    #include<stdio.h>
struct users
{
char username[25];
char password[25];
} user[6];
using namespace std;
int main()
{
cout << "Sign up x6" << endl;
for (int i = 0; i < 6; i++)
{
    cout << "username:";
    gets(user[i].username);
    cout << "Password:";
    gets(user[i].password);
}
std::ofstream file;
file.open("C:/Users/Programmer/Desktop/sa.txt", ios::app);
for (int i = 0; i < 6; i++)
{
    file <<"("<<i<<") user\n" << user[i].username << endl <<  user[i].password<< endl;          
    cout << "\n \n \n";
}
}