I'm really hoping there is someone out there with more C++ experience than me (a total of 3 days now) who can take a few minutes to look over my code (below) and offer some advice as to why it's not operating correctly. I used Codeblocks (Ver 10.50) and whatever the latest compiler which comes with the download. I'm using Win 7 as my OS.
I've compiled the code and don't receive any warnings or erros, yet when the program is run, the MS DOS window appears to display my output, yet there is none....nothing. I'm sure my error is some stupid little something I'm overlooking, but I'll be darned if I can find it. I've also posted the data file I'm trying to read from at the bottom.
I removed much of the program comments due to the size of the question
Thank you for any help you can offer.
Program
=============
// THE INPUT DATA FILE IN THIS EXAMPLE IS A HYPOTHETICAL DATASET OBTAINED FROM A METEOROLOGICAL STATION.
// THE PROGRAM READS EACH PARAMETER AND STORES THESE PARAMETERS INTO ONE OF SEVEN VECTOR/ARRAYS. THE PROGRAM SENDS THE OUTPUT FROM EACH ARRAY TO THE CONSOLE AND WRITES (not yet done) THE OUTPUT TO A FILE IN THE SAME SUBFOLDER NAMED "DATA_OUT.TXT" (not yet complete).
#include <iostream> // Needed for input and output stream operations
#include <fstream> // Needed for the ifstream class
#include <iomanip> // Needed for the setw stream manipulator
#include <vector> // Needed for working with vectors/arrays
using namespace std;
// Use a structure named "Date" to store the recorded date (i.e. Year, Month, Day).
// Declare array member types. The year, month, and day are all integer values.
struct Date {
unsigned int year;
unsigned int month;
unsigned int day;
};
int main()
{
// STEP1: OPEN THE FILE TO BE READ
// Use ifstream as an object reprenting an input file stream. ifstream allows
// the program to read the data file "Data_in.text"
ifstream file_to_read("Data_in.txt");
// The data file contains two (2) header lines we don't want to read.
// Assign the maximum number of characters expected in a single line
// in the header to skip as 512.
const int max_num_of_char_in_a_line = 512, num_of_header_lines = 2;
// STEP2: SKIP THE FIRST 2 HEADER LINES
for(int i = 0; i < num_of_header_lines; ++i)
// The ignore member function ignores the number of characters in its first argument
// or until the character in the 2nd argument "the newline character", is reached.
// STEP3: READ THE DATA FILE. STORE THE DATA READ INTO EACH ARRAY
// There are 9 arrays to store data. Three vector objects are used for the structure
// "date" instead of a traditional array because vector objects can grow dynamically
// in size at runtime while traditional arrays have a fixed size. Note: The type of
// elements stored in a vector is specified in the template argument
// that is between (< and >).
vector<Date> date_array;
// Declare six vectors, and the type of data to be stored in each.
// Ignore the first parenthesis (") character in the data file.
file_to_read.ignore(1);
// Read only the first 4 characters of the data file (year) and store them
// in "date_read.year". NOTE: The set with (setw) stream manupulator tells
// the stream to read only 4 characters
file_to_read >> setw(4) >> date_read.year;
// Ignore the dash "-" character between the year and the month in the data file.
file_to_read.ignore(1);
// Read and store the next two characters (month) from the data file.
file_to_read >> setw(2) >> date_read.month;
// Ignore the dash character "-" between the month and the day in the data file
file_to_read.ignore(1);
// Read and store the next two characters (day) in the data file.
file_to_read >> setw(2) >> date_read.day;
// Ignore the second parenthesis (",) and the comma characters after the day
file_to_read.ignore(2);
// Store the Date structure read, date_read, into the end of time_array
date_array.push_back(date_read);
int winddir,time;
float windspeed, temp2m, temp10m, solarrad;
// Read the time (hours), wind speed, wind direction, temperature at 2 meters,
// temperature at 10 meters, and solar radiation values and store them in
// volt1, volt2 and volt3
// NOTE: remeber the default behaviour of a stream is to read until
// the space character is reached
// Print the date (year-month-day) that were read. Note: A vector
// object knows its size and can be retreived by, and call to the
// size() member function
cout << "The Years that were read are:\n";
for(unsigned int i = 0; i < date_array.size(); i++)
cout << setw(4) << date_array[i].year << ' ' << setw(2) << date_array[i].month << ' ' << setw(2) << date_array[i].day << '\n';
// Print the time, wind speed, wind direction, temp at 2 m, temp at 10m, and solar radiation
cout << "\n The Time-hours are:\n";
for(unsigned int i = 0; i < time_array.size(); i++)
cout << time_array[i] << '\n';
cout << "\n The Wind Speeds are :\n";
for(unsigned int i = 0; i < windspeed_array.size(); i++)
cout << windspeed_array[i] << '\n';
cout << "\n The Wind Directions are :\n";
for(unsigned int i = 0; i < winddir_array.size(); i++)
cout << winddir_array[i] << '\n';
cout << "\n The Temperature at 2 meters are:\n";
for(unsigned int i = 0; i < temp2m_array.size(); i++)
cout << temp2m_array[i] << '\n';
cout << "\n The Temperature at 10 meters are:\n";
for(unsigned int i = 0; i < temp10m_array.size(); i++)
cout << temp10m_array[i] << '\n';
cout << "\n The Solar Radiation values are:\n";
for(unsigned int i = 0; i < solarrad_array.size(); i++)
cout << solarrad_array[i] << '\n';
This line: file_to_read >> time >> windspeed >> winddir >> temp2m >> temp10m >> solarrad; is wrong. It expects the values separated by space. So the input is wrong