Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Findind the total cost

topdometopdome Member Posts: 1
#include
#include
#include
#include
#include
#define SIZE 1000 // Can hold 1000 struct records


typedef struct
{
char cash[10];
char code[10];
char item[14];
char cost[10];
char total[12];
char qty[3];
}record;

void convert_to_upper_array(char *ch);
void Add(void);
void sort_by_code(record in[], int n);
void Look(record in[], int n);
void print_record2(record in[], int j);
void record_header(void);
void print_record(record in[], int j);
void check_line(int *line, int max);
void sort_ID(record in[], int n);
void swap(record *in, int a, int b);


void main(void)
{
FILE *open;
int choice;
int i = 0;
record in[SIZE];

system("cls");

printf("
");
printf("
Please choose one of the options below

");
printf("
");
printf(" ?
");
printf(" [1] New Transaction
");
printf(" [2] View Transactions
");
printf(" [3] Search Transactions
");
printf(" [4] Exit Program
");
printf(" ?
");
printf("



Enter Choice (1-4): ");
scanf("%d", &choice);

if(choice == 1)
Add();
if(choice == 2)
{
open = fopen("sales.dat", "rb+");

//Checking to see if there is even a file
if (open == NULL)
{
system("cls");
printf("Error Opening Database..........
");
printf("Create one by adding a transaction.


");
system("PAUSE");
main();
}

while(!feof(open))
{
fread(&in[i], sizeof(record), 1, open);
i++;
}
sort_by_code(in, i-1);
fclose(open);
main();

}
if(choice == 3)
{
open = fopen("sales.dat", "rb+");

//Checking to see if there is even a file
if (open == NULL)
{
system("cls");
printf("Error Opening Database..........
");
printf("Create one by adding a transaction.


");
system("PAUSE");
main();
}

while(!feof(open))
{
fread(&in[i], sizeof(record), 1, open);
i++;
}
Look(in, i-1);
fclose(open);
main();
}

if(choice == 4)
{
system("cls");

printf("Exiting!
");
system("PAUSE");
exit(1);
}
}

void Add(void)
{
FILE *open;//*open1;
char choice;
record in;

system("cls");

//Getting input from User.
do
{
printf("Adding new transaction to database
");
printf("
");

printf("Enter Cashier's ID: ");
fflush(stdin);
gets(in.cash);
convert_to_upper_array(in.cash);

printf("Enter Transaction Code: ");
fflush(stdin);
gets(in.code);
convert_to_upper_array(in.code);

printf("Enter Item Purchased: ");
fflush(stdin);
gets(in.item); //reading the stirng
//converting every letter to uppercase
convert_to_upper_array(in.item); //

printf("Enter Cost of Item: $");
fflush(stdin);
gets(in.cost); //reading the stirng

printf("Quanity Purchased: ");
fflush(stdin);
gets(in.qty);

printf("Total Payment: $
");




I need to find the total payment. (cost x qty)
how?

thanks!

Comments

  • stoberstober Member Posts: 9,765 ✭✭✭
    [code]
    : #include
    : #include
    : #include
    : #include
    : #include
    : #define SIZE 1000 // Can hold 1000 struct records
    :
    :
    : typedef struct
    : {
    : char cash[10];
    : char code[10];
    : char item[14];
    : char cost[10];
    : char total[12];
    : char qty[3];
    : }record;
    :
    : void convert_to_upper_array(char *ch);
    : void Add(void);
    : void sort_by_code(record in[], int n);
    : void Look(record in[], int n);
    : void print_record2(record in[], int j);
    : void record_header(void);
    : void print_record(record in[], int j);
    : void check_line(int *line, int max);
    : void sort_ID(record in[], int n);
    : void swap(record *in, int a, int b);
    :
    :
    [blue]The function main() is supposed to return an int. This return value
    gets passed back to the operating system so that if another program runs
    it that program knows its return value (normally an error number[/blue]

    [red] int main(void)[/red]
    : {
    : FILE *open;
    : int choice;
    : int i = 0;
    : record in[SIZE];
    :
    : system("cls");
    :
    : printf("
    ");
    : printf("
    Please choose one of the options below

    ");
    : printf("
    ");
    : printf(" ?
    ");
    : printf(" [1] New Transaction
    ");
    : printf(" [2] View Transactions
    ");
    : printf(" [3] Search Transactions
    ");
    : printf(" [4] Exit Program
    ");
    : printf(" ?
    ");
    : printf("



    Enter Choice (1-4): ");
    : scanf("%d", &choice);
    :
    : if(choice == 1)
    : Add();
    : if(choice == 2)
    : {
    : open = fopen("sales.dat", "rb+");
    :
    : //Checking to see if there is even a file
    : if (open == NULL)
    : {
    : system("cls");
    : printf("Error Opening Database..........
    ");
    : printf("Create one by adding a transaction.


    ");
    : system("PAUSE");
    [red]Never, Never, Never call main() from within your program! It is
    only to be called from the operating system. If you want to restart
    it, use a while or do loop.[/red]
    : main();
    : }
    :
    : while(!feof(open))
    : {
    : fread(&in[i], sizeof(record), 1, open);
    : i++;
    : }
    : sort_by_code(in, i-1);
    : fclose(open);
    : main();
    :
    : }
    : if(choice == 3)
    : {
    : open = fopen("sales.dat", "rb+");
    :
    : //Checking to see if there is even a file
    : if (open == NULL)
    : {
    : system("cls");
    : printf("Error Opening Database..........
    ");
    : printf("Create one by adding a transaction.


    ");
    : system("PAUSE");
    : main();
    : }
    :
    : while(!feof(open))
    : {
    : fread(&in[i], sizeof(record), 1, open);
    : i++;
    : }
    : Look(in, i-1);
    : fclose(open);
    : main();
    : }
    :
    : if(choice == 4)
    : {
    : system("cls");
    :
    : printf("Exiting!
    ");
    : system("PAUSE");
    : exit(1);
    : }
    : }
    :
    : void Add(void)
    : {
    : FILE *open;//*open1;
    : char choice;
    : record in;
    [red] double cost, total;[/red]
    :
    : system("cls");
    :
    : //Getting input from User.
    : do
    : {
    : printf("Adding new transaction to database
    ");
    : printf("
    ");
    :
    : printf("Enter Cashier's ID: ");
    : fflush(stdin);
    : gets(in.cash);
    : convert_to_upper_array(in.cash);
    :
    : printf("Enter Transaction Code: ");
    : fflush(stdin);
    : gets(in.code);
    : convert_to_upper_array(in.code);
    :
    : printf("Enter Item Purchased: ");
    : fflush(stdin);
    : gets(in.item); //reading the stirng
    : //converting every letter to uppercase
    [red] /*[/red] convert_to_upper_array(in.item); [red]*/[/red]
    [red] strupr(in.item);[/red]
    :
    : printf("Enter Cost of Item: $");
    : fflush(stdin);
    : gets(in.cost); //reading the stirng
    :
    : printf("Quanity Purchased: ");
    : fflush(stdin);
    : gets(in.qty);
    :
    : printf("Total Payment: $
    ");
    [red] cost = atof(in.cost);
    total = cost * atoi(in.qty);
    [/red]
    :
    [/code]
    :
    :
    :
    : I need to find the total payment. (cost x qty)
    : how?
    :
    : thanks!
    :

  • dboydboy Member Posts: 3
    : #include
    : #include
    : #include
    : #include
    : #include
    : #define SIZE 1000 // Can hold 1000 struct records
    :
    :
    : typedef struct
    : {
    : char cash[10];
    : char code[10];
    : char item[14];
    : char cost[10];
    : char total[12];
    : char qty[3];
    : }record;
    :
    : void convert_to_upper_array(char *ch);
    : void Add(void);
    : void sort_by_code(record in[], int n);
    : void Look(record in[], int n);
    : void print_record2(record in[], int j);
    : void record_header(void);
    : void print_record(record in[], int j);
    : void check_line(int *line, int max);
    : void sort_ID(record in[], int n);
    : void swap(record *in, int a, int b);
    :
    :
    : void main(void)
    : {
    : FILE *open;
    : int choice;
    : int i = 0;
    : record in[SIZE];
    :
    : system("cls");
    :
    : printf("
    ");
    : printf("
    Please choose one of the options below

    ");
    : printf("
    ");
    : printf(" ?
    ");
    : printf(" [1] New Transaction
    ");
    : printf(" [2] View Transactions
    ");
    : printf(" [3] Search Transactions
    ");
    : printf(" [4] Exit Program
    ");
    : printf(" ?
    ");
    : printf("



    Enter Choice (1-4): ");
    : scanf("%d", &choice);
    :
    : if(choice == 1)
    : Add();
    : if(choice == 2)
    : {
    : open = fopen("sales.dat", "rb+");
    :
    : //Checking to see if there is even a file
    : if (open == NULL)
    : {
    : system("cls");
    : printf("Error Opening Database..........
    ");
    : printf("Create one by adding a transaction.


    ");
    : system("PAUSE");
    : main();
    : }
    :
    : while(!feof(open))
    : {
    : fread(&in[i], sizeof(record), 1, open);
    : i++;
    : }
    : sort_by_code(in, i-1);
    : fclose(open);
    : main();
    :
    : }
    : if(choice == 3)
    : {
    : open = fopen("sales.dat", "rb+");
    :
    : //Checking to see if there is even a file
    : if (open == NULL)
    : {
    : system("cls");
    : printf("Error Opening Database..........
    ");
    : printf("Create one by adding a transaction.


    ");
    : system("PAUSE");
    : main();
    : }
    :
    : while(!feof(open))
    : {
    : fread(&in[i], sizeof(record), 1, open);
    : i++;
    : }
    : Look(in, i-1);
    : fclose(open);
    : main();
    : }
    :
    : if(choice == 4)
    : {
    : system("cls");
    :
    : printf("Exiting!
    ");
    : system("PAUSE");
    : exit(1);
    : }
    : }
    :
    : void Add(void)
    : {
    : FILE *open;//*open1;
    : char choice;
    : record in;
    :
    : system("cls");
    :
    : //Getting input from User.
    : do
    : {
    : printf("Adding new transaction to database
    ");
    : printf("
    ");
    :
    : printf("Enter Cashier's ID: ");
    : fflush(stdin);
    : gets(in.cash);
    : convert_to_upper_array(in.cash);
    :
    : printf("Enter Transaction Code: ");
    : fflush(stdin);
    : gets(in.code);
    : convert_to_upper_array(in.code);
    :
    : printf("Enter Item Purchased: ");
    : fflush(stdin);
    : gets(in.item); //reading the stirng
    : //converting every letter to uppercase
    : convert_to_upper_array(in.item); //
    :
    : printf("Enter Cost of Item: $");
    : fflush(stdin);
    : gets(in.cost); //reading the stirng
    :
    : printf("Quanity Purchased: ");
    : fflush(stdin);
    : gets(in.qty);
    :
    : printf("Total Payment: $
    ");
    :
    :
    :
    :
    : I need to find the total payment. (cost x qty)
    : how?
    :
    : thanks!
    :
    Try putting the item purchased * quantity in a count controlled loop totalPrice = (item *quantity)
    while(!end of program)
    grandTotal = grandTotal + totalPrice;
    cout << grandTotal @ end of program or upon prompt
  • Chris BrownChris Brown USAMember Posts: 4,624 ✭✭

    ________ || http://forcoder.org || free ebooks and video tutorials about [ PHP PL/SQL C++ Java MATLAB Visual Basic Swift Ruby Perl Assembly C JavaScript Python Objective-C R Visual Basic .NET C# Go Delphi Scratch Ada Alice Transact-SQL Kotlin Scheme COBOL Erlang Julia ML F# Lisp Bash Hack LabVIEW D Dart Logo Fortran Prolog ABAP Rust FoxPro Scala VBScript Apex Lua Clojure Crystal Awk SAS ] __

Sign In or Register to comment.