Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
输入函数有什么问题? 对于我的第二个C++类,我们必须编写一个程序,创建一个5个结构和一个类的数组。结构中有一个整数、一个5个双精度的数组和一个81个元素的字符数组。当然,其中一个功能是接收用户的信息。该函数称为setStructData():_C++_Arrays_Input_Struct - Fatal编程技术网

输入函数有什么问题? 对于我的第二个C++类,我们必须编写一个程序,创建一个5个结构和一个类的数组。结构中有一个整数、一个5个双精度的数组和一个81个元素的字符数组。当然,其中一个功能是接收用户的信息。该函数称为setStructData():

输入函数有什么问题? 对于我的第二个C++类,我们必须编写一个程序,创建一个5个结构和一个类的数组。结构中有一个整数、一个5个双精度的数组和一个81个元素的字符数组。当然,其中一个功能是接收用户的信息。该函数称为setStructData():,c++,arrays,input,struct,C++,Arrays,Input,Struct,是文本文件把我的程序搞砸了,因为它的字符数组在完全不同的行上。我相信这只是我输入数据的方式,但我不知道如何修复它。有人能帮我吗? 当我使用I/O重定向来使用test.txt文件输入数据时,我让它输出回第一行(没有字符串),然后剩下的就是一堆疯狂的垃圾。请帮帮我!d首先,您应该始终验证您的输入是否成功!也就是说,始终在读取流后检查流是否仍处于良好状态,例如: if (!(std::cin >> StructArray[i].m_dArray[j])) { std::cout &

是文本文件把我的程序搞砸了,因为它的字符数组在完全不同的行上。我相信这只是我输入数据的方式,但我不知道如何修复它。有人能帮我吗?
当我使用I/O重定向来使用test.txt文件输入数据时,我让它输出回第一行(没有字符串),然后剩下的就是一堆疯狂的垃圾。请帮帮我!d

首先,您应该始终验证您的输入是否成功!也就是说,始终在读取流后检查流是否仍处于良好状态,例如:

if (!(std::cin >> StructArray[i].m_dArray[j])) {
    std::cout << "failed to read double value\n";
}
就我个人而言,我更喜欢使用
std::getline(std::cin>>std::ws,str)
,第二个参数的类型是
std::string
,但您的赋值似乎不允许使用
std::string
类。操纵器
std::ws
读取所有前导空格

void Prog1Class::getStructData(int index, Prog1Struct *struct_ptr)
{
    struct_ptr=&StructArray[index];
    cout<<struct_ptr->m_iVal<<" ";
    for(int i=0; i<5; i++)
    {
        cout<<struct_ptr->m_dArray[i]<<" "; 
    }
    cout<<struct_ptr->m_sLine<<endl;
}
10 1.2 2.3 3.4 4.5 5.6 
Test string 1
20 2.3 3.4 4.5 5.6 6.7 
Test string 2
30 3.4 4.5 5.6 6.7 7.8 
Test string 3
40 4.5 5.6 6.7 7.8 8.9 
Test string 4
50 5.6 6.7 7.8 8.9 9.1 
Test string 5
if (!(std::cin >> StructArray[i].m_dArray[j])) {
    std::cout << "failed to read double value\n";
}
if (!(std::cin >> std::setw(sizeof(StructArray[i].m_sLine)) >> StructArray[i].m_sLine)) {
    std::cout << "failed to read string\n";
}
if (!(std::cin >> std::ws).getline(StructArray[i].m_sLine, sizeof(StructArray[i].m_sLine)) {
    ...
}