Question

Language is C++ I am reading in data from a file. In the .txt file is...

Language is C++

I am reading in data from a file.

In the .txt file is a series of dates.

I need help converting dates from the format "20-Apr-18" to the standard "4/20/18"

I've gotten the rest taken care of, I just need help converting the dates from the file. Thank you.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program to convert the Format of date:

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main() {
int sum , i = 0;
string word;
ifstream inFile;

// Name of the months associated with its index value in the array.
string months [12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
inFile.open("test.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}  
// moving the file value to the variable word.
while (inFile >> word)

// Extracting month, date, year from the given format.
{
string date = word.substr(1, 2);
string year = word.substr(8, 2);
string month = word.substr(4, 3);
for (i=0; i < 12; i++){
if (months[i] == month)
{

// converting the integer index value to string, to concatenate with year and month.
string str= to_string(i);

// concatenating the year, month and date.
cout << str + "/" + date + "/" + year;
cout << "\n";
}
  
}
}
  
inFile.close();

return 0;
}

Sample File content:

File Name : test.txt

"20-Apr-18
"20-Jul-18
"20-Aug-18
"20-Sep-18
"20-Nov-18

Output:

InputTextFile:

Add a comment
Know the answer?
Add Answer to:
Language is C++ I am reading in data from a file. In the .txt file is...
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
  • i need help with a c++ program, it will be reading characters in from a file...

    i need help with a c++ program, it will be reading characters in from a file using file stream while the program is reading chartacter by charecter , it will identify what is it reading so say a file contains this x = 10; (This is one line) y = 20;    (This is one line)                                                                                                                                                                                                                             print x +y; (This is one line)                                                                                                                                                                                                                    the outcome would be this x is a word 10 is a number y is a word...

  • Help on writing a MATLAB code. Let's say i am given a .txt file and in...

    Help on writing a MATLAB code. Let's say i am given a .txt file and in that text file there are 26 rows that represent individual students grades and then 7 columns that represent specific homework assignments. We want to ask the user to select a specific homework assignment and output the mean and standard deviation for that assignment. Then we want to ask the user to select an individual student and output the mean and standard deviation for that...

  • In C++, how would I read data from a .txt file into an array? For example,...

    In C++, how would I read data from a .txt file into an array? For example, I have an assignment that requires me to read temperatures from a file named data.txt into an array. How would I go about doing this?

  • I am having problems with reading a file into an array. This is my code. This...

    I am having problems with reading a file into an array. This is my code. This is what I get when I run my program. But this is my text file I am reading. I tried everything and it seems to be reading in the last digit of the file. I want to read in their names line by line into an array and ultimatly also read in the scores line by line. 1 E/7 Programming Assignment 6.cpp Defines the...

  • I am having trouble trying to output my file Lab13.txt. It will say that everything is...

    I am having trouble trying to output my file Lab13.txt. It will say that everything is correct but won't output what is in the file. Please Help Write a program that will input data from the file Lab13.txt(downloadable file); a name, telephone number, and email address. Store the data in a simple local array to the main module, then sort the array by the names. You should have several functions that pass data by reference. Hint: make your array large...

  • I am using python3.6 and i am getting an error on line 42 num = int(fin.readline())...

    I am using python3.6 and i am getting an error on line 42 num = int(fin.readline()) #reading first value valueError: invalid literal, for int() with base 10 Any help is appreciated, here is the code. reads from txt file a few integers in an array and sorts them. thank you! # Function to do insertion sort def insertionSort(arr):     # Traverse through 1 to len(arr)     for i in range(1, len(arr)):         key = arr[i]         # Move elements of...

  • Hey! i hit a roadblock in my C# program. I am just wondering how i would...

    Hey! i hit a roadblock in my C# program. I am just wondering how i would search for a set of 9 numbers within an array and have a message box say if it was there or not. I have it where the contents of the txt file is loaded into an array right when the program starts. Any and all help is greatly appreciated :) each number in the txt file is like this 123445567 385910475 938405719 503818405 the...

  • I am trying to read from a file with with text as follows and I am...

    I am trying to read from a file with with text as follows and I am not sure why my code is not working. DHH-2180 110.25 TOB-6851 -258.45 JNO-1438 375.95 CS 145 Computer Science II IPO-5410 834.15 PWV-5792 793.00 Here is a description of what the class must do: AccountFileIO 1. Create a class called AccountFileIO in the uwstout.cs145.labs.lab02 package. (This is a capital i then a capital o for input/output.) a. This class will read data from a file...

  • FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data...

    FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data as arguments (parameters) for a number of functions you will write. You will need to: Write and test a function square_each(nums) Where nums is a (Python) list of numbers. It modifies the list nums by squaring each entry and replacing its original value. You must modify the parameter, a return will not be allowed! Write and test a function sum_list(nums) Where nums is a...

  • Using c 3 File Input & Data Processing Reading data from a file is often done in order to pro...

    using c 3 File Input & Data Processing Reading data from a file is often done in order to process and aggregate it to get ad- ditional results. In this activity you will read in data from a file containing win/loss data from the 2011 Major League Baseball season. Specifically, the file data/mlb_nl_2011.txt contains data about each National League team. Each line contains a team name fol- lowed by the number of wins and number of losses during the 2011...

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