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
  • Write a C++ program that will input data from a Comma-separated values (.csv) file and output som...

    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...

  • According to Wikipedia , a comma-separated values (CSV) file is a delimited text file that uses...

    According to Wikipedia , a comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. A CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format. A company has text data that is not...

  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • Description Write C++ code that correctly input and output information. (Moving data to and from the...

    Description Write C++ code that correctly input and output information. (Moving data to and from the screen and to and from text files. Also inputting predefined functions, etc., from included libraries.) Problem Description Consider a data file that has geometrical angle values in degrees. Angle values may be on one line, several lines, or any other white space separated format. Fig. 1 below shows one possible format, but other formats are possible. 12.9 100.8 270.5 300.6 120.8 There are no...

  • Write a program that will first receive as input the name of an input file and an output file. It will then read in a list of names, id #s, and balances from the input file specified (call it InFile.t...

    Write a program that will first receive as input the name of an input file and an output file. It will then read in a list of names, id #s, and balances from the input file specified (call it InFile.txt) which you will create from the data provided below. The program will then prompt the user for a name to search for, when it finds the name it will output to a file (call it OFile.txt) the person’s id#, name,...

  • 1. Write a C++ program that reads daily weather observation data from a file and writes...

    1. Write a C++ program that reads daily weather observation data from a file and writes monthly and annual summaries of the daily data to a file. a. The daily weather data will be contained in a file named wx_data.txt, with each line of the file representing the weather data for a single day. b. For each day, the date, precipitation amount, maximum temperature, and minimum temperature will be provided as tab-separated data with the following format: 20180101 0.02 37...

  • Write a C program that takes inventory data from a file and loads a structure of up to 100 items ...

    Write a C program that takes inventory data from a file and loads a structure of up to 100 items defined as: struct item { int item_number; char item_name[20]; char item_desc[30]; float item_price; } I called mine: struct item inventory[100]; (you may use any name you wish) I will let you decide the appropriate prompts and edit messages. You will read the data from the data file and store the info in an array. Assume no more than 100 records...

  • Use C++ For this week’s lab you will write a program to read a data file...

    Use C++ For this week’s lab you will write a program to read a data file containing numerical values, one per line. The program should compute average of the numbers and also find the smallest and the largest value in the file. You may assume that the data file will have EXACTLY 100 integer values in it. Process all the values out of the data file. Show the average, smallest, largest, and the name of the data file in the...

  • C PROGRAM, A FOPEN FILE NEED TO BE CREATED Objective To review reading from a file....

    C PROGRAM, A FOPEN FILE NEED TO BE CREATED Objective To review reading from a file. To use records (an instance of a struct) To use Dynamic Memory Allocation To create and use a dynamically allocated array of structs To use enumerated types in a useful manner (more info given on Discussion board!) The Problem Bad economic times has forced the UCF administration to think outside the box for alternative methods of income. Specifically, we now have a UCF lottery,...

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