在输入结构对象的输入过程中运行时间错误
run time error during taking input of a structure object
首次迭代成功,但后来无法输入名称。另外,我听说过不使用Get,所以我也尝试了fgets,但也没有起作用。请帮助!
我正在用C 语言在CodeBlocks工作
struct stu
{
char n[25];
int r;
}s[3];
void getinfo()
{
for(int i=0;i<3;i++)
{
cout<<"name: ";
gets(s[i].n);
cout<<"R.no: ";
cin>>s[i].r;
}
}
int main()
{
getinfo();
for(int l=0;l<3;l++)
{
cout<<s[l].n;
cout<<s[l].r;
}
}
输出,正确获取第一次迭代,但下次无法输入名称
注意:
cin.getline()
- 用于从标准输入设备读取未形式的字符串(一组字符)。
提取操作员>>
不会删除行字符的末端,因此,如果将其与getline()
混合,则需要调用 cin.ignore(number,'n');
摆脱'n'
#include <iostream>
#include <limits>
struct stu
{
char n[25];
int r;
}s[3];
void getinfo()
{
for(int i=0; i<3; i++)
{
std::cout << "name: ";
std::cin.getline(s[i].n,25);
std::cout << "R.no: ";
std::cin >> s[i].r;
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), 'n');
}
}
int main()
{
getinfo();
std::cout << std::endl;
for(int l=0; l<3; l++)
{
std::cout << s[l].n << " ";
std::cout << s[l].r << std::endl;
}
return 0;
}
输出:
name: Henry the VIII
R.no: 1
name: Ronald Regan
R.no: 2
name: Adam K.
R.no: 3
Henry the VIII 1
Ronald Regan 2
Adam K. 3
相关文章:
- KMP算法和LPS表构造的运行时间
- 如何计算Big-O表示法中的平均渐近运行时间
- 用时钟测量程序的运行时间
- 如何根据用户在C++中的输入运行不同数量的 for 循环
- 如何减少代码的运行时间以对齐文本?
- 我的 c++ 应用程序中的运行时间从 0 增加到 60 太快了(例如一毫秒或一微秒)
- 使用 Qt 测量过程的实际运行时间
- 如何执行用 C++ 编写的程序的运行时间?
- 无限运行时间
- 如何优化C++键值程序以获得更快的运行时间
- 这个算法的运行时间复杂度是多少?你是如何分析的
- 两个嵌套循环的运行时间复杂性:二次型还是线性
- 重复值的最长运行时间
- 同一程序的运行时间差异很大,有时达到1000+us
- 测量 OpenCL 应用程序的运行时间
- 字母输入运行无限循环
- variadic函数模板:基于n编译时值在运行时间自动n输入
- 在输入结构对象的输入过程中运行时间错误
- 查找我的算法的运行时间,以查找输入在输入方面是否是素数
- 在运行时间内用户输入任何内容时,如何停止计时器