File I/O questions Xcode

ok so i need help i have no clue how to create a .txt. file and add it to the file because it seems to not exist. or when i do create a file that is readable and doesn't give me the error code of the indata file i cannot find the output if it was even created. or if the data was read from the in data please help me understand ALL CODING IS DONE IN XCODE FOR MAC'S






#include <iostream>
//add the library <--- #1
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{

int num;
int min=0;
int max=0;
int sum=0;
int count=0;
int i;

//Declare input/out pout file variables <--- #2
ifstream indata;
ofstream odddata, evendata, graph;

//open your input file <--- # 3
indata.open("numbers.txt");
if (indata.fail())
{
cout << " file does not exit\n";
exit(1);
}
//open your output file <--- # 3
odddata.open("OddNumbers.txt");
if (odddata.fail())
{
cout << " file does not exit\n";
exit(1);
}
evendata.open("EvenNumbers.txt");
if (evendata.fail())
{
cout << " file does not exit\n";
exit(2);
}
graph.open("Graph.txt");
if (graph.fail())
{
cout << " file does not exit\n";
exit(3);
}
//read from your input file (similar to cin) <--- #4
indata >> num;
max=min=num;

while (!indata.eof())
{
if(num != 0)
{
if(num %2 == 1)
odddata << num << endl;
else
evendata << num <<endl;
count++;
sum += num;
if (num > max)
max=num;
else if (num < min)
min = num;
//draw the bar graph for this data item
graph << num <<":\t";
for (i=0; i < num; i+=2)
{
graph << "*";
}
graph << endl;
}
//get the next number
indata >> num;
}

cout <<endl <<"Sum: "<<sum<< " Count: " << count<<" Max: " << max<<" Min: "<<min <<endl;

//close all your open files <--- #5
indata.close();
odddata.close();
evendata.close();
graph.close();

return 0;
}
Last edited on
Topic archived. No new replies allowed.