I
inkexit
I'm getting assertion errors when I try to import or export a file
using this code. Please help. I don't even know what an assertion
error is.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
const int MAX_SIZE = 50;
struct widget
{
char color;
int size;
float weight;
string material;
};
widget my_widget[MAX_SIZE];
void import_db(widget my_widget[MAX_SIZE], ifstream *input_file, int
*num_items)
{
int i = 0;
*num_items = 0;
while( !(*input_file).eof())
{
*input_file >> my_widget.color >> my_widget.size;
*input_file >> my_widget.weight >> my_widget.material;
//must check for EOF
if( !(*input_file).eof())
{
i++;
}
}
*num_items = i;
}
void export_db(widget my_widget[MAX_SIZE], ofstream *out_file, int
num_items)
{
int i;
for (i=0; i<num_items; i++)
{
(*out_file) << my_widget.color << my_widget.size <<
my_widget.weight << my_widget.material << endl;
}
}
void print_db(widget my_widget[MAX_SIZE], int *num_items)
{
for(int i=0; i < *num_items; i++)
{
cout << setiosflags(ios::left);
cout << setw(8) << my_widget.color << setw(6) <<
my_widget.size;
cout << setw(9) << my_widget.weight << setw(9) <<
my_widget.material << endl;
}
}
void add_item(widget my_widget[MAX_SIZE], widget *item, int *num_items)
{
//if user wants to add an item when the array is full, exit
if (*num_items >= MAX_SIZE)
{
cout << "\nSorry, too many items are in the database already.";
cout << "\nProgram now exiting...";
exit (1);
}
else
{
//add item struct to my_widget
my_widget[*num_items].color = (*item).color;
my_widget[*num_items].size = (*item).size;
my_widget[*num_items].weight = (*item).weight;
my_widget[*num_items].material = (*item).material;
(*num_items)++;
}
}
void del_item(widget my_widget[MAX_SIZE], int item_num, int *num_items)
{
int i;
//shift all items > item_num, up one position in my_widget
for (i=(item_num-1); i<*num_items-1; i++)
{
my_widget = my_widget[i+1];
}
(*num_items)--;
}
int main()
{
//declare my_widget and program variables
ofstream out_file;
ifstream input_file;
widget item;
char operation_choice = 'x', modify_choice, color_choice;
int size_choice, num_items = 0, item_num;
float weight_choice;
string material_choice, file_name;
//main switch statement of the program
//this is where all user interaction occurs
//this loop while continue until the user exits
do
{
cout << "\nDatabase operations: \ni) import database \ne) export
database";
cout << "\np) print database \nm) modify database \nq) quit \nChoice:
";
cin >> operation_choice;
switch (operation_choice)
{
case 'i':
{
//user chooses import
do
{
cout << "\nEnter the name of the file to import: ";
//grab the filename
getline(cin, file_name);
cin.ignore(INT_MAX,'\n');
//try to open the file
input_file.open(file_name.c_str());
}while(!input_file);
import_db(my_widget, &input_file, &num_items);
//close file
//from now on, all operations done on my_widget array in local
memory
input_file.close();
break;
}
case 'e':
{
//user chooses to export
cout << "\nEnter the name of the file to export: ";
//grab the filename
getline(cin, file_name);
cin.ignore(INT_MAX,'\n');
//open the file to write to
out_file.open(file_name.c_str());
//export my_widget using export_db function
export_db(my_widget, &out_file, num_items);
//close file
out_file.close();
break;
}
case 'p':
{
//user chooses to print, print my_widget
print_db(my_widget, &num_items);
break;
}
case 'm':
{
//user chooses to modify my_widget
cout << "\n(A)dd or (D)elete an item? ";
cin >> modify_choice;
cin.ignore(INT_MAX,'\n');
if (modify_choice == 'a' || modify_choice == 'A')
{
//user chooses to add an item. prompt user for data.
cout << "Color? ";
cin >> color_choice;
cin.ignore(INT_MAX,'\n');
cout << "Size? ";
cin >> size_choice;
cin.ignore(INT_MAX,'\n');
cout << "Weight? ";
cin >> weight_choice;
cin.ignore(INT_MAX,'\n');
cout << "Material? ";
cin >> material_choice;
cin.ignore(INT_MAX,'\n');
//store all the new info in a widget struct called item
item.color = color_choice;
item.size = size_choice;
item.weight = weight_choice;
item.material = material_choice;
//add new item to my_widget using add_item function.
add_item(my_widget, &item, &num_items);
}
else
{
//user chooses to delete an item, prompt user for the
//number of the item to delete.
cout << "Enter the item number to delete: ";
cin >> item_num;
cin.ignore(INT_MAX,'\n');
//actually delete the item from my_widget by using del_item
function
del_item(my_widget, item_num, &num_items);
}
break;
}
case 'q':
{
//user chooses to quit, quit program
cout << "exiting program...";
exit (1);
break;
}
}
}
while ( operation_choice != 'q' ); //if user does not choose to exit
//continue to ask user to do operations
return 0;
}
using this code. Please help. I don't even know what an assertion
error is.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
const int MAX_SIZE = 50;
struct widget
{
char color;
int size;
float weight;
string material;
};
widget my_widget[MAX_SIZE];
void import_db(widget my_widget[MAX_SIZE], ifstream *input_file, int
*num_items)
{
int i = 0;
*num_items = 0;
while( !(*input_file).eof())
{
*input_file >> my_widget.color >> my_widget.size;
*input_file >> my_widget.weight >> my_widget.material;
//must check for EOF
if( !(*input_file).eof())
{
i++;
}
}
*num_items = i;
}
void export_db(widget my_widget[MAX_SIZE], ofstream *out_file, int
num_items)
{
int i;
for (i=0; i<num_items; i++)
{
(*out_file) << my_widget.color << my_widget.size <<
my_widget.weight << my_widget.material << endl;
}
}
void print_db(widget my_widget[MAX_SIZE], int *num_items)
{
for(int i=0; i < *num_items; i++)
{
cout << setiosflags(ios::left);
cout << setw(8) << my_widget.color << setw(6) <<
my_widget.size;
cout << setw(9) << my_widget.weight << setw(9) <<
my_widget.material << endl;
}
}
void add_item(widget my_widget[MAX_SIZE], widget *item, int *num_items)
{
//if user wants to add an item when the array is full, exit
if (*num_items >= MAX_SIZE)
{
cout << "\nSorry, too many items are in the database already.";
cout << "\nProgram now exiting...";
exit (1);
}
else
{
//add item struct to my_widget
my_widget[*num_items].color = (*item).color;
my_widget[*num_items].size = (*item).size;
my_widget[*num_items].weight = (*item).weight;
my_widget[*num_items].material = (*item).material;
(*num_items)++;
}
}
void del_item(widget my_widget[MAX_SIZE], int item_num, int *num_items)
{
int i;
//shift all items > item_num, up one position in my_widget
for (i=(item_num-1); i<*num_items-1; i++)
{
my_widget = my_widget[i+1];
}
(*num_items)--;
}
int main()
{
//declare my_widget and program variables
ofstream out_file;
ifstream input_file;
widget item;
char operation_choice = 'x', modify_choice, color_choice;
int size_choice, num_items = 0, item_num;
float weight_choice;
string material_choice, file_name;
//main switch statement of the program
//this is where all user interaction occurs
//this loop while continue until the user exits
do
{
cout << "\nDatabase operations: \ni) import database \ne) export
database";
cout << "\np) print database \nm) modify database \nq) quit \nChoice:
";
cin >> operation_choice;
switch (operation_choice)
{
case 'i':
{
//user chooses import
do
{
cout << "\nEnter the name of the file to import: ";
//grab the filename
getline(cin, file_name);
cin.ignore(INT_MAX,'\n');
//try to open the file
input_file.open(file_name.c_str());
}while(!input_file);
import_db(my_widget, &input_file, &num_items);
//close file
//from now on, all operations done on my_widget array in local
memory
input_file.close();
break;
}
case 'e':
{
//user chooses to export
cout << "\nEnter the name of the file to export: ";
//grab the filename
getline(cin, file_name);
cin.ignore(INT_MAX,'\n');
//open the file to write to
out_file.open(file_name.c_str());
//export my_widget using export_db function
export_db(my_widget, &out_file, num_items);
//close file
out_file.close();
break;
}
case 'p':
{
//user chooses to print, print my_widget
print_db(my_widget, &num_items);
break;
}
case 'm':
{
//user chooses to modify my_widget
cout << "\n(A)dd or (D)elete an item? ";
cin >> modify_choice;
cin.ignore(INT_MAX,'\n');
if (modify_choice == 'a' || modify_choice == 'A')
{
//user chooses to add an item. prompt user for data.
cout << "Color? ";
cin >> color_choice;
cin.ignore(INT_MAX,'\n');
cout << "Size? ";
cin >> size_choice;
cin.ignore(INT_MAX,'\n');
cout << "Weight? ";
cin >> weight_choice;
cin.ignore(INT_MAX,'\n');
cout << "Material? ";
cin >> material_choice;
cin.ignore(INT_MAX,'\n');
//store all the new info in a widget struct called item
item.color = color_choice;
item.size = size_choice;
item.weight = weight_choice;
item.material = material_choice;
//add new item to my_widget using add_item function.
add_item(my_widget, &item, &num_items);
}
else
{
//user chooses to delete an item, prompt user for the
//number of the item to delete.
cout << "Enter the item number to delete: ";
cin >> item_num;
cin.ignore(INT_MAX,'\n');
//actually delete the item from my_widget by using del_item
function
del_item(my_widget, item_num, &num_items);
}
break;
}
case 'q':
{
//user chooses to quit, quit program
cout << "exiting program...";
exit (1);
break;
}
}
}
while ( operation_choice != 'q' ); //if user does not choose to exit
//continue to ask user to do operations
return 0;
}