如何解决为什么某些字符串变量没有将输入从结构传递到void函数?
在下面的代码中,某些字符串正在接受输入,但有些只是在跳过。我检查了一切都没有发现任何东西。如果您让我知道我在哪里犯规,那将是理想的选择。我需要提及的是,在这个问题上我还有很多工作要做,这就是我创建函数的原因
预期产量
::::::::::::::::::::::::::: 从下面选择选项 ::::::::::::::::::::::::::
1.for添加新学生
2。添加新老师
1
输入学生的名字:结婚
输入学生的姓:lisa
输入学生名册:245
输入学生的部分:C
输入学生的年份:1
输入学生的学期:2
进入学生部门:IR
输入学生的电子邮件:lisa2@hotmail.com
输入学生的电话:15648955
输入学生的地址:XYZ街2 / A
立即输出
::::::::::::::::::::::::::: 从下面选择选项 ::::::::::::::::::::::::::
1。添加新学生
2。添加新老师
1
输入学生的名字:输入学生的名字:lisa
输入学生名册:245
输入学生部分:输入学生的年份:1
输入学生的学期:2
进入学生部门:输入学生的电子邮件:lisa2@hotmail.com
输入学生的电话:15648955
输入学生的地址:
进程返回0(0x0)执行时间:52.725 s 按任意键继续。
#include <iostream>
#include<string>
#include<iomanip>
using namespace std;
struct student{
string firstName;
string lastName;
int Roll;
string Section;
int Year;
int Semester;
string Department;
string Email;
int Phone;
string Address;
};
void student_part(void){
struct student stu;
cout<<"Enter student's first name :";
getline(cin,stu.firstName);
cout<<"Enter student's last name :";
getline(cin,stu.lastName);
cout<<"Enter student's roll no :";
cin>>stu.Roll;
cout<<"Enter student's section :";
getline(cin,stu.Section);
cout<<"Enter student's year :";
cin>>stu.Year;
cout<<"Enter student's semester :";
cin>>stu.Semester;
cout<<"Enter student's department :";
getline(cin,stu.Department);
cout<<"Enter student's email :";
getline(cin,stu.Email);
cout<<"Enter student's phone :";
cin>>stu.Phone;
cout<<"Enter student's address :";
getline(cin,stu.Address);
}
void add_info(void){
int choice;
cout<<"::::::::::::::::::::::::::"<<endl;
cout<<" Choose Option From Below"<<endl;
cout<<"::::::::::::::::::::::::::"<<endl;
cout<<"\n1.for add new student"<<endl;
cout<<"2.for add new teacher"<<endl;
cout<<"3.for add new notice"<<endl;
cin>>choice;
if(choice==1){
student_part();
}
}
int main()
{
add_info();
}
解决方法
cin>>stu.Phone;
正在从命令行获取一个整数,因此它不会在\n
之后立即读取的末尾读取getline
字符。
在getline
之前,写fgetc(stdin)
来读取newlîne。
另一种可能性是将所有getLine()
功能更改为cin>>stu...
通话。
发生错误,因为cin
方法没有将\n
从输入缓冲区中移出。接下来的getLine
从输入Buffer中取出其余的\n
,并立即终止。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。