I need some help with my code here. I am using file in out now, and it is not working as I would want it to. I have mess with the code a bit so if you see some funky stuff that is why.
It will save the item to the (item_name)file fine. But it wont save anything to the "database" or "maindat"...
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
ofstream outfile;
ifstream infile;
void auction_item(), auction_bid(), auctionsave(int ta), auctionsavepre(int ta), totalauctions(int ta), auctionread(int ta);
char item_name[25], item_description[250];
char total[25], maindat[25];
float item_price;
int ta;
int choice, itemammount;
int main()
{
cout << "Welcome to auctioneer, an auctioning program by Sam Evertson.\n";
cout << "This program will accept any auctions that you the end user will input.\n";
cout << "Once you input them it will store it onto a database and allow other users\n";
cout << "to bid on it.\n";
cout << "First please tell me if you are auctioning an item, or bidding on one.\n";
cout << "Enter (1) for auctioning an item, and (2) for bidding.\n";
cin >> choice;
switch (choice)
{
case 1:
auction_item();
break;
case 2:
auction_bid();
break;
}
return 0;
}
void auction_item()
{
cin.ignore();
cout << "You have chosen to auction an item, and are putting it into the " << maindat << " database, please reply with the items name. (Such as: Model Plane, or Ford F50) (Max of 25 Characters).\n";
cin.getline(item_name,25);
cout << "Now please briefly describe the item. (Max of 250 characters).\n";
cin.getline(item_description,250);
cout << "Now please state the price. (Please use a format of 0.00) (Leave out all other symbols such as Doller signs ($).\n";
cin >> item_price;
cin.ignore(90,'\n');
outfile.open(item_name,ios::out);
if (outfile)
{
outfile << item_name << endl;
outfile << item_description << endl;
outfile << item_price << endl;
outfile.close();
}
else
{
cout << "I am sorry, but the file failed to open.\n";
}
auctionread(ta);
auctionsavepre(ta);
auctionsave(ta);
cin.get();
cout << "You have successfully stored the data of your auctioned item into the database, here is the info that you gave us:\n";
infile.open(item_name,ios::in);
if (infile)
{
cout << "Item Name: ";
infile.getline(item_name, 25);
cout << item_name;
cout << "\n";
cout << "Item Description: ";
infile.getline(item_description, 250);
cout << item_description;
infile >> item_price;
cout << "\n";
cout << "Item Price: $";
cout << item_price;
infile.clear();
cout << "\n";
}
else
{
cout << "We have failed to open up the Auction Items file.\n";
}
}
void auction_bid()
{
cout << "You have stated that you wish to bid on an item, we currently have "<< ta << " item(s) in our database.\n";
}
void auctionsavepre(int ta)
{
infile.open("database",ios::in);
if (infile)
{
ta++;
infile.clear();
}
else
{
cout << "We could not open the database\n";
}
}
void auctionsave(int ta)
{
outfile.open("database",ios::out);
if (outfile)
{
outfile << ta;
outfile.clear();
}
else
{
cout << "We could not open the database\n";
}
}
void auctionread(int ta)
{
infile.open("database",ios::in);
if (infile)
{
infile >> ta;
infile.clear();
}
else
{
cout << "We could not open the database\n";
}
}
