help with breaking up an integer into an array and overwritng a file

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;

int main()
{
const int stid = 20102265;


string ifilename, ofilename, line;
ifstream inFile, checkOutFile;
ofstream outFile;
char response;
// Input file
cout << "Please enter the name of the file you wish to open : ";
cin >> ifilename;
inFile.open(ifilename.c_str());
if(inFile.fail())
{
cout << "The file " << ifilename << " was not successfully opened." << endl;
cout << "Please check the path and name of the file. " << endl;
exit(1);
}
else
{
cout << "The file is successfully opened." << endl;
}
// Output file
cout << "Please enter the name of the file you wish to write : ";
cin >> ofilename;
checkOutFile.open(ofilename.c_str());
if(!checkOutFile.fail())
{
cout << "A file " << ofilename << " exists.\nDo you want to continue and overwrite it? (y/n) : ";
cin >> response;
if(tolower(response) == 'n')
{
cout << "The existing file will not be overwritten. " << endl;
exit(1);
}
}
outFile.open(ofilename.c_str());
if(outFile.fail())
{
cout << "The file " << ofilename << " was not successfully opened." << endl;
cout << "Please check the path and name of the file. " << endl;
exit(1);
}
else
{
cout << "The file is successfully opened." << endl;
}
// Copy file contents from inFile to outFile
while(getline(inFile, line))
{
cout << line << endl;
outFile << line << endl;



}
{
cout << "Student ID : " << stid << endl;
char array[stid]; *****THIS IS WHERE IM CONFUSED*****
}

// Close files
inFile.close();
outFile.close();
} // main






how do i make the stid into an array. i know i need to do % to break the 8 integer up into 3 numbers but i'm confused
Last edited on
Are you asking how to turn an integer into an array of char?
When i overwrite input11.txt to output11.txt i need creat an array for those 7 integer numbers on the output.11txt file then i need to creat and store my 8 number student id into an array so i can put it back into output11.txt

I guess what im having trouble on is how to create an array mid-file
i need to creat and store my 8 number student id into an array so i can put it back into output11.txt


Why do you need to change the number into an array of char first? Why can you not just write the number straight to file?
1
2
int x = 35275637;
outFile << x;
you're right i did what you said and set it and did % ect to break up the stid integer

i needed to create a counter in the

while(getline(inFile, line))
{
cout << line << endl;
outFile << line << endl;

so it would rewrite it back into the outpu11.txt file

so now i have while(getline(inFile, line))
{
cout << line << endl;
outFile << line << endl;
for( s1 = 0; s1 < size; s1++)
s1=0;
for( s2 = 0; s2 < size; s2++)
s2=0;
for (s3 = 0; s2 < size; s3++)
s3=0;
cout << "Student ID : " << stid << endl;
s1 = stid / 100000;
s2 = stid % 100000 / 1000;
s3 = stid % 1000;
ary[0]= 2;
ary[1]= 35;
ary[2]= 96;
ary[3]= 168;
ary[4]= 487;
ary[5]= 791;
ary[6]= 890;
ary[7]= s1;
ary[8]= s2;
ary[9]= s3;
cout << line << endl;
outFile << line << endl;


but the counter isnt working ..?

it goes into an infinite loop and is not posting ary[1] ect ect
Last edited on
Topic archived. No new replies allowed.