6 Replies - 338 Views - Last Post: 27 April 2010 - 01:13 PM Rate Topic: -----

#1 eraserhead87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 19-April 10

Trivial if statement problem

Posted 26 April 2010 - 12:06 PM

Hi, when I run the function below, the program exits if 0 is entered as the last input. Can anyone point me in the direction to overcome this problem? My attempt is shown, bottom, but I can't get it to work. If the user enters '0', I want to return to if statement. I have tried changing the scope (as that seems to have been 50% of my errors throughout this project), but have only run into more errors.

Thanks in advance guys.



				 
/* create and initialise total outside scope of  
 for loop so it is not reset every interation */

int totalP = 0; 
   
//Perform loop iteration for b number of appliances
for (int b=0; b <arraySize; b++) { 
    //Ask user if they would like to use each appliance
    cout << "\n Would you like to use " << a[b].getName() <<"? Please enter 1 for yes or 0 for no. " << endl;
	
    bool ans;			//Declare bool variable for answer to question
    cin >> ans;			//User input answer

    if (ans==1){
	//Ask user how many times they use appliance during a weekday
        cout << "\n How many times would you like to use " << a[b].getName() << " during a weekday?" << endl;
					
	int n;			//Declare variable to store number of uses
	cin >> n;		//User inputs number of uses


	//int calc1 = average power used by appliance * duration used * number of weekday uses
	int calc1 = a[b].getAveragePower() * a[b].getDuration() * n;

	//Ask user how many times they use appliance during a weekend day
	cout << "\n How many times would you like to use " << a[b].getName() << " during a weekend day?" << endl;
					
	int m;			//Declare variable to store number of uses
	cin >> m;		//User inputs number of uses

	//int calc1 = average power used by appliance * duration used * number of weekend day uses
	int calc2 = a[b].getAveragePower() * a[b].getDuration() * m;
					
					
	//Calculate total power use in Watts for weekend use 
	totalP =  totalP + calc1 + calc2;  // new total = old total plus new value for calc1 + calc2 
			
			

	//Print 
	cout << "\n Total energy use for weekend days is " << calc1 << endl; 
	cout << "\n Total energy use for weekdays is " << calc2 << endl;

    }
			
}
 
return totalP; // Taken out of scope of for loop, so loop runs more than once 

} 
} 
}

This post has been edited by JackOfAllTrades: 26 April 2010 - 12:51 PM
Reason for edit:: Fixed horrific indentation (sort of)


Is This A Good Question/Topic? 0
  • +

Replies To: Trivial if statement problem

#2 walleyegirl  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 11
  • Joined: 26-April 10

Re: Trivial if statement problem

Posted 26 April 2010 - 02:08 PM

I think it will be beneficial to see the other objects/methods in this project...thx
Was This Post Helpful? 1
  • +
  • -

#3 joesyuh  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 174
  • Joined: 30-September 08

Re: Trivial if statement problem

Posted 26 April 2010 - 02:26 PM

Like walleye said it would be nice to see some more and to understand what you are saying. You could run the function in some sort of loop where it runs until the appropriate outcome is reached. Or you could have an else statement in that function that more appropriately handles the '0'.

Your question is a little vague and so is your code. Hopefully my answer is some help:)
Was This Post Helpful? 1
  • +
  • -

#4 eraserhead87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 19-April 10

Re: Trivial if statement problem

Posted 27 April 2010 - 02:15 AM

Sorry guys, thought I had included enough. Full program below:-

//Header file for appliances

#include <iostream>					//includes the funtions for input/output stream
#include <string>					//Includes the functions for working with strings
#include <fstream>					//Includes the functions for working with files

using namespace std;

class appliances {					//Creates class called "appliances" to model each appliance{
private:
	string name;					//Name of appliance
	int averagePower;				//Average power used in KW
	int duration;					//How long appliance is switched on (in whole minutes)
	int timeUsed;					//Earliest time of use (0000 to 2359)
	int averageWeekday;				//Average number of weekday uses
	int averageWeekend;				//Average number of weekend uses
public:

	// Getters & Setters
	string getName() { return name;};
	void setName(string n) {name = n;};

	int getAveragePower() { return averagePower; };
	void setAveragePower(int p) {averagePower = p; };
	
	int getDuration() { return duration; };
	void setDuration(int d) { duration = d; };

	int getTimeUsed() { return timeUsed; };
	void setTimeUsed(int t) { timeUsed = t; };

	int getAverageWeekday() { return averageWeekday; };
	void setAverageWeekday(int a) { averageWeekday = a; };

	int getAverageWeekend() { return averageWeekend; };
	void setAverageWeekend(int a) { averageWeekend = a; };


};


//Header File for Properties


#include <iostream>					//Includes the functions for input/output
#include <string>					//Includes the functions for working with strings
#include <fstream>					//Includes the functions for working with files

using namespace std;

class properties {					//Creates class called "properties" to model property
private:
	string propertyType;            //Name of appliance
	int numberOfRooms;              //Number of rooms
	int averageLighting;			//Average lighting power kW
	int averageElectricHeating;		//Average electric heating used

public:

	// Getters & Setters
	string getPropertyType() { return propertyType;};
	void setPropertyType(string n) {propertyType = n;};

	int getNumberOfRooms() { return numberOfRooms; };
	void setNumberOfRooms(int r) {numberOfRooms = r; };

	int getAverageLighting() { return averageLighting; };
	void setAverageLighting(int l) {averageLighting = l; };

	int getAverageElectricHeating() { return averageElectricHeating; };
	void setAverageElectricHeating(int h) {averageElectricHeating = h; };
	

};


//INSTRUCTIONS TO USER HERE


#include"properties.h"					//Include properties header file
#include"appliances.h"					//Include appliances header file

double prop();							//Forward declaration for function prop() 
int wkdayCalc();						//Forward declaration for function wkdayCalc()
int wkendCalc();						//Forward declaration for function wkendCalc()
int wkdayAppliances();					//Forward declaration for function wkdayAppliances()
int wkendAppliances();					//Forward declaration for function wkendAppliances()
int option();							//Forward declaration for funtion option()

int main()
{

	//double x = return value from function prop()  ( prop() = propPower)
	double x = prop();
	
	//Ask user to select user specified model or standard model
	cout << "\n \n Would you like to run a user specified model? Please enter 1 for yes or 0 for no." << endl;
	bool isTrue;
	cin >> isTrue;
	

	
	if (isTrue==1){
		//If user selects user specified model declare int w = return value from option()
		int w = option();

		//Print value calculated in option() to console
		cout << "\n Total energy calculated from selected input file = " << w << endl;

		//Print total energy use for property and user specified file
		cout << "\n Total energy used = " << x+w << "\n" << endl;


	}else{
		if(isTrue==0){
	

	//Shows user list of appliances in file
	cout << "\n \n The list of weekday appliances loaded from file  " << endl;
	
	//Call wkdayAppliances()
	wkdayAppliances();

	//Set int y = return value from function wkday calc()
	int y = wkdayCalc();


	//Shows user list of appliances from file
	cout << "\n \n The list of weekend appliances loaded from file " << endl;
	wkendAppliances();

	//Set int z = return value from function wkendCalc()
	int z = wkendCalc();


	//double total = sum of power calculated from each function
	double total = x + y + z;

	//Prit total to console
	cout << "Total energy use for specified number of weekdays and weekends is " << total << endl;



	system("pause");


		}
	}
	return 0;
}


#include "Appliances.h"				//Include Appliances header file and headers included within Appliances.h

	
//User specified function
int option()
{ 
 
                //Ask user to enter input file
        string myfilename, temp; 
        cout << "\n Enter the file to be opened " << endl; 
        cin >> myfilename; 
 
 
                //Open user specified appliances file (with error check) 
        ifstream inputfile; 
        inputfile.open(myfilename.c_str()); 
        if(!inputfile) 
        { 
				//If open file is unsuccessful return error message
                cout << "Error opening input file" << endl; 
                return -1; 
                 cout << temp << endl;

        } else { 

				//Ask user to enter number of appliances to be modelled
                cout << "\n How many appliances would you like to model?" << endl; 

                int arraySize;			//Number of appliances to be modelled determines size of array
                cin >> arraySize;		//User enters number of appliances

				//Declare and initialize dynamic array to store data from appliances file
                appliances *a = new appliances[arraySize]; 
                string temp; 
                appliances app; 

				//Store values from file in array (size determined by user)
                for(int i = 0; i<arraySize; i++){ 

						//Each appliance has 6 elements of data
                        for(int j = 0; j<6; j++){ 
                                getline(inputfile, temp, ','); 
                                if(j==0){ 
                                        app.setName(temp); 
                                }else if(j==1){ 
                                        int averagePower = atoi(temp.c_str()); 
                                        app.setAveragePower(averagePower); 
 
                                }else if(j==2){ 
                                int duration = atoi(temp.c_str()); 
                                app.setDuration(duration); 
 
                                }else if(j==3){ 
                                int timeUsed = atoi(temp.c_str()); 
                                app.setTimeUsed(timeUsed); 
 
                                }else if(j==4){ 
                                int averageWeekday = atoi(temp.c_str()); 
                                app.setAverageWeekday(averageWeekday); 
 
                                }else if(j==5){ 
                                int averageWeekend = atoi(temp.c_str()); 
                                app.setAverageWeekend(averageWeekend); 
                                } 
 
                                 
                        } 
                        a[i] = app; 
                } 



				 /* create and initialise total outside scope of  
					 for loop so it is not reset every interation */

			 int totalP = 0; 
   
			 //Perform loop iteration for b number of appliances
			for (int b=0; b <arraySize; b++) { 

				//Ask user if they would like to use each appliance
				cout << "\n Would you like to use " << a[b].getName() <<"? Please enter 1 for yes or 0 for no. " << endl;
				
				bool ans;			//Declare bool variable for answer to question
				cin >> ans;			//User input answer


				
				if (ans==1){


					//Ask user how many times they use appliance during a weekday
					cout << "\n How many times would you like to use " << a[b].getName() << " during a weekday?" << endl;
					
					int n;			//Declare variable to store number of uses
					cin >> n;		//User inputs number of uses




					//int calc1 = average power used by appliance * duration used * number of weekday uses
					int calc1 = a[b].getAveragePower() * a[b].getDuration() * n;

					

					//Ask user how many times they use appliance during a weekend day
					cout << "\n How many times would you like to use " << a[b].getName() << " during a weekend day?" << endl;
					
					int m;			//Declare variable to store number of uses
					cin >> m;		//User inputs number of uses

					//int calc1 = average power used by appliance * duration used * number of weekend day uses
					int calc2 = a[b].getAveragePower() * a[b].getDuration() * m;
					
					
				 //Calculate total power use in Watts for weekend use 
				 totalP =  totalP + calc1 + calc2;  // new total = old total plus new value for calc1 + calc2 
			
			

				//Print 
				cout << "\n Total energy use for weekend days is " << calc1 << endl; 
				cout << "\n Total energy use for weekdays is " << calc2 << endl;

			

			
				}
			
 
				system ("pause");
		return totalP; // Taken out of scope of for loop, so loop runs more than once 
 

			}
		}
}


//Returns power use for property type

#include "properties.h"

double prop(){

		// Read input file & store each value in an array
	string myfilename, temp;
	cout << "Enter the file to be opened " << endl;
	cin >> myfilename;


		//Open user specified property file (with error check)
	ifstream inputfile;
	inputfile.open(myfilename.c_str());
	if(!inputfile)
	{
		cout << "Error opening input file" << endl;
		return -1;
		
		cout << temp << endl;
	}

		{

			//Stores values from file in array

		properties p[1];
		string temp;
		properties prop;

		for(int i = 0; i<1; i++){
			for(int j = 0; j<4; j++){
				getline(inputfile, temp, ',');
				if(j==0){
					prop.setPropertyType(temp);
				}else if(j==1){
					int numberOfRooms = atoi(temp.c_str());
					prop.setNumberOfRooms(numberOfRooms);

				}else if(j==2){
				int averageLighting = atoi(temp.c_str());
				prop.setAverageLighting(averageLighting);

				}else if(j==3){
				int averageElectricHeating = atoi(temp.c_str());
				prop.setAverageElectricHeating(averageElectricHeating);

				}
				}

				
			
			p[i] = prop;
		}


	   	//Prints property information to console
	cout << "\n The property type you have chosen to model is a " << p[0].getPropertyType() << "." << endl;
	cout << "\n The property has " << p[0].getNumberOfRooms() << " rooms. " << endl;

		//Perform calculations specific to property type
	cout << "During what season is the simulation occuring? Spring/Summer/Autumn/Winter. " << endl;
	string season;
	cin >> season;
	double summ = 0.5;
	double wint = 1.5;
	double spri = 0.8;
	double autu = 1;

	if (season == "summer"){
	double propPower = p[0].getNumberOfRooms() * p[0].getAverageLighting() * p[0].getAverageElectricHeating() * summ;
	cout << "\n Power used by property is property during summer is " << propPower << endl;
	}else{
		if (season == "winter"){
			double propPower = p[0].getNumberOfRooms() * p[0].getAverageLighting() * p[0].getAverageElectricHeating() 
					* wint;
			cout << "\n Power used by property during winter is " << propPower << endl;
		}else{
			if (season == "spring"){
				double propPower = p[0].getNumberOfRooms() * p[0].getAverageLighting() * p[0].getAverageElectricHeating() 
						* spri;
				cout << "\n Power used by property during spring is " << propPower << endl;
			}else{
				if (season == "autumn"){
					double propPower = p[0].getNumberOfRooms() * p[0].getAverageLighting() * p[0].getAverageElectricHeating()
							* autu;
					cout << "\n Power used by property during autumn is " << propPower << endl;
	
				
				
	return propPower;
				}
			}
		}
	}
		}
}


//Prints a list of appliances to console

#include "Appliances.h"


int wkdayAppliances()
{
		

		// Read input file & store each value in an array

	ifstream inputfile("\\\\zeus\\wyb08171$\\Desktop\\canydaeit.csv");
	if(! inputfile)
	{
		cout << "Error opening input file" << endl;
		return -1;
	} else {


		appliances a[25];
		string temp;
		appliances app;

		// Generate list of weekday appliances used 

		for(int i = 0; i<25; i++){
			for(int j = 0; j<6; j++){
				getline(inputfile, temp, ',');
				if(j==0){
					app.setName(temp);
				}
				
			}
			a[i] = app;
			}

	

		//Print list of appliances being modelled to console

		for(int c = 0; c<25; c++){
		cout << a[c].getName();
	}
		return 0;
	
	}
}


//Performs calculations for weekday appliances

#include "Appliances.h"

int wkdayCalc()
{
		

		// Read input file & store each value in an array

	ifstream inputfile("\\\\zeus\\wyb08171$\\Desktop\\canydaeit.csv");
	if(! inputfile)
	{
		cout << "Error opening input file" << endl;
		return -1;
	}else{
 
      
		appliances a[25];
		string temp;
		appliances app;

		for(int i = 0; i<25; i++){
			for(int j = 0; j<6; j++){
				getline(inputfile, temp, ',');
				if(j==0){
					app.setName(temp);
				}else if(j==1){
					int averagePower = atoi(temp.c_str());
					app.setAveragePower(averagePower);

				}else if(j==2){
				int duration = atoi(temp.c_str());
				app.setDuration(duration);

				}else if(j==3){
				int timeUsed = atoi(temp.c_str());
				app.setTimeUsed(timeUsed);

				}else if(j==4){
				int averageWeekday = atoi(temp.c_str());
				app.setAverageWeekday(averageWeekday);

				}else if(j==5){
				int averageWeekend = atoi(temp.c_str());
				app.setAverageWeekend(averageWeekend);
				}

				
			}
			a[i] = app;
		}





		// Determine number of weekdays

		cout << " \n \n How many weekdays do you wish to model?" << endl;
		int noOfWd;
		cin >> noOfWd;
		cout << "The number of weekdays you chose to model was " << noOfWd << endl;

		cout << "\n The power used by each appliance in file was " << endl;





		//Calculations for energy use in kWh during weekdays

		
		int app1wd = a[0].getAveragePower() * a[0].getDuration() * a[0].getAverageWeekday();
		cout << app1wd << endl;

		int app2wd = a[1].getAveragePower() * a[1].getDuration() * a[1].getAverageWeekday();
		cout << app2wd << endl;

		int app3wd = a[2].getAveragePower() * a[2].getDuration() * a[2].getAverageWeekday();
		cout << app3wd << endl;

		int app4wd = a[3].getAveragePower() * a[3].getDuration() * a[3].getAverageWeekday();
		cout << app4wd << endl;

		int app5wd = a[4].getAveragePower() * a[4].getDuration() * a[4].getAverageWeekday();
		cout << app5wd << endl;

		int app6wd = a[5].getAveragePower() * a[5].getDuration() * a[5].getAverageWeekday();
		cout << app6wd << endl;

		int app7wd = a[6].getAveragePower() * a[6].getDuration() * a[6].getAverageWeekday();
		cout << app7wd << endl;

		int app8wd = a[7].getAveragePower() * a[7].getDuration() * a[7].getAverageWeekday();
		cout << app8wd << endl;

		int app9wd = a[8].getAveragePower() * a[8].getDuration() * a[8].getAverageWeekday();
		cout << app9wd << endl;

		int app10wd = a[9].getAveragePower() * a[9].getDuration() * a[9].getAverageWeekday();
		cout << app10wd << endl;

		int app11wd = a[10].getAveragePower() * a[10].getDuration() * a[10].getAverageWeekday();
		cout << app11wd << endl;

		int app12wd = a[11].getAveragePower() * a[11].getDuration() * a[11].getAverageWeekday();
		cout << app12wd << endl;

		int app13wd = a[12].getAveragePower() * a[12].getDuration() * a[12].getAverageWeekday();
		cout << app13wd << endl;


		//Calculate & print total energy use in kWh for weekdays

		int totalwdAveragePower = app1wd + app2wd + app3wd + app4wd + app5wd + app6wd + app7wd + 
					app8wd + app9wd + app10wd + app11wd + app12wd + app13wd;

		int totalwd = noOfWd * totalwdAveragePower;
		cout << "\n Total energy use for " << noOfWd << " weekdays is " << totalwd << endl;

		return totalwd;


		}
		}


//Prints list of appliances to console

#include "Appliances.h"


int wkendAppliances()
{
		

		// Read input file & store each value in an array

	ifstream inputfile("\\\\zeus\\wyb08171$\\Desktop\\canydaeit.csv");
	if(! inputfile)
	{
		cout << "Error opening input file" << endl;
		return -1;
	} else {

		appliances a[25];
		string temp;
		appliances app;

		for(int i = 0; i<25; i++){
			for(int j = 0; j<6; j++){
				getline(inputfile, temp, ',');
				if(j==0){
					app.setName(temp);
				}
			
			}
			a[i] = app;
			}

	

		//List of appliances being modelled:-

		for(int c = 0; c<25; c++){
		cout << a[c].getName();
	}
		return 0;

	}
}


//Performs calculations for weekend appliances

#include "Appliances.h"	

int wkendCalc()
{

ifstream inputfile("\\\\zeus\\wyb08171$\\Desktop\\canydaeit.csv");
	if(! inputfile)
	{
		cout << "Error opening input file" << endl;
		return -1;
	} else {

		appliances a[25];
		string temp;
		appliances app;

		for(int i = 0; i<25; i++){
			for(int j = 0; j<6; j++){
				getline(inputfile, temp, ',');
				if(j==0){
					app.setName(temp);
				}else if(j==1){
					int averagePower = atoi(temp.c_str());
					app.setAveragePower(averagePower);

				}else if(j==2){
				int duration = atoi(temp.c_str());
				app.setDuration(duration);

				}else if(j==3){
				int timeUsed = atoi(temp.c_str());
				app.setTimeUsed(timeUsed);

				}else if(j==4){
				int averageWeekday = atoi(temp.c_str());
				app.setAverageWeekday(averageWeekday);

				}else if(j==5){
				int averageWeekend = atoi(temp.c_str());
				app.setAverageWeekend(averageWeekend);
				}

				
			}
			a[i] = app;
		}

		// Determine number of weekends

		cout << " \n \n How many weekends do you wish to model?" << endl;
		int noOfWe;
		cin >> noOfWe;
		cout << "The number of weekends you chose to model was " << noOfWe << endl;

		cout << "\n The power used by each appliance in file was " << endl;



		//Calculations for energy use in kWh for weekends


		int app1we = a[0].getAveragePower() * a[0].getDuration() * a[0].getAverageWeekend();
		cout << app1we << endl;

		int app2we = a[1].getAveragePower() * a[1].getDuration() * a[1].getAverageWeekend();
		cout << app2we << endl;

		int app3we = a[2].getAveragePower() * a[2].getDuration() * a[2].getAverageWeekend();
		cout << app3we << endl;

		int app4we = a[3].getAveragePower() * a[3].getDuration() * a[3].getAverageWeekend();
		cout << app4we << endl;

		int app5we = a[4].getAveragePower() * a[4].getDuration() * a[4].getAverageWeekend();
		cout << app5we << endl;

		int app6we = a[5].getAveragePower() * a[5].getDuration() * a[5].getAverageWeekend();
		cout << app6we << endl;

		int app7we = a[6].getAveragePower() * a[6].getDuration() * a[6].getAverageWeekend();
		cout << app7we << endl;


		int app8we = a[7].getAveragePower() * a[7].getDuration() * a[7].getAverageWeekend();
		cout << app8we << endl;

		int app9we = a[8].getAveragePower() * a[8].getDuration() * a[8].getAverageWeekend();
		cout << app9we << endl;

		int app10we = a[9].getAveragePower() * a[9].getDuration() * a[9].getAverageWeekend();
		cout << app10we << endl;


		int app11we = a[10].getAveragePower() * a[10].getDuration() * a[10].getAverageWeekend();
		cout << app11we << endl;


		int app12we = a[11].getAveragePower() * a[11].getDuration() * a[11].getAverageWeekend();
		cout << app12we << endl;


		int app13we = a[12].getAveragePower() * a[12].getDuration() * a[12].getAverageWeekend();
		cout << app13we << endl;


		//Calculate & print total energy use in kWh for weekend use
		int totalwe = app1we + app2we + app3we + app4we + app5we + app6we + app7we + 
					app8we + app9we + app10we + app11we + app12we + app13we;

		cout << "\n Total energy use for " << noOfWe << "weekend days is " << totalwe * noOfWe<< endl;


		return totalwe;

	}
}

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5667
  • View blog
  • Posts: 22,511
  • Joined: 23-August 08

Re: Trivial if statement problem

Posted 27 April 2010 - 05:12 AM

Something for you to investigate. Is the comment on this line actually true?
return totalP; // Taken out of scope of for loop, so loop runs more than once 

Was This Post Helpful? 1
  • +
  • -

#6 walleyegirl  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 11
  • Joined: 26-April 10

Re: Trivial if statement problem

Posted 27 April 2010 - 12:52 PM

Something I'd say in any case is that you're going to want to step into your code, and evaluate the values in your variables as you go. Check all variables that touch your problem, not just the obvious few.

A lot of beginning programmers think they should be able to work through logical bugs by evaluating the statements outside of a compiled run - I used to do that, too. Once you break free from the mindset, though, you'll find that your learning will grow rapidly, because the computer invariably evaluates your instructions differently than you intended. Your programming environment becomes your best instructor. :)

It becomes increasingly important to step into code while working in the C family languages, because manipulating data types and other information-handling constructs in this language set is a more involved undertaking. By a glance at your code, I think you have it in you to do very well. Stepping into and through compiled work will free you to advance rapidly in your abilities. - (a former programming instructor)
Was This Post Helpful? 1
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5667
  • View blog
  • Posts: 22,511
  • Joined: 23-August 08

Re: Trivial if statement problem

Posted 27 April 2010 - 01:13 PM

Welcome to D.I.C., walleyegirl! An excellent post; you are obviously a fine addition to our community!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1