homework help

This is a discussion on homework help within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to run this code for a homework assignment for my c-programming class and I keep getting ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    1

    homework help

    Hi, I am trying to run this code for a homework assignment for my c-programming class and I keep getting this error when it tries to run....

    Debug Assertion Failed

    Expression: (stream !=NULL)

    I am not sure what to do at all, so any help would be appreciated.

    Here is my code, with the attached sample text file for the homework:

    insert
    Code:
     
    
    /*Josh Jackson*/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h> //needed for toupper
    #include <string.h>
    
    #define SIZE 100
    #define LENGTH 25
    
    int mainMenu ();
    void mainMenu_ver2 (int *);
    void mainMenu_verBad (int );
    
    typedef struct
    {
    	int accountArray;
    	double balanceArray;
    	char name[LENGTH];
    } customer;
    
    //--------------------------
    
    char bankerMenu ();
    char customerMenu ();
    
    /*void  bankerSide (int accountArray[], double balanceArray[], char name[][25]);
    void  customerSide(int accountArray[], double balanceArray[], char name[][25]);*/
    
    void  bankerSide (customer A[]);
    void  customerSide(customer A[]);
    
    
    void mainBanner ();
    void customerBanner();
    void bankerBanner ();
    
    //---------------------------new items
    
    void sort (customer A[]);
    void printAll ( customer A[] );
    
    
    //-----------------------------
    
    //****************************
    void addNewAccount ( customer A[] );
    void deleteAccount ( customer A[] );
    void accountSearch ( customer A[]  );
    void nameSearch ( customer A[] );
    
    
    
    //**********************
    void withdraw ( customer A[] );
    void deposit ( customer A[] );
    void checkBalance ( customer A[] );
    
    
    
    //------------------------new items
    int valid ( int index  , int *arr );
    void clean ( char [] ); 
    
    
    
    int main ()
    
    {
    
    int mainSelection;
    
    	int index;
    
    FILE *stream = fopen ("bankData.txt", "r");
    
    customer user[SIZE] ={{0,0, ""}};
    
    int counter = 0;
    
    	 int account ;
       double balance;
       char name[LENGTH];
       
       
       
     fscanf (stream, "%d", &account );
     
     
     while ( !feof (stream) )
     {
     
             fscanf (stream, "%lf", &balance );
            
             fgets(name, 25, stream);
    
    		 clean (name);
             
             user[counter].accountArray = account;
             user[counter].balanceArray = balance;
             strcpy ( user[counter].name, name);
             counter ++;     
             
             
             fscanf (stream, "%d", &account );
    
    }
    
     printAll (user);
    
    
    	
    	
    	do
    	{
    		mainSelection = mainMenu ();
    
    		switch ( mainSelection )
    		{
    		case 9 :
    			for ( index= 0; index <SIZE; index++)
    			{
    				printf ("%5d, %6d, %-10.2lf\n",  user);
    			}
    
    			printf ("\n\n");
    			break;
    
    
    				case 1 : customerSide(user);
    					break;
    				case 2 : bankerSide(user);
    					break;
    				case 0 : printf ("Thank you, and bye!\n\n");
    					break;
    				default : printf ("Invalid main selection!\n\n");
    					break;
    
    		}
    
    
    
    	}while ( mainSelection != 0);
    
    	
    
    	system("pause");
    	return 0;
    }
    //--------------
    int mainMenu ()
    {
    	int option; //local variable
    
    	mainBanner();
    	printf ("\tEnter \n");
    	printf ("\t\t0-Exit\n");
    	printf ("\t\t1-Customer\n");
    	printf ("\t\t2-Banker\n");
    	printf ("\t\t\t9-(TEMP. OPTION) SHOW ALL\n\n");
    
    	printf ("\t\t\tPick your choice :");
    	scanf ("%i", &option);
    
    	return option;
    
    }
    
    //----------------------
    
    char bankerMenu ()
    {
    	char temp;
    
    	bankerBanner ();
    	printf ("\tEnter\n");
    	printf ("\t\tA-Add New Account\n");
    	printf ("\t\tB-Close Account\n");
    	printf ("\t\tC-Search by account\n");
    	printf ("\t\tD-Search by name\n");
    	printf ("\t\tE-Display all customers\n");
    	printf ("\t\tF-Return Main Menu\n\n");
    
    	printf ("\t\t\tPick your choice :");
    	scanf (" %c", &temp);
    	return temp;
    }
    //-------------------
    char customerMenu ()
    {
    
    	char temp;
    
    	customerBanner ();
    	printf ("\tEnter\n");
    	printf ("\t\t1-Withdraw\n");
    	printf ("\t\t2-Deposit\n");
    	printf ("\t\t3-Balance\n");
    	printf ("\t\t0-Return Main Menu\n\n");
    
    	printf ("\t\t\tPick your choice :");
    	scanf (" %c", &temp);
    	return temp;
    
    }
    //-------------
    
    void  bankerSide ( customer A[] )
    {
    	char bankerSelection;
    
    	do
    	{
    		bankerSelection = bankerMenu ();
    
    		bankerSelection = toupper (bankerSelection);
    
    		switch ( bankerSelection )
    		{
    				case 'A' : addNewAccount (A); 
    					break;
    
    				case 'B' : deleteAccount (A);
    					break;
    
    				case 'C' : accountSearch (A);
    					break;
    
    					case 'D' : nameSearch (A);
    				    break;
    
    				case 'E' : sort;
    					break;
    
    				case 'F':
    					break;
    
    				default : printf ("Invalid banker selection!\n\n");
    					break;
    
    		}
    
    	}while ( bankerSelection != 'F');
    
    
    
    }
    //---------------------------------------
    
    
    
    
    void  customerSide ( customer A[] )
    {
    	char customerSelection;
    
    	do
    	{
    		customerSelection = customerMenu ();
    
    
    
    		switch ( customerSelection )
    		{
    				case '1' : withdraw (A);
    					break;
    
    				case '2' : deposit (A);
    					break;
    
    				case '3' : checkBalance (A);
    					break;
    
    				case '0':
    					break;
    
    				default : printf ("Invalid customer selection!\n\n");
    					break;
    
    		}
    
    	}
    	while ( customerSelection != '0');
    
    
    
    }
    
    
    //-------------------
    void addNewAccount ( customer A[] )
    {
    	int index = 0;
    
    	while (A[index] .accountArray != 0 && index<SIZE )
    	{
    			index++;
    	}
    
    	printf ("Enter the account number and the starting balance :");
    	scanf ("%d%lf", &A[index].accountArray, &A[index].balanceArray);
    
    	fflush (stdin);
    	printf ("Enter customer's name:");
    	gets (A[index].name);
    
    	printf ("Customer added!\n\n");
    
    
    }
    //-----
    
    void deposit ( customer A[] )
    
    {	int account;
    	int index , found = -1;
    	double amount = 0;
    
    
    
    	printf ("Enter the account number :");
    	scanf ("%d", &account);
    
    
    	index = 0;
    
    	while ( index < SIZE )
    	{
    		if ( A[index].accountArray == account )
    		{
    			found = index;
    			break;
    		}
    		index++;
    	}
    
    	if ( found == -1 ) printf ("Sorry, invalid account\n\n");
    	else
    	{
    		printf ("Enter the amount to be deposited :");
    		scanf ("%lf", &amount);
        A[index].balanceArray+= amount;
        }
    }
    
    
    //----------------------
    
    void withdraw ( customer A[] )
    {
    
    	int account;
    	int index , found = -1;
    	double amount = 0;
    
    
    
    	printf ("Enter the account number :");
    	scanf ("%d", &account);
    
    
    	index = 0;
    
    	while ( index < SIZE )
    	{
    		if ( A[index].accountArray == account )
    		{
    			found = index;
    			break;
    		}
    		index++;
    	}
    
    	if ( found == -1 ) printf ("Sorry, invalid account\n\n");
    	else
    	{
    		printf ("Enter the amount to be withdrawn :");
    		scanf ("%lf", &amount);
    
    		if ( amount > A[index].balanceArray ) printf ("Not enough fund!\n\n");
    		else A[index].balanceArray -= amount;
    
    	}
    
    
    
    }
    //-------------------------------------------
    
    void checkBalance ( customer A[] )
    {
    
    	int account;
    	int index , found = -1;
    	double amount = 0;
    
    
    
    	printf ("Enter the account number :");
    	scanf ("%d", &account);
    
    
    	index = 0;
    
    	while ( index < SIZE )
    	{
    		if ( A[index].accountArray == account )
    		{
    			found = index;
    			break;
    		}
    		index++;
    	}
    
    	if ( found == -1 ) printf ("Sorry, invalid account\n\n");
    	else
    	{
    		printf ("the balance is %.2lf\n",A[index].balanceArray);
    
    
    	}
    
    
    
    }
    
    //------------------------------
    void mainBanner ()
    {
    
    	printf ("\n\t* * * M A I N   M E N U * * * \n");
    
    }
    
    
    void customerBanner()
    {
    	printf ("\n\t* * * C U S T O M E R   M E N U * * * \n");
    
    }
    void bankerBanner ()
    {
    	printf ("\n\t* * * B A N K E R   M E N U * * * \n");
    }
    
    //-------------------------------
    void deleteAccount(customer A[])
    
    {
    
    	int account;
    	int index , found = -1;
    
    
    
    	printf ("Enter the account number :");
    	scanf ("%d", &account);
    
    
    	index = 0;
    
    	while ( index < SIZE )
    	{
    		if ( A[index].accountArray == account )
    		{
    			found = index;
    			break;
    		}
    		index++;
    	}
    
    	if ( found == -1 ) printf ("Sorry, invalid account\n\n");
    	else
    	{
            A[index].balanceArray = 0;       //the balance Array is intialized lossing all value
    		A[index].accountArray = 0;      //the account Array is intialized lossing all the information
    		A[index].name[LENGTH] = 0; 
    
    	}
    
    
    
    }
    
    //-------------------------------------------
    
    void accountSearch (customer A[] )
    {
    
    	int account;
    	int index , found = -1;
    	double amount = 0;
    
    
    
    	printf ("Enter the account number :");
    	scanf ("%d", &account);
    
    	
    
    	index = 0;
    
    	while ( index < SIZE )
    	{
    		if ( A[index].accountArray == account )
    		{
    			found = index;
    			break;
    		}
    		index++;
    	}
    
    	if ( found == -1 ) printf ("Sorry, invalid account\n\n");
    	else
    	{
    		printf ("\n\nAccount found :");
    		printf ("\n\nThe name of the Account holder is: %c", A[index].name );
    		printf ("\n\nthe Account number and balance is %d %.2lf\n",A[index].accountArray, A[index].balanceArray );
    
    
    	}
    
    }
    
    //-----------------------
    	void sort (customer A[] )
    	{
    		int i, j;
    		char temp[25];
    		double tempBal;
    		int tempAcc;
    		int index;
    
    		for (j=0; j<SIZE; j++)
    		{
    		for (i=0; i<SIZE-1; i++)
    		{
    			if (strcmp(A[i].name, A[i+1].name) > 0)
    			{
    				strcpy (temp, A[i].name);
    				strcpy (A[i].name, A[i+1].name);
    				strcpy (A[i+1].name, temp);
    
    				tempBal = A[i].balanceArray;
    				A[i].balanceArray = A[i+1].balanceArray;
                    A[i+1].balanceArray = tempBal;
             
                    tempAcc = A[i].accountArray;
                    A[i].accountArray = A[i+1].accountArray;
                    A[i+1].accountArray = tempAcc;
    			}
    		}
    
    		for (index = 0; index<SIZE; index++)
    		{
    			if (A[index].accountArray !=0)
    				printf("%s\t%d\t\t%.2lf\n", A[index].name, A[index].accountArray, A[index].balanceArray);
    		}
    	}
    
    
    }
    
    //------------------------------
    
    	void nameSearch (customer A[] )
    	{	
    		char temp [25];
    	int index; 
        char found = -1;
        fflush (stdin);
    	printf ("\nEnter the name you are looking for:");
        gets(temp);
         
    index = 0;
    
    	for(index=0; index<SIZE; index++)
    	{
    		if ( strcmp (A[index].name, temp) == 0 )
    		{
    			found = 1;
    		
    		printf("found");
    		break;
        }
    	}
    	if ( found == -1 ) printf ("Sorry, that person doesn't belong to this bank\n\n");
    	else
    	{    
    	printf ("\nAccount: %d\t Name: %s \t Amount: %.2lf\n\n\n", A[index].accountArray, A[index].name, A[index].balanceArray);
    	}
    	}
    
    
    //-------------------------------------------
    void makeUpper ( char str[])
    {
         int i;
         
         for (  i = 0; i< strlen ( str ); i++)
         {
             str[i] = toupper ( str[i] );
         }
         
    }
    
    
    //-------------------------------------------
    void printAll (customer A[] )
    {
    	int index = 0; 
    	while ( index <100 )
    	{
    		if (A[index].accountArray !=0)
    			printf ("%d\t%.2lf\t[%s]\n", A[index].accountArray, A[index].balanceArray, A[index].name);
    
    		index++;
    	}
    }
    
    
    //-------------------------------------------
    
    void clean ( char name [] )
    {
    	int index = strlen (name) -2;
    
    	while (index >= 0)
    	{
    		if (name[index] == '\n' || name[index] == ' ' )
    			index --;
    		else
    		{
    			name[index+1] = '\0';
    		break;
    		}
    	}
    	for ( index=0; index < strlen(name); index++)
    	{
    		name[index] = name[index+1];
    	}
    }
    
    
    
    //-------------------------------------------
    Attached Files Attached Files

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    FILE *stream = fopen ("bankData.txt", "r");
    
    customer user[SIZE] ={{0,0, ""}};
    
    int counter = 0;
    
    	 int account ;
       double balance;
       char name[LENGTH];
       
       
       
     fscanf (stream, "%d", &account );
     
     
     while ( !feof (stream) )
     {
     
             fscanf (stream, "%lf", &balance );
            
             fgets(name, 25, stream);
    
    		 clean (name);
             
             user[counter].accountArray = account;
             user[counter].balanceArray = balance;
             strcpy ( user[counter].name, name);
             counter ++;     
             
             
             fscanf (stream, "%d", &account );
    
    }
    You never check to see that fopen actually worked, and you never actually check any ofy our return values when you read from your file to see if they worked also. There is also a FAQ (Cprogramming.com FAQ > Why it's bad to use feof() to control a loop) on why it's usually bad to use feof for loop control.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > fflush (stdin);
    > gets(temp);
    There are FAQ entries for these two horrors as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework Help
    By alecmcraig in forum C Programming
    Replies: 4
    Last Post: 11-17-2007, 12:03 AM
  2. Homework Help
    By shadowctr in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2005, 07:29 PM
  3. I want homework!
    By Tynnhammar in forum C++ Programming
    Replies: 9
    Last Post: 09-29-2004, 02:49 PM
  4. ok, yes, I need help with my homework!
    By melee in forum C Programming
    Replies: 5
    Last Post: 09-22-2004, 07:42 AM
  5. do my homework for me..
    By dP munky in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2003, 10:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21