473,221 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

Assertion Errors

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[i].color >> my_widget[i].size;
*input_file >> my_widget[i].weight >> my_widget[i].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[i].color << my_widget[i].size <<
my_widget[i].weight << my_widget[i].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[i].color << setw(6) <<
my_widget[i].size;
cout << setw(9) << my_widget[i].weight << setw(9) <<
my_widget[i].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[i] = 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;
}

Dec 9 '05 #1
7 2512
in*****@yahoo.com wrote:
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[i].color >> my_widget[i].size;
*input_file >> my_widget[i].weight >> my_widget[i].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[i].color << my_widget[i].size <<
my_widget[i].weight << my_widget[i].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[i].color << setw(6) <<
my_widget[i].size;
cout << setw(9) << my_widget[i].weight << setw(9) <<
my_widget[i].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[i] = 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;
}


Assertions are used to document the assumptions of the programmer. For
instance, you might have a function:

#include <cassert>

void Foo( int *const pi )
{
assert( 0 != pi );
*pi = 42;
}

This function requires that pi not be null on entry, and if the
assertion fails, the program terminates or something to that effect.

Where are you getting the error? What is the input sequence you use to
reproduce the problem?

Probably a pointer is null somewhere. Step through the code in your
debugger or add some extra print statements to identify which line(s)
cause the problem.

Cheers! --M

Dec 9 '05 #2

<in*****@yahoo.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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.


Assertion failures occur when an assert statement tests an statement which
returns false, such as:

assert (n > 0);

when n is actually 0 (or less, if unsigned).

Assert statements are used in debug (development) versions of software to
let you know you've got a design problem, such as allowing invalid data
(n==0) to be passed to a function that absolutely _requires_ valid data (n >
0).
You need to find _where_ in the code that assertion is raised. If the error
message you're seeing doesn't give you enough information to find it, then
use your debugger to find where it happens.

I do see one line that doesn't make sense to me (although I admit I'm not
up-to-date on streams):

do
{
....
//try to open the file
input_file.open(file_name.c_str());
}while(!input_file);

Shouldn't the test be whether input_file.open() succeeds or not?

One other thing I see in your code: you're using exit(1) when the user tries
to enter an item there's no room left for. That's not a clean way to handle
user errors at all. Why not let the user know they tried to do something
wrong, and simply prevent that specific action from doing anything? Why
crash the application?

-Howard

Dec 9 '05 #3
Okay, well I tried to run the visual studio debugger and it gave me
this .c file. I'm in way over my head here. I assume this is some
higher level code the compiler translates my .cpp fil into?

Anyway, the debugger said the erros is being caused by line 55. I have
added a comment to that line for easy identification.

/***
*fopen.c - open a file
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines fopen() and _fsopen() - open a file as a stream and
open a file
* with a specified sharing mode as a stream
*
************************************************** *****************************/

#include <cruntime.h>
#include <stdio.h>
#include <share.h>
#include <dbgint.h>
#include <internal.h>
#include <mtdll.h>
#include <file2.h>
#include <tchar.h>
#include <errno.h>

/***
*FILE *_fsopen(file, mode, shflag) - open a file
*
*Purpose:
* Opens the file specified as a stream. mode determines file
mode:
* "r": read "w": write "a": append
* "r+": read/write "w+": open empty for read/write
* "a+": read/append
* Append "t" or "b" for text and binary mode. shflag determines
the
* sharing mode. Values are the same as for sopen().
*
*Entry:
* char *file - file name to open
* char *mode - mode of file access
*
*Exit:
* returns pointer to stream
* returns NULL if fails
*
*Exceptions:
*
************************************************** *****************************/

FILE * __cdecl _tfsopen (
const _TSCHAR *file,
const _TSCHAR *mode
,int shflag
)
{
REG1 FILE *stream;
REG2 FILE *retval;

_ASSERTE(file != NULL);
_ASSERTE(*file != _T('\0')); /*THE ERROR IS WITH THIS LINE*/
_ASSERTE(mode != NULL);
_ASSERTE(*mode != _T('\0'));

/* Get a free stream */
/* [NOTE: _getstream() returns a locked stream.] */

if ((stream = _getstream()) == NULL) {
errno = EMFILE;
return(NULL);
}

#ifdef _MT
__try {
#endif /* _MT */

/* open the stream */
#ifdef _UNICODE
retval = _wopenfile(file,mode,shflag,stream);
#else /* _UNICODE */
retval = _openfile(file,mode,shflag,stream);
#endif /* _UNICODE */

#ifdef _MT
}
__finally {
_unlock_str(stream);
}
#endif /* _MT */

return(retval);
}
/***
*FILE *fopen(file, mode) - open a file
*
*Purpose:
* Opens the file specified as a stream. mode determines file
mode:
* "r": read "w": write "a": append
* "r+": read/write "w+": open empty for read/write
* "a+": read/append
* Append "t" or "b" for text and binary mode
*
*Entry:
* char *file - file name to open
* char *mode - mode of file access
*
*Exit:
* returns pointer to stream
* returns NULL if fails
*
*Exceptions:
*
************************************************** *****************************/

FILE * __cdecl _tfopen (
const _TSCHAR *file,
const _TSCHAR *mode
)
{
return( _tfsopen(file, mode, _SH_DENYNO) );
}

Dec 9 '05 #4
Yes. That is a good point. But I couldn't figure out how to break out
of a switch statement from a an if statement nested within it.

Dec 9 '05 #5
in*****@yahoo.com wrote:
Okay, well I tried to run the visual studio debugger and it gave me
this .c file. I'm in way over my head here. I assume this is some
higher level code the compiler translates my .cpp fil into?
No, that's a library function. Look at the call stack, go to _your_
function there and see what values your variables have. IOW, learn to
use your debugger.
Anyway, the debugger said the erros is being caused by line 55. I have
added a comment to that line for easy identification.

/***
*fopen.c - open a file
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines fopen() and _fsopen() - open a file as a stream and
open a file
* with a specified sharing mode as a stream
*
************************************************** *****************************/

#include <cruntime.h>
#include <stdio.h>
#include <share.h>
#include <dbgint.h>
#include <internal.h>
#include <mtdll.h>
#include <file2.h>
#include <tchar.h>
#include <errno.h>

/***
*FILE *_fsopen(file, mode, shflag) - open a file
*
*Purpose:
* Opens the file specified as a stream. mode determines file
mode:
* "r": read "w": write "a": append
* "r+": read/write "w+": open empty for read/write
* "a+": read/append
* Append "t" or "b" for text and binary mode. shflag determines
the
* sharing mode. Values are the same as for sopen().
*
*Entry:
* char *file - file name to open
* char *mode - mode of file access
*
*Exit:
* returns pointer to stream
* returns NULL if fails
*
*Exceptions:
*
************************************************** *****************************/

FILE * __cdecl _tfsopen (
const _TSCHAR *file,
const _TSCHAR *mode
,int shflag
)
{
REG1 FILE *stream;
REG2 FILE *retval;

_ASSERTE(file != NULL);
_ASSERTE(*file != _T('\0')); /*THE ERROR IS WITH THIS LINE*/
[...]


Did you supply 'fopen' with an empty c-string as the file name?

fopen("", "r");

would certainly do it.

V
Dec 9 '05 #6

<in*****@yahoo.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Yes. That is a good point. But I couldn't figure out how to break out
of a switch statement from a an if statement nested within it.


You said it yourself right there: "break". Break exits a loop or switch
statement (regardless if whether you're in a nested conditional statement).

In your future replies, please include the text to which you're replying (as
I've done here), or at least enough of it so that we know what you're
responding to, ok?

And check out the FAQ at:
http://www.parashift.com/c++-faq-lite/
-Howard

Dec 9 '05 #7
in*****@yahoo.com wrote:
Yes. That is a good point.


What is?

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Dec 9 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Todd Miller | last post by:
Hi, I recently discovered an assertion failure in the Python garbage collection system when scripts using our C extension (numarray) exit. The assertion is activated for Pythons configured using...
89
by: Radioactive Man | last post by:
In python 2.3 (IDLE 1.0.3) running under windows 95, I get the following types of errors whenever I do simple arithmetic: 1st example: >>> 12.10 + 8.30 20.399999999999999 >>> 1.1 - 0.2...
4
by: Morgan Leppink | last post by:
Hey all - We are running SQL 2000 with ALL available service packs, etc. applied. We just built a brand new database server, which has dual 2Ghz XEONs, 2GB memory, and the following disk...
1
by: Kapt. Boogschutter | last post by:
Does anyone know Why I'm getting this Error??? Debug Assertion Failed! File: daocore.cpp Line: 42 My code: CDaoDatabase dao; dao.Create("filename");
3
by: crispin.proctor | last post by:
Hi All, I have started getting Assertion Errors in SQL. It appears when I process a cube (Most of the time) Other SQL statements, usually with a join or 6 do the same thing. Whaving a scratch...
3
by: bill | last post by:
I firmly believe that it is always a bad idea to put code in a header file. Nothing pisses me off more than seeing function definitions in a ..h, and I recently was truly blessed :) to witness...
1
by: Timur Safin | last post by:
Hi All, Sorry if it is offtopic here, I wasn't able to find any more relevant group... I'm slowly approaching AMD64 build for our product (as my personal fun project). And after I ran that...
0
by: jpl78 | last post by:
Hi, I'm new in visual studio... I have an application that is running well in c++, using MFClasses. Now, I'm trying to design a windows form to start the code, but it gaves me a "debug assertion...
0
by: =?Utf-8?B?REx1ZWNr?= | last post by:
I am getting a debug assertion error that reads: Debug Assertion Failed! program E:\program files\internet explorer\iexplore,exe File: dbgheap.c Line: 1252 Expression:...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.