Reputation: 21
Hi this question might some a little bit stupid(the question title might not be not relevant going to change until found a perfect title), i had spend couple hours figuring this out.
i'll try to explain as details as i could. First i will give the code of mine first.
File 'main.cpp'
using namespace std;
void readFile(Vector<WindLog> &VecObj);
void showMenu(Vector<WindLog> &VecObj);
void option1(Vector<WindLog> &VecObj);
void option2(Vector<WindLog> &VecObj);
void option3(Vector<WindLog> &VecObj);
void option4(Vector<WindLog> &VecObj);
void option5();
int getUserChoice();
int getMonthInput();
int getYearInput();
int main()
{
Vector<WindLog> Obj;
readFile(Obj);
showMenu(Obj);
return 0;
}
void readFile(Vector<WindLog> &VecObj){
Date date;
Time time;
float s, sr;
WindLog wLog;
string token, filename, line;
ifstream myFile("testfile.csv");
getline(myFile, line); //to skip header information ;
if(myFile.is_open()){
while(getline(myFile, line))
{
stringstream ss(line); // <-- pas line to stream
getline(ss, token,'/'); // <--Token for day;
date.setDay(atoi(token.c_str()));
getline(ss, token,'/'); // <--Token for month;
date.setMonth(atoi(token.c_str()));
getline(ss, token,' '); // <--Token for year;
date.setYear(atoi(token.c_str()));
getline(ss, token,':'); // <-- Token for Hour;
time.setHour(atoi(token.c_str()));
getline(ss, token,','); // <-- Token for Minute;
time.setMinute(atoi(token.c_str()));
for(int i = 0; i<9; i++)
getline(ss, token, ',');
getline(ss, token,','); // <-- Token for WindSpeed
s = atof(token.c_str());
getline(ss,token,','); // <-- Token for Solar Radiation
sr = atof(token.c_str());
wLog = WindLog(date, time, s, sr);
VecObj.add(wLog);
}
}
else{cout<<"No File Opened"<<endl;}
}
void showMenu(Vector<WindLog> &VecObj){
int choice;
while(true){
cout << "\t\tOptions Menu" << endl;
cout << "<------------------------------------------->" <<endl;
cout << "1) Show Maximum Wind Speed for Specified Month and Year." << endl;
cout << "2) Show Average Wind Speed of Specified Year." << endl;
cout << "3) Show Total Radiation in kWh/m^2 for each Month of Specified Year." << endl;
cout << "4) Print Average Wind Speed and Total Solar Radiation for each month of Specified Year, to File('WindandSolar.csv')." << endl;
cout << "5) Exit Program." << endl;
choice = getUserChoice();
switch(choice)
{
case 1:option1(VecObj); break;
case 2:option2(VecObj); break;
case 3:option3(VecObj); break;
case 4:option4(VecObj); break;
case 5:option5(); break;
default:
cout<<"Invalid Choice, Try Again"<<endl;
}
}
}
int getUserChoice(){
int temp;
cout << "Enter Your Choice : ";
cin >> temp;
while(cin.fail()){
cout << "Error, Try Again. " << endl;
cout << "Enter Your Choice : ";
cin.clear();cin.ignore(256,'\n');
cin >> temp;
}
return temp;
}
int getMonthInput(){
int tempMonth;
bool verify = false;
while(verify==false)
{
cout << "Enter Month : ";
cin >> tempMonth;
if(cin.fail())
{
cout<<"Input Error, Try Again."<<endl;
cin.clear();cin.ignore(256,'\n');
}
else if(tempMonth < 1 || tempMonth > 12)
{cout<<"Invalid Month, Try Again."<<endl;}
else
{verify = true;}
}
return tempMonth;
}
int getYearInput(){
int tempYear;
bool verify = false;
while(verify==false)
{
cout << "Enter Year : ";
cin >> tempYear;
if(cin.fail())
{
cin.clear();cout<<"Input Error, Try Again."<<endl;
cin.ignore(256,'\n');
}
else if(tempYear<=0)
{cout<<"Invalid Year, Try Again."<<endl;}
else
{verify = true;}
}
return tempYear;
}
void option1(Vector<WindLog> &VecObj){}
void option2(Vector<WindLog> &VecObj){}
void option3(Vector<WindLog> &VecObj){}
void option4(Vector<WindLog> &VecObj){}
void option5(){exit(0);}
Let me explain ( if something not clear please give some comment i'll add more specific detail, just into c++ for about 1 month ).
at main i declare Vector<windLog> Obj
to store my windlog
file. then i need to pass it to void readFile(Vector<WindLog> &VecObj);
for some read file and add into the Vector Class i created.
after that the program will goes void showMenu(Vector<WindLog> &VecObj);
and showing the menu options and User
entered choice, from 1 to 5;
one of this function will executed based on user choice.
void option1(Vector<WindLog> &VecObj);
void option2(Vector<WindLog> &VecObj);
void option3(Vector<WindLog> &VecObj);
void option4(Vector<WindLog> &VecObj);
my question is the parameter of option1 - option4, they has the same parameter and that bother me, i dont know whether if its okay or not.
If any of my question are not clear, please give me some comment so i could fix it. and if you guys want to give me some advice on my code, i would really appreciate it.
Upvotes: 0
Views: 123
Reputation: 780688
If all options will always take the same parameter, you could put them in a vector and index it with the option.
typedef void (*option_fun_t)(Vector<WindLog> &VecObj);
option_fun_t option_fun[] = {option1, option2, option3, option4};
choice = getUserChoice();
option_fun[choice-1](VecObj);
Upvotes: 4
Reputation: 1612
It is perfeclty fine to have the same parameter types in different functions, as long as the functions have different names (which they do). If it really bothers you, you could write one function and pass an additional parameter (like int option
), and then do the processing based on that option, like this:
void option(Vector<WindLog>& VecObj, int option)
{
switch(option)
{
case 1:
{
// Processing in case of option 1
break;
}
case 2:
{
// Processing in case of option 2
break;
}
case 3:
{
// Processing in case of option 3
break;
}
case 4:
{
// Processing in case of option 4
break;
}
default:
break;
}
}
Upvotes: -1