Question

Create another program that will prompt a user for input file. The user will choose from...

Create another program that will prompt a user for input file. The user will choose from 4 choices:

1. Display all positive balance accounts.

2. Display all negative balance accounts.

3. Display all zero balance accounts.

4. End program.

C PROGRAMMING

my code so far

#include<stdlib.h>

#include<stdio.h>

int main(void)

{

int request;

int account;

FILE *rptr;

char name[20];

double balance;

FILE *wptr;

  

int accountNumber;

  

if((wptr = fopen("clients.dat","w")) == NULL)

{

puts("File could not be opened");

}

else

{

puts("Enter the account, name, and balance.");

puts("Enter EOF to end input");

printf("%s","? ");

scanf("%d%29s%lf", &account, name, &balance);

  

while(!feof(stdin))

{

fprintf(wptr,"%d %s %.2f\n", account, name, balance);

printf("%s","? ");

scanf("%d%29s%lf", &account, name, &balance);

  

}

rewind(wptr);

fclose(wptr);

  

}

  

if((rptr = fopen("clients.dat","r")) == NULL)

{

puts("File could not be opened");

}

else

{

  

  

/*

printf("%s%s%s","Account","Name","Balance");

fscanf(rptr, "%d%29s%lf", &account,name, &balance);

   */

  

while(!feof(rptr))

{

  

printf("%s", "enter request: \n"

   "1. List negative balances. \n"

   "2. List positive balances. \n"

   "3.List Zero balances. \n"

   "4- End program.\n");

scanf("%d", &request);

  

switch(request)

{

case 1:

break;

  

case 2:

break;

  

case 3:

break;

  

default: printf("program is now ending ");

break;

}

  

}

}

  

return EXIT_SUCCESS;

}

MY CODE SO FAR

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

Here the modified code:

//Header file inclusion

#include<stdlib.h>

#include<stdio.h>

// Main method

int main(void)

{

// variable declaration

    int request;

    int account;

     int count;

     int i,accno[100];

double bal[100];

    FILE *rptr;

    char name[20];

    double balance;

    FILE *wptr;

// For storing the user entered file name

     char filename[30];

    int accountNumber;

// Getting the file name from the user

     printf("\n Enter the file Name:");

     scanf("%s",filename);

//Opening the user given file

    if((wptr = fopen(filename,"w")) == NULL)

    {

     puts("File could not be opened");

    }

//if file is successfully opened

    else

    {

     puts("Enter the account, name, and balance.");

     puts("Enter EOF to end input");

    count= scanf("%d%29s%lf", &account, name, &balance);

     while(1)

     {      if(count<3)

               break;

          else

          {

               fprintf(wptr,"%d %s %.2f\n", account, name, balance);

               count=scanf("%d%29s%lf", &account, name, &balance);

          }

     }

rewind(wptr);

fclose(wptr);

}

//Opening the file in read mode

   if((rptr = fopen(filename,"r")) == NULL)

   {

        puts("File could not be opened");

   }

//if successfully opened the file read the

   else

   {

        i=0;

          while(!feof(rptr))

          {

          fscanf(rptr,"%d %s %.2f",&accno[i],name &bal[i]);

          i++;

          }

   }

fclose(rptr);

     printf("enter request: \n"

             "1. List negative balances. \n"

             "2. List positive balances. \n"

             "3.List Zero balances. \n"

             "4- End program.\n");

     scanf("%d", &request);

     switch(request)

         {

          case 1:

//Display the account number which has negative balance

               for(i=0;i<100;i++)

               {

                    if(bal[i]<0)

                    printf("\n%d",accno[i]);

               }

break;

//Display the account that has positive balance

          case 2:

                   for(i=0;i<100;i++)

                    {

                        if(bal[i]>0)

                        printf("\n %d",accno[i]);

                   }

                    break;

   // Display the zero account balance                

          case 3:

                   for(i=0;i<100;i++)

                    {

                        if(bal[i]==0)

                        printf("\n %d",accno[i]);

                   }

                    break;

          default:

printf("program is now ending ");

                    break;

           }

return EXIT_SUCCESS;

}

Output:

The sample output is

Add a comment
Know the answer?
Add Answer to:
Create another program that will prompt a user for input file. The user will choose from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • create case 4 do Method 1:copy item by item and skip the item you want to...

    create case 4 do Method 1:copy item by item and skip the item you want to delete after you are done, delete the old file and rename the new one to the old name. #include int main(void) { int counter; int choice; FILE *fp; char item[100]; while(1) { printf("Welcome to my shopping list\n\n"); printf("Main Menu:\n"); printf("1. Add to list\n"); printf("2. Print List\n"); printf("3. Delete List\n"); printf("4. Remove an item from the List\n"); printf("5. Exit\n\n"); scanf("%i", &choice); switch(choice) { case 1:...

  • Hardware Inventory – Write a database to keep track of tools, their cost and number. Your...

    Hardware Inventory – Write a database to keep track of tools, their cost and number. Your program should initialize hardware.dat to 100 empty records, let the user input a record number, tool name, cost and number of that tool. Your program should let you delete and edit records in the database. The next run of the program must start with the data from the last session. This is my program so far, when I run the program I keep getting...

  • Within this C program change the input to a file instead of individual input from the...

    Within this C program change the input to a file instead of individual input from the user. You must take the input file name on the command line. Be sure to have simple error checking to be sure the file exists and was successfully opened. The input file will have the following format: First line: Number of students Second Line: Number of grades Third Line: Student Names, space delimited Fourth+ Line(s): Grades for all students for one assignment, space delimited....

  • Identify and list all the User defined Functions to be used in the system #include <stdio.h>...

    Identify and list all the User defined Functions to be used in the system #include <stdio.h> ///for input output functions like printf, scanf #include <stdlib.h> #include <conio.h> #include <windows.h> ///for windows related functions (not important) #include <string.h> ///string operations /** List of Global Variable */ COORD coord = {0,0}; /// top-left corner of window /** function : gotoxy @param input: x and y coordinates @param output: moves the cursor in specified position of console */ void gotoxy(int x,int y) {...

  • C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the class program with methods and variables 2. bill.cpp = contains the functions from class file 3. main.cpp = contains the main program. Please split this program into three files and make the program run. I have posted the code here. #include<iostream>...

  • C++ HELP I need help with this program. I have done and compiled this program in...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. I am using printf and scanf. Instead of printf and scanf use cin and cout to make the program run. after this please split this program in three files 1. bill.h = contains the class program with methods and variables eg of class file class bill { } 2. bill.cpp = contains the functions from class...

  • How would I change the following C code to implement the following functions: CODE: #include <stdio.h>...

    How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...

  • C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string usi...

    C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string using the RSA algorithm (C90 language only). It can contain ONLY the following libraries: stdio, stdlib, math and string. I want to use the following function prototypes to try and implement things: /*Check whether a given number is prime or not*/ int checkPrime(int n) /*to find gcd*/ int gcd(int a, int h) void Encrypt(); void Decrypt(); int getPublicKeys(); int getPrivateKeys();...

  • I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the...

    I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the program until they enter "q" to quit. However, my program runs through one time, the prompt appears again, but then it terminates before the user is allowed to respond to the prompt again. I'm not able to debug why this...

  • Part D. On executing the below program, what will be the output of the following program...

    Part D. On executing the below program, what will be the output of the following program if the source file contains a line "This is a sample."? [5 marks] #include <stdio.h> int main() { char ch; int k, n=0; FILE * fs = fopen("source.txt", "r"); while(1) { ch = fgetc(fs); if(ch == EOF) break; else { for(k=0;k<4;k++) fgetc(fs); printf("%c", ch); n += k; } } printf("%d\n", n); fclose(fs); return 0; } ---------------------------------- Any one can explain the question to me...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT