lundi 16 février 2015

Dev C++ output to text file


#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;

int main()
{
ofstream sisend2;
sisend2.open("sisend2.txt");

srand(time(NULL));

long A;
cout << "Insert an amount in numbers: ";
cin>> A;

long B;
cout << "Insert the maximum value of the numbers inside the amount of numbers: ";
cin >> B;

vector<int> R;
long Q = rand() %(B);
R.push_back(Q);

for(int I = 2;I <= A; I++)
{
long S = rand() %B + 1;
R.push_back(S);
}

for(vector<int>::iterator I = R.begin(); I != R.end(); I++)
{
cout << *I << " | ";
sisend2 << *I << " | ";
sisend2.close();
}
}


What I am tring to do is to insert the randomly generated numbers into the sisend2.txt file, however this code only inserts the first number and doesnt go beyond that. It displays all of the numbers correctly in the Console, but doesnt write them into the text file beyond the first one.


What am I missing/doing wrong? I am a total beginner to C++ and not a native English speaker.





Aucun commentaire:

Enregistrer un commentaire