//HI,need help to extract info from text file and display.
//Here is my text file called Report.txt
01 09 2014 john 3.4 12.5 9.5
02 09 2014 freddy 3.4 12.5 9.5
03 09 2014 conor 3.4 12.5 9.5
04 09 2014 mike 3.4 12.5 9.5
05 09 2014 john 3.4 12.5 9.5
06 09 2014 john 3.4 12.5 9.5
07 09 2014 mike 3.4 12.5 9.5
08 09 2014 freddy 3.4 12.5 9.5
09 09 2014 conor 3.4 12.5 9.5
10 09 2014 conor 3.4 12.5 9.5
11 09 2014 john 3.4 12.5 9.5
12 09 2014 mike 3.4 12.5 9.5
13 09 2014 john 3.4 12.5 9.5
14 09 2014 john 3.4 12.5 9.5
15 09 2014 mike 3.4 12.5 9.5
16 09 2014 conor 3.4 12.5 9.5
17 09 2014 conor 3.4 12.5 9.5
18 09 2014 fredyy 3.4 12.5 9.5
19 09 2014 freddy 3.4 12.5 9.5
20 09 2014 mike 3.4 12.5 9.5
21 09 2014 conor 3.4 12.5 9.5
22 09 2014 conor 3.4 12.5 9.5
23 09 2014 mnike 3.4 12.5 9.5
24 09 2014 mike 3.4 12.5 9.5
25 09 2014 john 3.4 12.5 9.5
26 09 2014 conor 3.4 12.5 9.5
27 09 2014 freddy 3.4 12.5 9.5
28 09 2014 freddy 3.4 12.5 9.5
29 09 2014 mike 3.4 12.5 9.5
30 09 2014 john 3.4 12.5 9.5
So it,s a date,name,hours of sunshine (double)
rainfall in millimeters (double)
midday temperature in degrees (double)
Here is the code:
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
struct DATE{
int day;
int month;
int year;
};
struct onDutyRecords{
string name;
double sunshine;
double rainfall;
double midTemp;
DATE date;
};
int ShowMenu(void);
void ListDaysOnDutyOfMet(void);
void ListAllTheWetDays(void);
void ListDaysOnDutyOfMet(void);
void ListAllTheSunnyDays(void);
void ListAverageOfTwoDays(void);
void ListHotestDayByMiddayTemp(void);
void ListDaysWithShortestSunshine(void);
void Exit(void);
bool quitFlag = false;
void LoadFile(void);
int _tmain(int argc, _TCHAR* argv[])
{
int option;
do
{
option = ShowMenu();
switch (option)
{
case 0:
ListDaysOnDutyOfMet();
break;
case 1:
ListAllTheWetDays();
break;
case 2:
ListAllTheSunnyDays();
break;
case 3:
ListAverageOfTwoDays();
break;
case 4:
ListHotestDayByMiddayTemp();
break;
case 5:
ListDaysWithShortestSunshine();
break;
case 6:
Exit();
break;
default:
cout << "invalid option\n";
}
} while (!quitFlag);
return 0;
}
int ShowMenu(void)
{
int option;
cout << "\n\n\n";
cout << "\t0. Do Days On Duty Of Meteorologist\n";
cout << "\t1. Display All The Wet Days\n";
cout << "\t2. All The Sunny Days\n";
cout << "\t3. Average Of Two Days\n";
cout << "\t4. Hotest Day By Midday Temp\n";
cout << "\t5.\tDays With Shortest Sunshine\n";
cout << "\t6. Exit\n";
cout << "\t\tEnter option : ";
cin >> option;
cout << endl << endl;
return option;
}
void ListDaysOnDutyOfMet(void)
{
cout << "Not impl";
}
void ListAllTheWetDays(void)
{
cout << "Not impl";
}
void ListAllTheSunnyDays(void)
{
cout << "Not impl";
}
void ListAverageOfTwoDays(void)
{
cout << "Not impl";
}
void ListHotestDayByMiddayTemp(void)
{
cout << "Not impl";
}
void ListDaysWithShortestSunshine(void)
{
cout << "Not impl";
}
void Exit(void)
{
quitFlag = true;
}
void LoadFile(void)
{
ifstream infile("Report.txt");
if (!infile)
{
cout << "Database file does not exist.";
}
else
{
struct DATE Date;
infile >> Date.day >> Date.month >> Date.year;
struct onDutyRecords record;
infile >> record.name >> record.sunshine >> record.rainfall >> record.midTemp;
}
}
As you can see at the end need somehow to merge this structs and display,so i could use other functions.
aluhnev
0
Junior Poster in Training
Recommended Answers
Jump to PostYour first step would to use the date property of the onDutyRecords instead of a separate DATE object. This will put the data for one line in one struct object.
Something like this should work:
struct onDutyRecords record; infile >> record.date.day >> record.date.month >> record.date.year; infile …
Jump to PostI see nothing in your code about where you're calling the LoadFile function or displaying the data.
You can display the data with
cout
, something like this:cout << record.date.day << "/" << record.date.month << "/" << record.date.year << " - "; cout << record.name …
Jump to PostFirst off, your code doesn't call
LoadFile()
, so the vector doesn't get populated and there is nothing to find.Secondly, using
find
, you're trying to compare a string(name
) with anonDutyRecords
object, which will never find a match. You probably need to use thefind_if
…
Jump to PostDid you add the ToString method to your struct, as per the code I showed you?
This code was compiled and tested of VS2013. It should work as is, in its entirety.
All 13 Replies
tinstaafl
1,176
Posting Maven
aluhnev
0
Junior Poster in Training
tinstaafl
1,176
Posting Maven
aluhnev
0
Junior Poster in Training
tinstaafl
1,176
Posting Maven
aluhnev
0
Junior Poster in Training
tinstaafl
1,176
Posting Maven
aluhnev
0
Junior Poster in Training
tinstaafl
1,176
Posting Maven
aluhnev
0
Junior Poster in Training
tinstaafl
1,176
Posting Maven
aluhnev
0
Junior Poster in Training
tinstaafl
1,176
Posting Maven
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.