Write a C program that prompts the user to enter some data regarding some client
ID: 3760723 • Letter: W
Question
Write a C program that prompts the user to enter some data regarding some clients to a business. The user should prompt for customer account number (a positive integer between 1 and 1000), a last name (a string), and their account balance (a positive floating point number) as shown below. Notice how the user input terminates once the user enters a -999 as the account number: Enter account number, last name, and balance. Enter -999 to end input: ? 100 Smith 24.98 ? 8000 Jones 334.33 *** Invalid account number. Please enter 1 - 1000 or -999 to exit *** ? 800 Jones 334.33 ? 400 Johnson 56.55 ? 300 Roberts -330.90 *** Invalid balance amount. Please enter a positive value. *** ? 300 Roberts 330.90 ? 500 White 0.00 ? –999 (What the user types in is shown in blue for clarity only.) You could store this information in either 3 separate arrays or in an array of type struct (something). Make the array sizes large enough to hold data for up to 5 clients. Part 2 (data output): Once the user has completed entering the data, the program should then SORT THE DATA IN ASCENDING ORDER BY ACCOUNT NUMBER, and then output the client data in table form as follows: ACCOUNT LAST NAME BALANCE 100 Smith 24.98 300 Roberts 330.90 400 Johnson 56.55 500 White 0.00 800 Jones 334.33
Explanation / Answer
#include <stdio.h>
struct account
{
int acc_no;
char name[100];
float bal;
};
int main()
{
int i,j,cnt=5;
struct account ac[6], temp;
for(i=0;i<5;i++)
{
printf("Enter account number, last name, and balance. Enter -999 to end input: ");
scanf("%d %s %f",&ac[i].acc_no,&ac[i].name,&ac[i].bal);
if(ac[i].acc_no==-999)
{
cnt = i;
break;
}
if(ac[i].acc_no < 1 || ac[i].acc_no > 1000)
{
printf("enter a account number between 1- 1000 ");
i--;
continue;
}
if(ac[i].bal < 0)
{
printf("enter a valid balance ");
i--;
continue;
}
}
for(i=0;i<cnt;i++)
{
for(j=1;j<cnt-i;j++)
{
if(ac[j].acc_no < ac[j-1].acc_no)
{
temp = ac[j];
ac[j]= ac[j-1];
ac[j-1] = temp;
}
}
}
for(i=0;i<cnt;i++)
{
printf("%d %s %f ",ac[i].acc_no,ac[i].name, ac[i].bal);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.