Reusable Function
Apr 19, 2014 at 8:55pm UTC
Hi Friends!
I have completed my project just can not find a way to reduce my code, especially i could not made a function seperately to open file and reuse the code. And particularly I am repeating my code to open the text file because I do not know how to make a function for it and call it repeatedly.
myFile("fileName" , ios::in);
Is it possible to make a separate function and call it from different places?
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
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int menu(int );
int myChoice(char , int );
int main(){
int loopCondition = 1;
cout << "\n\nTemperature Statistics\n----------------------\n\nReading logged values for processing and presentation...\n\nPress Enter for menu: " ;
cin.get();
menu(loopCondition);
return 0;
}
int menu(int loopCondition){
char choice;
while (loopCondition){
cout<<"\n\nMENU\n----\n\n1. Display temperature values\n2. View maximum and minimum temperatures\n3. View average temperature\n4. Quit\n\nMake your choice: " ;
cin.get(choice);
cin.get();
loopCondition = myChoice(choice, loopCondition);
cout<<"\n\nPress Enter to continue:" ;
cin.get();
};
return 0;
}
int myChoice(char choice, int loopCondition){
double fileToken;
int i;
if (choice=='1' ){
cout<<"\nDisplaying the latest 24 temperature values:\n\n" ;
ifstream myFile("templog.txt" ,ios::in);
for (i=0;i<24;i++){
if (i%6==0)
cout<<endl;
myFile>>fileToken;
cout<<fixed<<setprecision(2)<<setw(8)<<fileToken;
}
}
else if (choice=='2' ){
cout<<"\nCalculating the maximum and minimum temperature...\n" ;
double max=0, min=0;
ifstream myFile("templog.txt" ,ios::in);
myFile>>fileToken;
max=min=fileToken;
for (int i=1;i<24;i++){
myFile>>fileToken;
if (fileToken>max)
max=fileToken;
if (fileToken<min)
min=fileToken;
}
cout<<"\nMaximum temperature: " <<fixed<<setprecision(2)<<max<<" degrees Celcius\n" ;
cout<<"\nMinimum temperature: " <<min<<" degrees Celcius\n" ;
}
else if (choice=='3' ){
cout<<"\nCalculating average temperature...\n" ;
double total=0.0, average=0.0;
ifstream myFile("templog.txt" );
for (i=0;i<24;i++){
myFile>>fileToken;
total+=fileToken;
}
average=total/24;
cout<<"\nAverage temperature: " ;
cout<<fixed<<setprecision(2)<<average<<" degrees Celcius\n" ;
}
else if (choice=='4' ){
loopCondition = 0;
cout << "\n\nTerminating the program." ;
return (0);
}
}
Thank you very much
Sincere Regards,
Last edited on Apr 19, 2014 at 9:46pm UTC
Apr 20, 2014 at 1:15am UTC
in myChoice
, you need to add a return
at the end.
You also need to close the file streams when you're done with them. That could become a serious problem if you don't correct it.
You can eliminate about half of what you wrote for myChoice
by writing a function that just gets the last X temps and returning them. The calculations can be done by the caller.
Apr 20, 2014 at 2:33pm UTC
Hi !
I have fixed all the error and requirement but it is not properly handling file opening and reading...
I will appreciate if someone helps me out
here is 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
#include <iostream>
#include <fstream>
#include <iomanip>
//using namespace std; // full namespace is not used any more
int menu();
int myChoice(char );
char inputValidating();
int main(){
std::cout << "\n\nTemperature Statistics\n----------------------\n\nReading logged values for processing and presentation...\n\nPress Enter for menu: " ;
std::cin.get();
menu();
return 0;
}
int menu(){
char choice;
int loopCondition = 1;
while (loopCondition){
system("cls" );
std::cout<<"\n\nMENU\n----\n\n1. Display temperature values\n2. View maximum and minimum temperatures\n3. View average temperature\n4. Quit\n\nMake your choice: " ;
choice = inputValidating(); // validating the user input
std::cin.get();
loopCondition = myChoice(choice);
std::cout<<"\n\nPress Enter to continue:" ;
std::cin.get();
};
return 0;
}
//Error Handling for user interaction
char inputValidating(){
char choice;
bool valid = false ;
while (!valid){
valid = true ;
std::cin>> choice;
if (std::cin.fail()){
std::cin.clear();
std::cin.ignore();
std::cout<<"incorrect input" ;
valid = false ;
}
}
return choice;
}
int myChoice(char choice){
double fileToken;
std::string fileName = "templog.txt" ; // Use of Constant
std::ifstream myFile(fileName.c_str(),std::ios::in); //text file is only opened once
int i, loopCondition=1;
if (choice=='1' ){
std::cout<<"\nDisplaying the latest 24 temperature values:\n\n" ;
if (myFile.is_open()){ // Error handling for file opening and reading
for (i=0;i<24;i++){
if (i%6==0)
std::cout<<std::endl;
myFile>>fileToken;
std::cout<<std::fixed<<std::setprecision(2)<<std::setw(8)<<fileToken;
}
}else {
std::cout<<"Error occured while opening file" ;
return 0;
}
}
else if (choice=='2' ){
std::cout<<"\nCalculating the maximum and minimum temperature...\n" ;
double max=0, min=0;
myFile>>fileToken;
max=min=fileToken;
for (i=1;i<24;i++){
myFile>>fileToken;
if (fileToken>max)
max=fileToken;
if (fileToken<min)
min=fileToken;
}
std::cout<<"\nMaximum temperature: " <<std::fixed<<std::setprecision(2)<<max<<" degrees Celcius\n" ;
std::cout<<"\nMinimum temperature: " <<min<<" degrees Celcius\n" ;
}
else if (choice=='3' ){
double total=0.0, average=0.0;
std::cout<<"\nCalculating average temperature...\n" ;
for (i=0;i<24;i++){
myFile>>fileToken;
total+=fileToken;
}
average=total/24;
std::cout<<"\nAverage temperature: " ;
std::cout<<std::fixed<<std::setprecision(2)<<average<<" degrees Celcius\n" ;
}
else if (choice == '4' ){
std::cout << "\n\nTerminating the program." ;
myFile.close(); //Filed Closed on the Exit
return loopCondition = 0;
}
else {
std::cout << "\n\nYou have entered a wrong choice, Try again." ;
}
myFile.close(); // File Closed after reading
return loopCondition = 1;
}
Topic archived. No new replies allowed.