Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Accounting Program Help [solved] (http://www.programmingforums.org/showthread.php?t=1424)

Benoit Dec 4th, 2004 11:57 PM

I'm bored so I decided to create an accounting program based on what I learned so far in accounting class...it seems to work so far except for one thing, and I can't figure out whats causing it.....


:

#include<stdio.h>
#include<string.h>

#define MAX 40 //maximum number of accounts

typedef struct
{
        char name[50];
        float balance;
       
}account;

account assets[MAX]; 
int assets_number=0; //set the number of accounts for each to zero

account liabilities[MAX];
int liabilities_number=0;

account expenses[MAX];
int expenses_number=0;

account revenue[MAX];
int revenue_number=0;

account ownersequity[MAX];
int ownersequity_number=0;



void load();
void save();
void add();
void delete();
void balance_sheet();
void income_statement();

main()
{
char choice;

while(choice !='X')
{

        printf("\n\nAccounting database\n\n");
        printf("L: Load an existing set of accounts\n");
        printf("S: Save current accounts\n");
        printf("C: Create new accounts\n");
        printf("D: Delete an account\n");
        printf("X: Exit\n");
        printf("\n\nEnter your choice: ");

        scanf("%c", &choice);

        choice=toupper(choice);

        switch (choice)
        {
        case 'L': load();
 break;
        case 'S': save();
 break;
        case 'C': add();
 break;
        case 'D': delete();
 break;
        case 'X': exit(0);

        default: puts("Not an option");
 break;
        }
}
return 0;
}

void add()
{

char choice;
       
while(choice !='M')
{
       
        printf("\n\nAdd new accounts\n\n");
        printf("A: Asset\n");
        printf("L: Liability\n");
        printf("O: Owner's Equity\n");
        printf("E: Expense\n");
        printf("R: Revenue\n");
        printf("M: Main menue\n\n");
        printf("Enter your choice: ");

        scanf("%c", &choice);

        choice=toupper(choice);

        switch (choice)
        {
        case 'A':
 assets_number++;
 printf("\nNew asset account name: ");
 scanf("%s", &assets[assets_number].name);
 printf("%s balance: ",assets[assets_number].name);
 scanf("%f", &assets[assets_number].balance);
 break;
       
        case 'L':
 liabilities_number++;
 printf("\nLiability account name:");
 scanf("%s", &liabilities[liabilities_number].name);
 printf("%s balance: ",liabilities[liabilities_number].name);
 scanf("%f", &liabilities[liabilities_number].balance);
 break;
        case 'O':
 ownersequity_number++;
 printf("\nNew owner's equity account name: ");
 scanf("%s", &ownersequity[ownersequity_number].name);
 printf("%s balance: ",ownersequity[ownersequity_number].name);
 scanf("%f", &ownersequity[ownersequity_number].balance);
 break;

        case 'E':
 expenses_number++;
 printf("\nNew expense account name: ");
 scanf("%s", &expenses[expenses_number].name);
 printf("%s balance: ",expenses[expenses_number].name);
 scanf("%f", &expenses[expenses_number].balance);
 break;

        case 'R':
 revenue_number++;
 printf("\nNew revenue account name: ");
 scanf("%s", revenue[revenue_number].name);
 printf("%s balance: ",revenue[revenue_number].name);
 scanf("%f", &revenue[revenue_number].balance);
 break;

        case 'M': break;

       
       
        }

}
}


void load()
{
//nothing here yet
}


void save()
{
//nothing here yet
}


void delete()
{
//nothing here yet
}


As you can see its incomplete so far, but I tried testing out what I have so far to make sure it works and I get this output:

Accounting database

L: Load an existing set of accounts
S: Save current accounts
C: Create new accounts
D: Delete an account
X: Exit


Enter your choice: c


Add new accounts

A: Asset
L: Liability
O: Owner's Equity
E: Expense
R: Revenue
M: Main menue

Enter your choice:

Add new accounts

A: Asset
L: Liability
O: Owner's Equity
E: Expense
R: Revenue
M: Main menue

Enter your choice:


For some reason, it prints the menu for the add accounts section twice :wacko:

101 Dec 5th, 2004 6:52 AM

you should clean the keyboard buffer.

Benoit Dec 5th, 2004 10:07 AM

How do I do that?

Eggbert Dec 5th, 2004 11:10 AM

:

char ch;

while ( ( ch = getchar() ) != EOF && ch != '\n' )
 ;

However, the need to do this should indicate problems with the way you're reading input. It would be better to change your code so that the problem doesn't arise rather than fixing the symptom of the problem. The problem is with scanf and/or single character input leaving extraneous characters in the input stream. The way to fix it is to use fgets and take a line of input at a time, parsing it as you go.


All times are GMT -5. The time now is 3:28 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC