Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.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++ C++;_C++_Vector_Cin - Fatal编程技术网

C++ C++;

C++ C++;,c++,vector,cin,C++,Vector,Cin,这就是我的程序的工作原理。它提示用户输入,一旦检测到非数字,循环将停止。这是我的密码: int size = 0; float number; float total = 0; vector <float> data; //prompt user to enter file name string file; cout << "Enter a file name : " ; cin >> file ; //concatenate the file name

这就是我的程序的工作原理。它提示用户输入,一旦检测到非数字,循环将停止。这是我的密码:

int size = 0;
float number;
float total = 0;
vector <float> data;

//prompt user to enter file name
string file;
cout << "Enter a file name : " ;
cin >> file ;
//concatenate the file name as text file
file += ".txt";

//Write file
cout << "Enter number : ";
ofstream out_file;
out_file.open(file);
while(cin >> number)
{
    data.push_back(number);
    size++;
}

cout<< "Elements in array are : " ;
//check whether is there any 0 in array else print out the element in array
for (int count = 0; count < size; count++)
{
    if (data.size() == 0)
    {
        cout << "0 digit detected. " << endl;
        system("PAUSE");
    }else
    {
        //write the element in array into text file
        out_file << data.size() << " " ;
        cout << data.size() << " ";
    }
}
out_file.close();
int size=0;
浮点数;
浮动总数=0;
矢量数据;
//提示用户输入文件名
字符串文件;
cout>文件;
//将文件名连接为文本文件
文件+=“.txt”;
//写入文件
cout>数字)
{
数据。推回(数字);
大小++;
}

cout这条线就是你出错的地方:

out_file << data.size() << " " ;
out_file
for(int count=0;count不能,但我认为data.size()就是简单地获取整个向量并将其写入文本文件?我认为显示正确的方式会很有用:)耶耶耶,我刚才很困惑..大小就像java中的.length,它只给出数组/向量的大小。而且它通过数组中的数据进行nvr循环解决了这个问题。非常感谢你不需要跟踪你的大小self;
std::vector
为您做这件事。输出向量内容的惯用方法是
std::copy(data.begin(),data.end(),std::ostream_迭代器(out_file,”);
for (int count = 0; count < data.size(); count++) {
     if (data[count] == 0) {
         cout << "0 digit detected. " << endl;
         system("PAUSE");
     } else {
         //write the element in array into text file
         out_file << data[count] << " " ;
         cout << data[count] << " ";
     }
 }
 out_file.close();
std::vector<int> yourVector;

yourVector.push_back(1);
yourVector.push_back(3);

cout << "My vector size: " << yourVector.size() << endl; //This will give 2

cout << "My vector element: " << yourVector[0] << endl; //This will give 1