Write a program that uses a structure to store the following data on a company d
ID: 3913423 • Letter: W
Question
Write a program that uses a structure to store the following data on a company division.
- Division Name (East, West, North or South)
- Quarter (1, 2, 3 or 4)
- Quarterly Sales
The user should be asked for the four quarters’ sales figures for the East, West, North and South divisions. The data for each quarter for each division should be written to a file called salesreport.txt.
Input validation
- Do not accept negative numbers for any sales figures.
- Standardize user’s Division Name input to East, West, North or South
Please also answer the following questions:
1. What members will your structure contain? What is the data type of each member?
2. How will data be organized in your output file.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>
using namespace std;
struct division
{
char name[10];
int quarter;
int quarterlySales;
};
int main(int argc, char const *argv[])
{
FILE *fptr;
fptr = fopen("salesreport.txt", "a");
if(fptr == NULL){
printf("Error!");
exit(1);
}
for (int i = 0; i < 4; ++i)
{
printf("reading data for quarter %d ",i+1);
fprintf(fptr,"%d quarter ",i+1);
for (int j = 0; j < 4; ++j)
{
printf("Enter division Number 1.East 2.West 3.North 4.South ");
int n=0;
char s[10];
scanf("%d",&n);
while(n<1 || n>4){
scanf("%d",&n);
}
if (n==1)
{
strcpy(s,"East");
}
else if (n==2)
{
strcpy(s,"West");
}
else if (n==3)
{
strcpy(s,"North");
}
else if (n==4)
{
strcpy(s,"South");
}
// scanf("%s",s);
// printf("Enter quarterlySales ");
int sales=0;
scanf("%d",&sales);
if (sales<=0)
{
printf("invalid sales ");
while(sales <= 0){
scanf("%d",&sales);
}
}
struct division* new_node=NULL;
new_node=(struct division*)malloc(sizeof(struct division*));
strcpy(new_node->name,s);
new_node->quarter=i+1;
new_node->quarterlySales=sales;
fprintf(fptr,"%s ",new_node->name);
fprintf(fptr,"%d ",new_node->quarter);
fprintf(fptr,"%d ",new_node->quarterlySales);
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.