Changing Disk File Name Failed
This is my C++ homework for college...
My problem is when I use "change" the disk file doesn`t really change its name and I don`t know how to fix.
Anyone has any ideas? Thank you.
DUE TO THE LENGTH LIMIT, HERE IS THE FIRST PART OF MY CODE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#include <iomanip>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cctype>
using namespace std;
class WeatherStation
{
string StationDesignation;
double Temperature;
double AirQualityIndex;
double PollenCount;
public:
WeatherStation();
void SetDesignation(string ID) { StationDesignation = ID; }
void SetTemperature(double Degree) { Temperature = Degree; }
void SetPollenCount(double MeasuredCount) { PollenCount = MeasuredCount; }
void SetAQScore(double MeasuredIndex) { AirQualityIndex = MeasuredIndex; }
string GetDesignation() { return StationDesignation; }
double GetTemperature() { return Temperature; }
double GetPollenCount() { return PollenCount; }
double GetAQScore() { return AirQualityIndex; }
};
WeatherStation::WeatherStation()
{
StationDesignation = "";
Temperature = 0.0;
AirQualityIndex = 0.0;
PollenCount = 0.0;
}
class WeatherStationList
{
vector<WeatherStation> List;
string FileName;
public:
WeatherStationList() { FileName = "DEFAULT"; }
void AddWeatherStation();
void Input();
void HighLow();
void Daily();
void Pollen();
void Quality();
void SaveWeatherStationInfo();
void ReadWeatherStationInfo();
void ChangeFileName(string File) { FileName = File; }
void GetFileName();
string ShowFileName() { return FileName; }
};
void WeatherStationList::AddWeatherStation()
{
string ID = "";
WeatherStation Loc;
cout << "Enter Stations Designation Below, Stop To Quit." << endl;
while (ID != "stop")
{
cout << "Station Location: ";
getline(cin, ID);
int CommandLength = ID.size();
for (int i = 0 ; i < (int)CommandLength ; i++)
ID[i] = tolower(ID[i]);
if(ID != "stop")
{
Loc.SetDesignation(ID);
List.push_back(Loc);
}
}
}
void WeatherStationList::Input()
{
double Temp, Pol, Qua;
for(int i = 0 ; i < (int)List.size() ; ++i)
{
cout << "\nWeather Station: " << List[i].GetDesignation() << endl;
cout << "Temperature: ";
cin >> Temp;
List[i].SetTemperature(Temp);
cout << "Pollen Count: ";
cin >> Pol;
List[i].SetPollenCount(Pol);
cout << "Air Quality: ";
cin >> Qua;
List[i].SetAQScore(Qua);
}
cout << endl;
cin.ignore(100, '\n');
}
void WeatherStationList::HighLow()
{
int i;
double low = List[0].GetTemperature();
for(i = 1 ; i < (int)List.size() ; ++i)
low = List[i].GetTemperature() > low ? low:List[i].GetTemperature();
double high = List[0].GetTemperature();
for(i = 1 ; i < (int)List.size() ; ++i)
high = List[i].GetTemperature() < high ? high : List[i].GetTemperature();
cout << "\n\n===============================Temperature Report=============================="<<endl
<< "\n\t\t\tFahrenheit\tCelsius" << endl
<< "-------------------------------------------------------------------------------"
<< setprecision(4)
<< "\nLowest Temperature =\t\t" << low << "\t" << (low-32)*5/9 << endl
<< "-------------------------------------------------------------------------------"
<< "\nHighest Temperature =\t\t" << high << "\t" << (high-32)*5/9 << endl << endl
<< "-------------------------------------------------------------------------------\n\n";
}
void WeatherStationList::Daily()
{
int i;
double sum = 0, mean = 0;
for (i = 0 ; i < (int)List.size() ; ++i) sum += List[i].GetTemperature();
mean = sum/List.size();
cout << "\n\n===============================Temperature Report=============================="
<< "\n\n\t\t\tFahrenheit\tCelsius" << endl
<< "-------------------------------------------------------------------------------"
<< setprecision(4)
<< "\nMean Temperature =\t" << mean << "\t\t" << (mean-32)*5/9 << endl
<< "-------------------------------------------------------------------------------\n"
<< "Raw Data:\n\n";
for (i = 0 ; i < (int)List.size() ; ++i)
cout << List[i].GetDesignation() << "\t\t" << List[i].GetTemperature() << "\t\t" << (List[i].GetTemperature()-32)*5/9<<endl;
cout << "-------------------------------------------------------------------------------\n\n\n";
}
void WeatherStationList::Pollen()
{
int i;
for (i = 0 ; i < (int)List.size() ; ++i)
cout << List[i].GetDesignation() << "\t\t" << List[i].GetPollenCount() << endl;
cout << "-------------------------------------------------------------------------------\n\n\n";
}
void WeatherStationList::Quality()
{
int i;
for (i = 0 ; i < (int)List.size() ; ++i)
cout << List[i].GetDesignation() << "\t\t" << List[i].GetAQScore() << endl;
cout << "-------------------------------------------------------------------------------\n\n\n";
}
void WeatherStationList::SaveWeatherStationInfo()
{
int i;
fstream OutFile(FileName.c_str(), ios::out);
cout << "Saving to file..." << endl;
for(i = 0 ; i < (int)List.size() ; i++) {
OutFile << List[i].GetDesignation() << endl;
OutFile << List[i].GetTemperature() << endl;
OutFile << List[i].GetPollenCount() << endl;
OutFile << List[i].GetAQScore() << endl;
}
OutFile.close();
}
void WeatherStationList::ReadWeatherStationInfo()
{
fstream InFile(FileName.c_str(), ios::in);
cout << "Loading from file..." << endl;
WeatherStation Loc;
string StationDesignation;
double Temperature;
double PollenCount;
double AirQualityIndex;
while(!InFile.eof()) {
InFile >> StationDesignation;
InFile.ignore(100, '\n');
if(!InFile.eof()) {
InFile >> Temperature;
InFile >> PollenCount;
InFile >> AirQualityIndex;
Loc.SetDesignation(StationDesignation);
Loc.SetTemperature(Temperature);
Loc.SetPollenCount(PollenCount);
Loc.SetAQScore(AirQualityIndex);
List.push_back(Loc);
}
}
InFile.close();
}
|
Last edited on
AND HERE IS THE SECOND PART
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
class WeatherStationUI
{
WeatherStationList List;
public:
WeatherStationUI() { RunCommand(); }
void RunCommand();
void Menu();
void GetFileName();
void ChangeFileName();
};
void WeatherStationUI::RunCommand()
{
int i = 0;
string Command = "";
while (Command != "quit")
{
Menu();
cout << "Command: ";
getline (cin, Command);
int CommandLength = Command.size();
for (i = 0 ; i < (int)CommandLength ; i++)
Command[i] = tolower(Command[i]);
if (Command == "add")
List.AddWeatherStation();
if (Command == "post")
List.Input();
else if (Command == "highlow")
List.HighLow();
else if (Command == "daily")
List.Daily();
else if (Command == "pollen")
List.Pollen();
else if (Command == "quality")
List.Quality();
else if (Command == "save")
List.SaveWeatherStationInfo();
else if (Command == "load")
List.ReadWeatherStationInfo();
else if (Command == "show")
GetFileName();
else if (Command == "change")
ChangeFileName();
}
}
void WeatherStationUI::Menu()
{
cout << "Choices........................................................................" << endl
<< "-------------------------------------------------------------------------------"<<endl
<< "\tAdd.......puts an new WeatherStation object onto the list." << endl
<< "\tPost......allows the user to post values." << endl
<< "\tHighLow...displays the report of high and low temperatures." << endl
<< "\tDaily.....displays the daily report." << endl
<< "\tPollen....displays the daily pollen count." << endl
<< "\tQuality...displays the daily air quality report." << endl
<< "\tSave......copies the contents of the list to a disk file." << endl
<< "\tRead......copies the contents of a disk file to the vector." << endl
<< "\tQuit......exits the program." << endl
<< "\tShow......shows the name of the current data file." << endl
<< "\tChange....changes the name of the current data file." << endl
<< "-------------------------------------------------------------------------------\n";
}
void WeatherStationUI::GetFileName()
{
WeatherStationList List;
cout << "-------------------------------------------------------------------------------" << endl;
cout << "Current File Name: " << List.ShowFileName() << endl;
cout << "-------------------------------------------------------------------------------" << endl;
}
void WeatherStationUI::ChangeFileName()
{
string NewFile;
WeatherStationList List;
cout << "-------------------------------------------------------------------------------" << endl;
cout << "Current File Name: " << List.ShowFileName() << endl;
cout << "New File Name: ";
getline(cin, NewFile);
List.ChangeFileName(NewFile);
cout << "-------------------------------------------------------------------------------" << endl;
}
int main()
{
WeatherStationUI List;
}
|
ChangeFileName() just changes what file you are writing to, not any files actually on the disk.
But my teacher`s example shows exactly like that just without the problem... it`s supposed to change the name of the "File" I saved in the folder
Topic archived. No new replies allowed.