Some better indentation would be helpful:
Code:
#include <iostream.h> // Use <iostream> no ".h" instead
#include <string.h> // Use <cstring> no ".h" instead
#include <fstream.h> // Use <fstream> no ".h" instead
using namespace std; // Needed due to change in headers
void main () // main always returns an int
{
int numanswer;
int redo;
int i;
int k;
char answer,answer2, answer3, answer4, phone_search[20];
struct dvds_r_us
{
// It's usually better to work with string containers instead of C-style character arrays
char title[30];
char main_star[30];
char length[15];
char genre[30];
char renter_name[30];
char renter_number[20];
char cost[15];
};
dvds_r_us renter_search[100];
ofstream outfile;
ifstream infile;
cout << "Is this the first time you have run this program?(y/n)\n";
cin >> answer2;
if(answer2 == 'y' || answer2 == 'Y')
{
// ios::out is assumed for ofstream objects
outfile.open("renters.DAT",ios::out);
if (outfile)
{
// Major issue: Arrays are 0 based in C/C++, that means your loop should look like:
// ( k = 0; k < 100; ++k )
// You are currently writing to an index that does not exist
for (k = 1; k <= 100; k++)
{
// All these strcpy's are pointless, just write "Empty" directly to the file
strcpy(renter_search[k].title,"Empty");
strcpy(renter_search[k].main_star,"Empty");
strcpy(renter_search[k].length,"Empty");
strcpy(renter_search[k].genre,"Empty");
strcpy(renter_search[k].renter_name,"Empty");
strcpy(renter_search[k].renter_number,"Empty");
//strcpy(renter_search[k].cost,"Empty");
outfile << renter_search[k].title << endl;
outfile << renter_search[k].main_star << endl;
outfile << renter_search[k].length << endl;
outfile << renter_search[k].genre << endl;
outfile << renter_search[k].renter_name << endl;
outfile << renter_search[k].renter_number << endl;
//outfile << renter_search[k].cost << endl;
}
}
else
{
cout << "An error has occured opening the file.\n";
}
outfile.close();
}
do
{
answer3 = 0;
// ios::in is assumed for ifstream objects
infile.open("renters.DAT",ios::in);
if (infile)
{
// More array indexing problems
for (k = 1; k <= 100; k++)
{
infile.get(renter_search[k].title,30);
infile.ignore(80,'\n');
infile.get(renter_search[k].main_star,30);
infile.ignore(80,'\n');
infile.get(renter_search[k].length,15);
infile.ignore(80,'\n');
infile.get(renter_search[k].genre,30);
infile.ignore(80,'\n');
infile.get(renter_search[k].renter_name,30);
infile.ignore(80,'\n');
infile.get(renter_search[k].renter_number,20);
infile.ignore(80,'\n');
//infile.get(renter_search[k].cost,15);
//infile.ignore(80,'\n');
}
cout << "Do you wish to search for a renter by his/her phone number?(y/n)\n";
cin >> answer4;
if (answer4 == 'y' || answer4 == 'Y')
{
cout << "Enter the phone number of the person you are searching for\n";
cin >> phone_search;
cin.ignore(80, '\n');
// More array indexing problems
for (i = 1; i <= 100; i++)
{
if (strcmp(renter_search[i ].renter_number,phone_search) == 0)
{
cout << "The current statistics for the DVD number which is the " << i << " most popular." << endl;
cout << "1.(title) " << renter_search[i ].title << endl;
cout << "2.(main_star) " << renter_search[i ].main_star << endl;
cout << "3.(length) " << renter_search[i ].length << endl;
cout << "4.(genre) " << renter_search[i ].genre << endl;
cout << "5.(renter's name) " << renter_search[i ].renter_name << endl;
cout << "6.(renter's phone #) " << renter_search[i ].renter_number << endl;
//cout << "7.(cost) " << renter_search[i ].cost << endl;
}
}
}
if (answer4 == 'n' || answer4 == 'N')
{
cout << "Please enter the number with which you wish to view the stats of that DVD. \n";
cin >> i;
cout << "The current statistics for the DVD number which is the " << i << " most popular." << endl;
cout << "1.(title) " << renter_search[i ].title << endl;
cout << "2.(main_star) " << renter_search[i ].main_star << endl;
cout << "3.(length) " << renter_search[i ].length << endl;
cout << "4.(genre) " << renter_search[i ].genre << endl;
cout << "5.(renter's name) " << renter_search[i ].renter_name << endl;
cout << "6.(renter's phone #) " << renter_search[i ].renter_number << endl;
//cout << "7.(cost) " << renter_search[i ].cost << endl;
}
}
else
{
cout << "An error occurred while opening the file.\n";
}
infile.close();
cout << "Do you wish to change any of these stats?(y/n)\n";
cin >> answer;
if (answer == 'y' || answer == 'Y')
{
do
{
redo = 0;
cout << "Which statistic do you wish to change?(#)\n";
cin >> numanswer;
if (numanswer == 1)
{
cout << "Enter the new title for this movie.\n";
cin >> renter_search[i ].title;
}
if (numanswer == 2)
{
cout << "Enter the new main star for this movie.\n";
cin >> renter_search[i ].main_star;
}
if (numanswer == 3)
{
cout << "Enter the new length for this movie.\n";
cin >> renter_search[i ].length;
}
if (numanswer == 4)
{
cout << "Enter the new genre for this movie.\n";
cin >> renter_search[i ].genre;
}
if (numanswer == 5)
{
cout << "Enter the new renter for this movie.\n";
cin >> renter_search[i ].renter_name;
}
if (numanswer == 6)
{
cout << "Enter the new renter's phone number for this movie.\n";
cin >> renter_search[i ].renter_number;
}
if (numanswer == 7)
{
cout << "Enter the new cost for this movie.\n";
cin >> renter_search[i ].cost;
}
if (numanswer < 1 || numanswer > 7)
{
redo = 1;
}
} while (redo == 1);
}
// ios::out is assumed for ofstream objects
outfile.open("renters.DAT",ios::out);
// More array indexing problems
for (i = 1; i <= 100; i++)
{
if (outfile)
{
outfile << renter_search[i ].title << endl;
outfile << renter_search[i ].main_star << endl;
outfile << renter_search[i ].length << endl;
outfile << renter_search[i ].genre << endl;
outfile << renter_search[i ].renter_name << endl;
outfile << renter_search[i ].renter_number << endl;
//outfile << renter_search[i ].cost << endl;
}
else
{
cout << "An error has occured opening the file.\n";
}
}
outfile.close();
cout << "Do you wish to continue with this program?(y/n)";
cin >> answer3;
} while (answer3 != 'n');
return 0;
}
I stopped looking since there was quite a few basic things you already need to fix. Make those fixes, if you still are having your problems then repost your new code after making sure the code is nicely formated, (that's what the "Preview Post" button is for).