6 Replies - 403 Views - Last Post: 30 May 2013 - 08:36 PM Rate Topic: -----

#1 Konovoloff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-May 13

Read from a text file, and return a string based on input

Posted 28 May 2013 - 04:16 PM

Alright, so Im trying to write a program for a homework assignment that takes the month in a format like ddmmyyyy and returns it in the format month day, year. So 04/01/1990 becomes January 4th, 1990. Im suppose to take a text file that contains the months in a list with the corresponding names beside them. For example the list in the text file looks like this:

01January
02February
.. and so on.

I was able to write the program before I realised I needed to access this list and read it from there. Im brand new to programming, and I have experienced with strings a bit, but not really input output. I hate to blame my prof for anything, but this is the first class shes ever taught, and wont answer any of my emails, so I have received next to no help, so I decided to look for help on the internet instead. I understand the algorithm I need to use, I just dont know enough to know how to open the file, and compare the user inputted date to the strings in the file, and return the certain one.

Should I use ifstream? for example

ifstream months;

months.open(filename.txt)




Beacuse my current understanding of ifstream is that it just takes some inputs from a file, it doesnt just read off of it. I also understand I should use getLine, and String.find functions (as I was instructed to). Im pretty lost, so any and all help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Read from a text file, and return a string based on input

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1343
  • View blog
  • Posts: 4,604
  • Joined: 19-February 09

Re: Read from a text file, and return a string based on input

Posted 28 May 2013 - 05:04 PM

Is there a space between the number and month - 01January?

Using ifstream is good.

Input/Output with files
Was This Post Helpful? 0
  • +
  • -

#3 Konovoloff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-May 13

Re: Read from a text file, and return a string based on input

Posted 28 May 2013 - 06:50 PM

View Post#define, on 28 May 2013 - 05:04 PM, said:

Is there a space between the number and month - 01January?

Using ifstream is good.

Input/Output with files


Nope, there is not.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4063
  • View blog
  • Posts: 12,546
  • Joined: 25-December 09

Re: Read from a text file, and return a string based on input

Posted 28 May 2013 - 07:37 PM

Show what you've tried.

The easiest way would be to use the extraction operator to extract the integer then the name. Remember the extraction operator will stop processing a number when it encounters a non-digit.

Jim
Was This Post Helpful? 1
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Read from a text file, and return a string based on input

Posted 28 May 2013 - 11:59 PM

View PostKonovoloff, on 28 May 2013 - 07:16 PM, said:

Alright, so Im trying to write a program for a homework assignment that takes the month in a format like ddmmyyyy and returns it in the format month day, year. So 04/01/1990 becomes January 4th, 1990. Im suppose to take a text file that contains the months in a list with the corresponding names beside them. For example the list in the text file looks like this:

01January
02February
.. and so on.

I was able to write the program before I realised I needed to access this list and read it from there. Im brand new to programming, and I have experienced with strings a bit, but not really input output. I hate to blame my prof for anything, but this is the first class shes ever taught, and wont answer any of my emails, so I have received next to no help, so I decided to look for help on the internet instead. I understand the algorithm I need to use, I just dont know enough to know how to open the file, and compare the user inputted date to the strings in the file, and return the certain one.

Should I use ifstream? for example

ifstream months;

months.open(filename.txt)




Beacuse my current understanding of ifstream is that it just takes some inputs from a file, it doesnt just read off of it. I also understand I should use getLine, and String.find functions (as I was instructed to). Im pretty lost, so any and all help would be appreciated.



What Jim is hinting at looks like this ...

( which may look a little tricky at first, but once you 'see' ... not so tricky at all. )


Quote

The easiest way would be to use the extraction operator to extract the integer then the name. Remember the extraction operator will stop processing a number when it encounters a non-digit.


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const string FNAME = "myMonths.txt";
// example of file contents ...
/*
01January
02February
.. and so on.
*/

// in main

int monthNumber;
string monthName;

ifstream fin( FNAME.c_str() );
if( fin )
{
   // gets number, stops at first non digit, then getline gets rest of line and eats '\n' char(s) at end //
   while( fin >> monthNumber && getline( fin, monthName ) )
   {
      // do something with monthNumber and monthName
      cout << monthNumber << ", " << monthName << endl;

   }
   fin.close();

}
else cout << "There was some problem opening file " << FNAME << endl;


This post has been edited by David W: 29 May 2013 - 12:32 AM

Was This Post Helpful? 0
  • +
  • -

#6 Konovoloff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-May 13

Re: Read from a text file, and return a string based on input

Posted 30 May 2013 - 03:40 PM

Okay this is the second time Ive looked for help for my program, I know I nearly got it but I cant figure it out. So right now Im trying to write a program that has the user input the date in the format dd/mm/yyyy and return it as month date, year. So 01/01/1990 becomes January 1st, 1990. I need to use a text file that has the names of the months beside their corresponding numbers. So the list of the text file looks like this:

01January 02February 03March

.. and so on.

So far I have this:

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
  string thedate; //string to enter the date
  string month; // this string will hold the month
  ifstream myfile ("months.txt");
 cout << "Please enter the date in the format dd/mm/yyyy, include the slashes: " <<      endl;
cin >> thedate;

month = thedate.substr( 3, 2 );
string newmonth;

if (myfile.is_open())
{
while ( myfile.good() )
{
  getline (myfile,newmonth);
newmonth.find(month);



      cout << newmonth << endl;

}
myfile.close();
}

else cout << "Unable to open file"; 

return 0;
}



So I have been able to extract the month from the user input, and store that as month, Im just not too sure how search the text file for that month, and return only the name of the month into a new string, only from that line. Right now if I enter 02/05/1990, it will output

05 05 05 05 05

.. for 12 lines.

Im new to programming so any help is appreciated.

Also, I am only about 3 weeks into my programming course, and we havnt really learned functions, or arrays yet. So if you do have any help to offer, please avoid arrays and functions. I also understand it is easier to not read from a text file, but this is a requirement from my class to read it from the text file, so I need it. Thanks

Thanks.
Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1343
  • View blog
  • Posts: 4,604
  • Joined: 19-February 09

Re: Read from a text file, and return a string based on input

Posted 30 May 2013 - 08:36 PM

Hi, you can test whether you have found the month or not.

string::find
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1