Advice needed

Hello C++ Forum,

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.

file_to_read.ignore(max_num_of_char_in_a_line, '\n');

// 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.

vector<float> windspeed_array, temp2m_array, temp10m_array, solarrad_array;
vector<int> time_array, winddir_array;

// Read the data file while the End Of File character is NOT reached

while(!file_to_read.eof()) {

// If an empty data line is encountered in the data fle, ignore it.

if(!isdigit(file_to_read.peek()))
{
file_to_read.ignore();
continue;
}

// Open and read the vector date_read

Date date_read;

// 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

file_to_read >> time >> windspeed >> winddir >> temp2m >> temp10m >> solarrad;

// Store each value into the appropriate array

time_array.push_back(time);
windspeed_array.push_back(windspeed);
winddir_array.push_back(winddir);
temp2m_array.push_back(temp2m);
temp10m_array.push_back(temp10m);
solarrad_array.push_back(solarrad);
}

// STEP4: SEND DATA READ AND STORED IN THE ARRAY TO THE CONSOLE

// Send the values in the arrays to the console (screen) to allow
// the user to validate the program is operating correctly

// Format the consle output for 4 characters only and include a
// "0" to fill the value if none exists.

cout.fill('0');
cout.precision(4);
cout.setf(ios::fixed);

// 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';


return 0;
}

// End Program


Data_In.txt
================
Year-Mo-Day,Time,Wind Spd,Wind Dir,Temp(2m),Temp(10m),Solar Rad
None,(Hour-AKST),(m/s),(deg),(Deg C),(Deg C),(W/m2)
"2011-02-27",20:00,21.2,11,-10,-41,0
"2011-02-27",21:00,9.3,12,-10,41,0
"2011-02-27",22:00,4.4,13,-40,-41,0
"2011-02-27",23:00,2.5,12,-42,-41,0
"2011-02-28",0:00,2.6,15,-41,-41,0
"2011-02-28",1:00,4.7,20,-39,-41,0
"2011-02-28",2:00,6.8,20,-10,-41,0
"2011-02-28",3:00,8.9,21,-10,-41,0
"2011-02-28",4:00,10.01,21,0,-41,0
"2011-02-28",5:00,12.02,25,0,-41,0.02
"2011-02-28",6:00,14.03,26,1,,-41,0.12
"2011-02-28",7:00,16.04,27,5,-41,2
"2011-02-28",8:00,22.05,28,6,-41,12
"2011-02-28",9:00,24.06,29,20,-41,22
"2011-02-28",10:00,40.07,30,25,-41,42
"2011-02-28",11:00,60.08,25,26,-41,62
"2011-02-28",12:00,60.09,20,27,-41,82
"2011-02-28",13:00,50.10,15,28,-41,100
"2011-02-28",14:00,52.11,10,29,-41,120
"2011-02-28",15:00,45.12,5,30,-41,120
"2011-02-28",16:00,44.13,0,31,-41,100
"2011-02-28",17:00,43.14,355,32,-41,90
"2011-02-28",18:00,42.15,350,33,-41,70
"2011-02-28",19:00,32.16,345,34,-41,50
"2011-02-28",20:00,32.17,345,34,-41,10
"2011-02-28",21:00,31.18,350,36,-41,5
"2011-02-28",22:00,30.19,355,37,-41,1
"2011-02-28",23:00,10.20,0,38,-41,0
"2011-03-01",0:00,9.21,27,5,-41,0
"2011-03-01",1:00,8.22,26,10,-41,0
"2011-03-01",2:00,7.23,25,15,-41,0
"2011-03-01",3:00,6.24,24,20,-41,0
"2011-03-01",4:00,5.25,23,25,-41,0
"2011-03-01",5:00,4.26,22,30,-40,0
"2011-03-01",6:00,3.27,22,35,-41,0
"2011-03-01",7:00,2.28,22,35,-41,0
"2011-03-01",8:00,1.28,22,30,-41,0
"2011-03-01",9:00,20.30,20,-40,-41,0
"2011-03-01",10:00,21.31,15,-40,-41,0
"2011-03-01",11:00,31.32,10-40,-41,0
"2011-03-01",12:00,41.33,5,-40,-41,0
"2011-03-01",13:00,51.34,0,-40,-41,0
"2011-03-01",14:00,61.35,0,-40,-41,0
Use code tags: [code]Your code[/code]

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

This really looks risky
1
2
3
4
5
if(!isdigit(file_to_read.peek()))
{
file_to_read.ignore();
continue;
}
and indeed leads to an endless loop.

I recommend using getline http://www.cplusplus.com/reference/string/getline/
With that you can read the file line by line and parse the lines using stringstream http://www.cplusplus.com/reference/iostream/stringstream/

You can also use string and substr http://www.cplusplus.com/reference/string/string/substr/
Topic archived. No new replies allowed.