Thread: I have a homework question?

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Exclamation Theres an error using my driver program?

    Hello;
    Thanks Cshot and everyone else for your help. This is my problem, i have to input 70 degree, relativr humidity between 15 and 35% and 10 MPH. If I input all of this it should return on the screen as true otherwise any other answer reutrns a false. I must be close because I have no errors shown on my display but its still not right.

    Code:
    #include <iostream>
    #include <string>		
    using namespace std;
    
    double Okay_to_spray(double);
    
    int main()
    {
    	double temp, rel_humidity, wind_spd;
    	char spray;
    
    	cout << "Input the temperature, relative humidity and the wind speed of our plane: \n";
    		
    	cin >> temp >> rel_humidity >> wind_spd;
    
    	cout << "The plane's temperature is " << temp << " its relative humidity " 
    		 << rel_humidity << " and its wind speed is " << wind_spd << ".\n";
    	
    	cout << spray << "\n";
    		 return 0;
    }	 
    
    Okay_to_spray()
    {
    
    	double temp, rel_humidity, wind_spd, spray;
    
    	{
    		if (temp == 70)
    			if (rel_humidity >= 15 && rel_humidity <= 35)
    				if (wind_spd == 10)
    					spray = true;
    				else
    					spray = false;
    	}
    	return spray;
    
    }
    Last edited by correlcj; 10-03-2002 at 06:41 PM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ... and where abouts do you think its going wrong? Remember, debugging is an important part of the programming phases.

    Try reading through the flow of your code again...

    And please use a meaningful thread title, remember that everyone (99%) starting a thread here is doing so because they have a question.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    Re: Theres an error using my driver program?

    Okay, you're declaring two instances of your variables: double temp, rel_humidity, wind_spd; are declared and local to the functions main() and okay_to_spray(). Assigning values to these variables in main() is not going to affect the variables in okay_to_spray()

    You have three options:
    Make the variables double temp, rel_humidity, wind_spd; global, and remove their declarations from your functions eg:

    Code:
    #include <iostream>
    #include <string>		
    using namespace std;
    
    bool Okay_to_spray();
    double temp, rel_humidity, wind_spd;
    
    int main()
    {
    
    	cout << "Input the temperature, relative humidity and the wind speed of our plane: \n";
    		
    	cin >> temp >> rel_humidity >> wind_spd;
    
    	cout << "The plane's temperature is " << temp << " its relative humidity " 
    		 << rel_humidity << " and its wind speed is " << wind_spd << ".\n";
    	
    	cout << (Okay_to_spray()) ? "true" : "false" << "\n";
    		 return 0;
    }	 
    
    bool Okay_to_spray()
    {
    
    	{
    		if (temp == 70)
    			if (rel_humidity >= 15 && rel_humidity <= 35)
    				if (wind_spd == 10)
    					return true;
    				else
    					return false;
    	}
    }
    2. You can pass them to your function okay_to_spray()
    Code:
    #include <iostream>
    #include <string>		
    using namespace std;
    
    bool Okay_to_spray(double, double, double);
    
    
    int main()
    {
    	double temp, rel_humidity, wind_spd;
    
    	cout << "Input the temperature, relative humidity and the wind speed of our plane: \n";
    		
    	cin >> temp >> rel_humidity >> wind_spd;
    
    	cout << "The plane's temperature is " << temp << " its relative humidity " 
    		 << rel_humidity << " and its wind speed is " << wind_spd << ".\n";
    	
    	cout << (Okay_to_spray(temp, rel_humidity, wind_spd)) ? "true" : "false"  << "\n";
    	return 0;
    }	 
    
    bool Okay_to_spray(double temperature, double humid, double wind)
    {
    
    	{
    		if (temperature == 70)
    			if (humid >= 15 && humid <= 35)
    				if (wind == 10)
    					return true;
    				else
    					return false;
    	}
    }
    3. You can create a "spray" structure.
    Code:
    #include <iostream>
    #include <string>		
    using namespace std;
    
    struct spraystr
    {
    	double temperature;
    	double rel_humidity;
    	double wind_spd;
    };
    
    bool Okay_to_spray(spraystr);
    
    int main()
    {	
    
    	spraystr spray;
    
    	cout << "Input the temperature, relative humidity and the wind speed of our plane: \n";
    		
    	cin >> spray.temperature >> spray.rel_humidity >> spray.wind_spd;
    
    	cout << "The plane's temperature is " << spray.temperature << " its relative humidity " 
    		 << spray.rel_humidity << " and its wind speed is " << spray.wind_spd << ".\n";
    	
    	cout << (Okay_to_spray(spray)) ? "true" : "false" << "\n";
    	return 0;
    }	 
    
    bool Okay_to_spray(spraystr sprayStructure)
    {
    
    	{
    		if (sprayStructure.temperature == 70)
    			if (sprayStructure.rel_humidity >= 15 && humid <= 35)
    				if (sprayStructure.wind_spd == 10)
    					return true;
    				else
    					return false;
    	}
    }
    You also had other syntatical errors in your code which I fixed, like not calling your function, incorrect return type, not stating the return type, and I don't know what you were doing with the char variable "spray" etc. Personally I'd go with option 2 or 3.
    Last edited by Eibro; 10-03-2002 at 07:30 PM.

  4. #4
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    He pretty much covered everything without going into pointers. You'll learn that down the line.

    However I'm not sure you got the spray=false condition right. Initializing it to false would probably be a better option.
    Code:
       spray = false;
       if (temp == 70)
          if (rel_humidity >= 15 && rel_humidity <= 35)
             if (wind_spd == 10)
                spray = true;
    The way you previously had it, if your temperature was correct, then the else condition would never be executed. So if the other parameters are out of range, then you'd never set spray to false.
    Last edited by Cshot; 10-03-2002 at 08:49 PM.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Mmmm good point, that's why I always use brackets... no matter how small the if statement.

    Code:
       spray = false;
       if (temp == 70)
          if (rel_humidity >= 15 && rel_humidity <= 35)
             if (wind_spd == 10)
                spray = true;

    Code:
    if (temp == 70)
    {
    	if (rel_humidity >= 15 && rel_humidity <= 35)
    	{
    		if (wind_spd == 10)
    		{
    			return true;
    		}
    	}
    }
    return false;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question About My Homework Pls Help Me İmmediately
    By jennyyyy in forum C Programming
    Replies: 27
    Last Post: 03-13-2008, 11:40 AM
  2. quick question
    By surfingbum18 in forum C Programming
    Replies: 5
    Last Post: 12-04-2007, 06:16 AM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM