Question

Write a C++ program that will input data from a Comma-separated values (.csv) file and output...

Write a C++ program that will input data from a Comma-separated values (.csv) file and output some information about it. This program uses a csv file from Yahoo Finance

(.csv) filename : SBUX.csv

1. Output the name of the ticker to the console screen (without the “.csv”)

2. Output the start date and end date that was found in the file

3. Output how many trading day(s) existed in the file

4. Prompt the use to input a number of records

5. Input the number of records from the console screen

6. Output the following heading to the console screen: Date Open A.Close Percent Change

for example here is SBUX.csv information

Date Open High Low Close Adj Close Volume
1/1/2009 null null null null null null
2/1/2009 4.63 5.385 4.45 4.575 3.668556 487465600
3/1/2009 4.49 6.215 4.06 5.555 4.454388 574769000
4/1/2009 5.495 7.72 5.405 7.23 5.797522 656683400
5/1/2009 7.21 7.25 6.26 7.195 5.769456 555559800
6/1/2009 7.285 7.7 6.77 6.945 5.568988 521311000
7/1/2009 6.99 8.95 6.38 8.85 7.096554 673380800
8/1/2009 8.99 9.925 8.835 9.495 7.61376 493708000
9/1/2009 9.49 10.47 9.105 10.325 8.279309 498955800
10/1/2009 10.27 10.555 9.345 9.49 7.609747 424346200
11/1/2009 9.49 11.05 9.425 10.95 8.780479 436812000
12/1/2009 10.975 11.975 10.475 11.53 9.245566 394275400
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Program in cpp

//program for getting first, last and trading days from existing file

#include<stdio.h>
#include<stdlib.h>
#include<vector>
#include<iostream>
#include <bits/stdc++.h>
#include <sstream>
#include <string>
#include <cstring>
#include <bits/stdc++.h>
using namespace std;

int main(){
ifstream infile("SBUX.csv");
ifstream countfile("SBUX.csv");
string line;
char *Lastday;
int startfl=0,edflag=0,nulllinecount=0,linecount,paracount=7,count=0;
while (getline(countfile, line))//getting total record from file
{
   count++;
}

countfile.close();

while (getline(infile, line))
{
//cout<<line<<"\n";
int n = line.length();
// declaring character array
char char_array[n + 1];
//convert string to char array
strcpy(char_array, line.c_str());

linecount++;
if(strstr(char_array, "null") != NULL) {// if line consist null then increase nullcount by 1
    nulllinecount++;
    continue;
}
//divide line it by , comma
char *token = strtok(char_array, ",");
int i=0;
while (token != NULL)//till end of file
    {
      
        if(i==0 && startfl==0){// condition for start trading day
        printf("Start Trading Day is %s\n", token);
      
       startfl=1;  
       }
  
       else if((count-1)==linecount && i==0){// condition for start trading day
  
       printf("Last Trading Day is %s\n", token);
       }
        i++;
        token = strtok(NULL, ",");
      
   }

}
printf(" Trading Days are %d ",linecount-nulllinecount+1);
infile.close();
}

/*

output

input file in SBUX.csv (open it in excel)

*/

//Program for accepting data from user and add it into input.csv file and getting first, last and trading days from accepted data

Program in c++

#include<stdio.h>
#include<stdlib.h>
#include<vector>
#include<iostream>
#include <bits/stdc++.h>
#include <sstream>
#include <string>
#include <cstring>
#include <bits/stdc++.h>
using namespace std;
//program for getting first, last and trading days from existing file
int main(){
ifstream infile("input.csv");
ofstream outfile("input.csv");
int nn,j;
cout<<" \n ENter the number of record for file";
cin>>nn;
string data;
int open,high,low,close, adjclose,volum;
//accept data from file
for(j=0;j<nn;j++){
   cout<<"\n Enter Date,open,high,low,close, adjclose,volum for record"<<j<<"\n";
   cin>>data>>open>>high>>low>>close>>adjclose>>volum;
   outfile<<data<<","<<open<<","<<high<<","<<low<<","<<close<<","<<adjclose<<","<<volum<<"\n";
  
}
outfile.close();
string line;
char *Lastday;
int startfl=0,edflag=0,nulllinecount=0,linecount,paracount=7,count=0;
while (getline(infile, line))
{
//cout<<line<<"\n";
int n = line.length();
// declaring character array
char char_array[n + 1];
//convert string to char array
strcpy(char_array, line.c_str());

linecount++;
if(strstr(char_array, "null") != NULL) {// if line consist null then increase nullcount by 1
    nulllinecount++;
    continue;
}
//divide line it by , comma
char *token = strtok(char_array, ",");
int i=0;
while (token != NULL)//till end of file
    {
        //cout<<"\n"<<(nn-1)<<"---"<<linecount<<"-"<<i;
        if(i==0 && startfl==0){// condition for start trading day
        printf("Start Trading Day is %s\n", token);
      
       startfl=1;  
       }
  
       else if((nn-1)==linecount && i==0){// condition for start trading day
  
       printf("Last Trading Day is %s\n", token);
       }
        i++;
        token = strtok(NULL, ",");
      
   }

}
printf(" Trading Days are %d ",linecount-nulllinecount+1);
infile.close();
}

/*

output

input.csv

*/

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that will input data from a Comma-separated values (.csv) file and output...
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
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