473,327 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Program crashing while executing a certain part of the code.

13
Hi guys
I need some help please in c++. I'm trying to process different files so have created a main, MonthlyFiles and DaysData files. My problem is when I run the program it crashes when trying to save the output file (i.e. the DaysData - it ouputs the information to the screen and then stops responding.

Main:
Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. #include <stdlib.h>
  3. #include "MonthlyFiles.h"
  4. #include "DaysData.h"
  5.  
  6.  
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.     int option;
  13.     MonthlyFiles m;
  14.  
  15.     do
  16.     {
  17.     cout<<"Please select a number from the menu below to execute the needed operation:" << '\n';
  18.  
  19.     cout<<"1: To process August Electricity consumption for each day."<<'\n';
  20.     cout<<"2: To process September Electricity consumption for each day."<<'\n';
  21.     cout<<"3: To process the Electricity consumption for the two months together."<<'\n';
  22.     cout<<"4: Quit"<<'\n';
  23.     cout<<'\n';
  24.     cout<<"Please input option: ";
  25.     cin>> option;
  26.     cout<<'\n';
  27.  
  28.     switch(option)
  29.     {
  30.         case 1:m.process();
  31.                break;
  32.  
  33.         case 2:m.processSept();
  34.                break;
  35.  
  36.         case 3:m.processboth();
  37.                break;
  38.         case 4:cout<<"bye";
  39.                 exit(0);
  40.  
  41.         default:
  42.                       cout<<"Please enter a valid option";
  43.                       cout << '\n';
  44.                       break;
  45.  
  46.     }
  47.  
  48.     }while(option!=1);
  49. return 0;
  50. }
  51.  
  52.  
MonthlyFiles.cpp:
Expand|Select|Wrap|Line Numbers
  1. #include "MonthlyFiles.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <sstream>
  6. #include <vector>
  7.  
  8.  
  9. void MonthlyFiles::Clear()
  10. {
  11.     string temp = "";
  12. }
  13.  
  14. std::string MonthlyFiles::process()
  15. {
  16.     ifstream infile;
  17.     vector<string> fileNames = vector<string>();            //Creates a new array called fileNames to store the names of the filenames
  18.     vector<DaysData> dataVector = vector<DaysData>();       //Creates a new array called DataVector to store the data of each day.
  19.     infile.open( "Aug.txt");             // Retrieves the file names for a the month from a particular txt.
  20.     if( !infile)
  21.     {
  22.         cout << "Cannot open input file"<< endl;  // If file cannot open display an error message.
  23.         //return -1;
  24.     }
  25.     while( !infile.eof())      //loop through the file till the end of the contents
  26.     {
  27.         getline(infile, temp);  // Process each line
  28.  
  29.         fileNames.push_back(temp);    //Add each filename to the array
  30.  
  31.     }
  32.     int i;
  33.     for(i = 0; i < fileNames.size(); i ++)
  34.     {
  35.         //This part of the code opens each file up and adds it to a new Daysdata array
  36.         ifstream dataFile;
  37.         string fileLoc = "Data/"; //The location of the file.
  38.  
  39.         dataFile.open((fileLoc+fileNames.at(i)).c_str());   //This opens the new data file for a particular file.
  40.         if(dataFile.is_open())
  41.         {
  42.             DaysData d = DaysData();
  43.  
  44.             dataFile >> d;
  45.             dataVector.push_back(d);
  46.         }
  47.     }
  48.  
  49. }
  50.  
  51.  
DaysData.cpp
Expand|Select|Wrap|Line Numbers
  1. #include "DaysData.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <sstream>
  6. #include <vector>
  7.  
  8.  
  9. DaysData::DaysData()
  10. {
  11.  
  12. }
  13.  
  14. void DaysData::Clear()
  15. {
  16.     string line = "";
  17. }
  18.  
  19. void operator >>(istream & infile, DaysData & D)
  20. {
  21.     string m_csvline;
  22.     ofstream writeFile;
  23.     writeFile.open("2001-Aug.csv", ios::app);
  24.         while(!infile.eof())
  25.     {
  26.         getline(infile, m_csvline);  // Retrieves each line in the csv file.
  27.         string tempcsv = m_csvline;       //assigns it to a string variable
  28.  
  29.         D.myVec.push_back(tempcsv);      //adds the data from above into the vector.
  30.  
  31.     }
  32.  
  33.     string line1=D.myVec.at(9);        
  34.     string lastline=D.myVec.at(D.myVec.size()-2);    
  35.  
  36.  
  37.     D.date.setDates(line1);                               
  38.     D.date.setDates(lastline);                            
  39.     float powerline1=D.powers.setPower(line1);             
  40.     float powerlastline=D.powers.setPower(lastline);       
  41.  
  42.     float power=powerlastline-powerline1;                     
  43.     cout<<D.date.getDates() <<";"<< power<<endl;              
  44.  
  45.     //This section here concatenates temp1 so that it can be sent to the output file.
  46.     stringstream temp1;
  47.     temp1 << D.date.getDates() << ";" << power << '\n';
  48.     string out = temp1.str();
  49.  
  50.  
  51.     writeFile << out;               //write the data to the output file
  52.     writeFile.close();
  53.  
  54. }
  55.  
  56.  
Thanks for you help, really appreciate it.
May 28 '12 #1
4 1807
weaknessforcats
9,208 Expert Mod 8TB
Have you stepped through the code using your debugger?
May 28 '12 #2
deenar
13
Yes I have and it stops there, I may have a feeling that my Monthly.cpp does not have return statement that's why it crashes after creating the file..sorry this is an assumption as I am not to good at C++.
May 28 '12 #3
deenar
13
I've managed to sort it out. thank-you for your time.

changed:
std::string MonthlyFiles::process()
to
void MonthlyFiles::process()
May 28 '12 #4
weaknessforcats
9,208 Expert Mod 8TB
Part of the probem is that you are trying to use the return type for both reporting errors and to return data. By changing the return type to void you can no longer return error codes.

My personal style is to always use the return type for error codes. To return data I use an argument by reference.

I also do not use hard-coded error values, like -1 because -1 can have different meanings in different functions.

Instead I use an enum table I keep in a header file:

Expand|Select|Wrap|Line Numbers
  1. enum SysReturn {SYS_FILE_NOT_FOUND = -1, SYS_OUT_OF_RANGE = -1};
enum names are just named int values and not different variables so you use the header file. I tend to use the same enum table in all programs.

Now your process function can return SYS_FILE_NOT_FOUND which makes the cde easier to read.
May 28 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Daragoth | last post by:
Hi, I'm writing a program using Metrowerks CodeWarrior 4.0 that determines the best possible combination of a data set by checking every possible combination. I found there were about 250,000,000...
2
by: Joe Bloggs | last post by:
I have a Windows form application that has a start button, a progress bar and also writes information to the status bar the code is C#. My problem is that when you hit the start button and the...
0
by: Microsoft | last post by:
I have a porgram that opens an excel workbook and modifies some data, but almost randomly a box will pop up in the workbook telling me the file is now available and to hit ok or cancel. This...
6
by: Chris | last post by:
Could someone point me to an example or at least outline of a solution to the following problem: I want to be able to compile the body of a method written in C++, submitted by a possibly...
4
by: vashwath | last post by:
Hi all, I am not able to figure out why the following program fails. Hope this long program does not irritate you. #include <stdlib.h> #include <string.h> #include <stdio.h> #define...
9
by: santosh | last post by:
Hello all, I've put together a small program to count the number of characters and 'words' in a text file. The minimum length of a word, (in terms of no. of characters), as well as word...
0
by: Rain | last post by:
how do i place a button control on a certain part(using coordinates) in an image... in short, i want the functions of a button in a certain part of a image that in going to be placed in a form in...
1
by: nbs.tag | last post by:
Hi, ive created a webservice that im going to use to filter a table, however when i try to pass the parameters in that will be used, it crashes in the webbrowser with: ...
8
by: lovecreatesbea... | last post by:
K&R 2, sec 2.4 says: If the variable in question is not automatic, the initialization is done once only, conceptually before the program starts executing, ... . "Non-automatic variables are...
9
by: Hypnotik | last post by:
Ok, so I've finished the "library" problem. The program takes in input and sorts the information. However the program crashes if I enter an authors name beginning with the letter z. Also if the book...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.