Wreitten in c++ visual studio Must be modualr Brewster has provided a file that
ID: 3737995 • Letter: W
Question
Wreitten in c++ visual studio
Must be modualr
Brewster has provided a file that contains the following two fields
Sales person's ID number as an integer
Amount of sale Real number
Brewster wants you to design a program that prints a sales report. The sales and the totalsales for
for that person. The report should show the total sales for all sales people for the month.
Sales Id Amount of Sale
100 10000
100 12000
100 5000
101 14000
101 18000
101 12500
102 13500
102 14500
102 20000
Explanation / Answer
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <iomanip> // For formatting using setw and endl
using namespace std;
#define INPUT_LENGTH 255
struct salesData{
int salesID;
double sales;
};
int main(int argc, char *argv[]) {
struct salesData sd[10];
int salesid;
double totalsales = 0;
FILE *fileNames = NULL;
char input[INPUT_LENGTH]; // a string for reading from a file
char *next;
fileNames = fopen("data.txt", "r" ); // Read file name from arguments
int iCount = 0;
cout<<"Sales ID"<<setw(25)<<"Total sales amount"<<endl;
// Check if file opens
if( NULL != fileNames ){
// Iterate on all lines in file
while( NULL != fgets( input, INPUT_LENGTH, fileNames ) ){
// Tokenizing the line and print the data
next = strtok(input, " ");
salesid = atoi(next);
cout<<sd[iCount].salesID;
if(iCount == 0){
sd[iCount].salesID = salesid;
cout<<sd[iCount].salesID;
}
if(sd[iCount].salesID != salesid){
cout<<sd[iCount].salesID << setw(25) << sd[iCount].sales << endl;
iCount++;
sd[iCount].salesID = salesid;
}
// Get tokens from string separated by space.
while (next != NULL)
{
next = strtok(NULL, " ");
sd[iCount].sales = sd[iCount].sales + atof(next);
}
}
fclose(fileNames);
}else{
cout<<"Unable to open the file";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.