C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 23rd, 2012, 01:17 PM
cowbo_mike cowbo_mike is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 1 cowbo_mike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 43 sec
Reputation Power: 0
File IO - C program records and list monthly expenses

I am trying to write a C program using fseek, fopen, fwrite, SEEK_CUR, SEEK_END to record my monthly expenses, every purchase that I make I can add in to the program and look up how much I have spend. I am having trouble writing the code to delete an item? and when it records to the file that I have written it doubles de recording, and I dont know why?

Please help!

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

void Expense_type (int);

void Expense_type2 (int option);

int main (void)
{
int option;

printf("\n Please choose an option below\n\n"
"\n (1) Launch income / expense \n"
"\n (2) Cancel Expense || Edit entry \n"
"\n (3) See types of launch\n"
"\n (4) Register types of launch\n"
"\n (0) Exit\n");
scanf("%d", &option);

if (option > 0 && option <= 5)
{
Expense_type(option);

}
else

printf("\nOption invalid!");

system ("pause");

return 0;

}

void Expense_type (int option)
{
int option2, found = 0;

char purchase[20];

float amount, sum;

FILE *p;

while (option == 1)
{
printf("\nPlease choose an option to add your Expenses:\n\n"
"\n (1) Include \n"
"\n (2) Make a change \n"
"\n (3) Exclude an item \n"
"\n (4) Consult \n"
"\n (5) List all \n"
"\n (0) Exit \n\n");
scanf("%d", &option2);

// ************************************************** ************************************************** ***

if (option2 == 1)
{

p = fopen("CADASTRO.DAT","a+"); /* apend file (add text to
a file or create a file if it does not exist.*/
if (!p)
{
printf("File could not be opened\n\n");
}

printf("\n Purchase Type: ");
scanf("%s", purchase);

printf("\n How much: ");
scanf("%f", &amount);

fprintf(p, "%s\t%f\n", purchase, amount);
fclose (p);

} // Option 1 if statement

//************************************************** ************************************************** ***********

if (option2 == 2)

{
p = fopen("CADASTRO.DAT","a+"); /* apend file (add text to
a file or create a file if it does not exist.*/
if (!p)
{
printf("File could not be opened\n\n");
}

// ???? Como alterar o file???


} // chave que fecha option2 == 2

//************************************************** ************************************************** *****************
if (option == 3)
{
p = fopen("CADASTRO.DAT","a+"); /* apend file (add text to
a file or create a file if it does not exist.*/
if (!p)
{
printf("File could not be opened\n\n");
}


// Excluir algo do arquivo


} // chave que fecha option == 3

//************************************************** ************************************************** **********************
if (option == 4)
{
p = fopen("CADASTRO.DAT","a+"); /* apend file (add text to
a file or create a file if it does not exist.*/
if (!p)
{
printf("File could not be opened\n\n");
}


//Consultar o arquivo CADASTRO.DAT


} // chave que fecha option 4


//************************************************** ***************************************

if (option2 == 5)
{
p = fopen("CADASTRO.DAT","r"); // open file para listar tudo

if (p == NULL)
{
perror("\nError when trying to open file CADASTRO.DAT\n");

}
else

while (!feof(p) )
{
fscanf(p, "%s ", purchase);
fscanf(p, "%f", &amount);

sum += amount;

printf("\n Type of purchase: %s Valor R$: %.2f \n", purchase, amount);

}

printf("\n Total Value: %.2f \n", sum);


fclose(p);

}

//************************************************** ************************************************** ***********

if (option2 == 0)
{
exit (0) ;
}


} //bracket do while

} // Close function number 2

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > File IO - C program records and list monthly expenses


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap