Question:
Reading the correct line from a txt file c++?
Erik
2013-05-29 17:21:15 UTC
I have a question for an assignment that is kind of driving me crazy figuring out. Basically, I have the user input their birth date as ddmmyyyy, and output it as month, dd, yyyy. So 05/01/1990 becomes January 5th, 1990. Instead of having the months as strings, and using if statements, we have a text file called "months" that has the list of months with their corresponding numbers. The file basically looks like this:

01January
02February
03March

and so on.

Im just not sure what data type I should use for the input of the date, and how to compare the 2 characters in the middle of that input, and search for those characters in text file. I understand I need to use month.Stringfind(), but Im quite new to programming so Im not sure how. This is what I got so far.

// reading a text file
#include
#include
#include
using namespace std;

int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}

else cout << "Unable to open file";

return 0;
}

This is the first time my prof has taught a class, and she is wayy behind in our lectures, so the assignments are way more complicated than what she has taught us so far. Thats why I need to look through the internet for help, so any help is greatly appreciated!
Three answers:
cja
2013-05-31 06:24:03 UTC
Normally you'd just declare and and initialize an array for this, a static lookup table for month names. Initializing the lookup table from a file is a good exercise, though, and a good application for a map. You can try something like this:



#include

#include

#include

#include

#include



using namespace std;



int main(int argc, char *argv[]) {

    map theMap;

    map::iterator i;

    int key;

    string filename, in, value;

    stringstream ss;



    cout << "Enter filename: ";

    getline(cin, filename);

    ifstream inFile(filename.c_str());

    if (inFile.is_open()) {

        while (inFile) {

            getline(inFile, in);

            ss.clear(); ss.str(in);

            if (ss >> key >> value) {

                theMap.insert(pair(key, value));

            }

        }

        inFile.close();

        while (true) {

            cout << "> ";

            getline(cin, in);

            ss.clear(); ss.str(in);

            if ((ss >> key) && ((i = theMap.find(key)) != theMap.end())) {

                cout << i->first << " : " << i->second << endl;

            }

        }

    } else {

        cout << filename << " not found." << endl;

    }

    return 0;

}



#if 0



Enter filename: months.txt

> 2

2 : February

> 1

1 : January

> 13

> 8

8 : August

> x

> 4

4 : April

>



#endif
husoski
2013-05-29 21:31:53 UTC
The normal data structure for quick lookup based on a small number like month (1..12) is an array. A string[12] array, for example could be accessed by month-1 to convert to a 0-based index. The problem comes in splitting up the number and the name part of each file line.



C++11 has string-to-number conversions in the header, so you can use:



int mm = stoi(line.substr(0,2));



...to get the month as an integer, then store that in the array with:



month_names[mm-1] = line.substr(2);



Either way,



An alternate method that works prior to C++11, and is simpler to type, is to use atoi from the C header:



int mm = atoi( line.c_str() );



There are other ways, too. Whichever you choose, you can verify a good file line with:



if (mm < 1 || mm > 12) { ...the file line is no good ... }
?
2016-10-29 08:03:02 UTC
C treats strings as an array of characters so which you would be able to study each and each string into 2 variables and in the present day output them to the reveal screen by using fact the 1st 2 lines. Then study character positions one by utilising one and strengthen a third string made up of + and - preserving a count form of the + whilst comprehensive output the third string to the reveal screen accompanied by utilising the proportion calculation effect.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...