If someone could help, that would be great!!
I am a beginner to coding, and taking a beginner class in C++. We have an assignment to produce code to gather a string of input from the user in which they are entering a date. We then have to extract parts of that string to make substrings, and display different formats for the date(I will add my code in here so you can see what I have done). It took me a long while plucking away at this to understand this part. You will see that at the end of my code I have opened a file months.txt. We were provided with a file which states months corresponding to dates. I.e. 01January 02February 03March 04April, and so on until December. Exactly how I have typed it is how it is in the file.
I understand how to open and extract what is in the file as a string. I have extracted this as a string called myMonth (as you can see in the code as well) NOW, this is where my brain wants to explode...
I am supposed to have the program search for the month in the file, matching that to the month the user has input earlier, and then use the number infront of that month. I understand the basics of using find(), and making substrings. But how on earth do you get the computer to correlate what the user has input for a month, to finding that in the file, and then using the correct number.
AH. Help! Sorry this is so long winded, I just wanted to be as detailed as possible. If someone can help, that would be GREATLY appreciated. Here is the code I have done so far:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string myDate; // Date input from user;
string myMonth; // Input from months.txt
string day; // These
string month; // Are
string date; // Substrings
string year; // Of myDate;
int a, b; // for finding positions in myDate
and making new substrings
cout << "Please enter a date in the following
format: Friday, February 05, 2013: " << endl;
getline (cin,myDate);
a = 0; // To find 'day'
b = myDate.find("," );
day = myDate.substr(a ,b);
a = b + 2; // To find 'month'
b = myDate.find(" ", a);
month = myDate.substr(a ,b-a);
a = b + 1; // To find 'date'
b = myDate.find("," , a);
date = myDate.substr(a ,b-a);
a = b + 2; // To find 'year'
b = myDate.find(" ", a);
year = myDate.substr(a ,b-a);
cout << month << " " << date << " was a " << day
<< " in " << year << endl; //
cout << day.substr(0,3) << ", " << month.substr
(0,3) << " " << date << " '" << year.substr
(2,2) << endl; // DAY
ifstream months; // declares input stream
months.open("mo nths.txt"); //opens months.txt
getline(months, myMonth); // Extracts string
myMonth from file
months.txt
cout << myMonth << endl;
return 0;
}
I am a beginner to coding, and taking a beginner class in C++. We have an assignment to produce code to gather a string of input from the user in which they are entering a date. We then have to extract parts of that string to make substrings, and display different formats for the date(I will add my code in here so you can see what I have done). It took me a long while plucking away at this to understand this part. You will see that at the end of my code I have opened a file months.txt. We were provided with a file which states months corresponding to dates. I.e. 01January 02February 03March 04April, and so on until December. Exactly how I have typed it is how it is in the file.
I understand how to open and extract what is in the file as a string. I have extracted this as a string called myMonth (as you can see in the code as well) NOW, this is where my brain wants to explode...
I am supposed to have the program search for the month in the file, matching that to the month the user has input earlier, and then use the number infront of that month. I understand the basics of using find(), and making substrings. But how on earth do you get the computer to correlate what the user has input for a month, to finding that in the file, and then using the correct number.
AH. Help! Sorry this is so long winded, I just wanted to be as detailed as possible. If someone can help, that would be GREATLY appreciated. Here is the code I have done so far:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string myDate; // Date input from user;
string myMonth; // Input from months.txt
string day; // These
string month; // Are
string date; // Substrings
string year; // Of myDate;
int a, b; // for finding positions in myDate
and making new substrings
cout << "Please enter a date in the following
format: Friday, February 05, 2013: " << endl;
getline (cin,myDate);
a = 0; // To find 'day'
b = myDate.find("," );
day = myDate.substr(a ,b);
a = b + 2; // To find 'month'
b = myDate.find(" ", a);
month = myDate.substr(a ,b-a);
a = b + 1; // To find 'date'
b = myDate.find("," , a);
date = myDate.substr(a ,b-a);
a = b + 2; // To find 'year'
b = myDate.find(" ", a);
year = myDate.substr(a ,b-a);
cout << month << " " << date << " was a " << day
<< " in " << year << endl; //
cout << day.substr(0,3) << ", " << month.substr
(0,3) << " " << date << " '" << year.substr
(2,2) << endl; // DAY
ifstream months; // declares input stream
months.open("mo nths.txt"); //opens months.txt
getline(months, myMonth); // Extracts string
myMonth from file
months.txt
cout << myMonth << endl;
return 0;
}
Comment