Is this right?

This is a discussion on Is this right? within the C++ Programming forums, part of the General Programming Boards category; I have now completed the coding for the program and its running fine i think lol thank you for your ...

  1. #1
    Registered User [BIO]Asgard's Avatar
    Join Date
    Feb 2004
    Posts
    8

    Is this right?

    I have now completed the coding for the program and its running fine i think lol thank you for your kindness in helping me i am now writing the coding at the side i.e. the //this does that etc.

    Have i got it right OR have i totally got anything wrong on the // comments that the maker of C++ wants to find my house and slap me across the face? input welcome.
    Code:
    //What to wear according to temperature program
    
    #include<iostream.h> //Allows cin and cout to be used
    
    
    int main( void ) //Allows starting of main program 
    
    {
    	int i=0; //This gives the command to allow the program to carry on
    	while (i==0) //This gives the command to reinitialise the program
    	{
    	int temp; //allows input of temperature 
    	cout <<"Please input the temperature\n\n:";
    	cin >>temp;  //User input
    	//We will now prompt output messages based on user temperature input, next lines will test temperature value and prompt an adapted message.
    	if (temp>110) //First checking for temperature not to be higher than 110 (we are assuming that it's the maximum value)
    	{
    	cout <<"The temperature is too high please re-enter\n\n\n"; //If so, 'cout' command will be used to send an error message
    	}
    	else if (temp>=90 && temp<110) //If previous condition is not validated we are cheking if the temperature is contained beetween 110 et 90 value, then prompt adapted message with 'cout'.
    	{
    	cout <<"Spanish Weather : Wear shorts!\n\n"; 
    	i=1;
    	}
    	else if (temp>=70 && temp<90) 
    	{
    	cout <<"Ideal weather : Short sleaves are fine\n\n";
    	i=1;
    	}
    	else if (temp>50 && temp<70)
    	{
    	cout <<"A little chilly : Wear a light jacket\n\n";
    	i=1;
    	}
    	else if (temp>32 && temp<50)
    	{
    	cout <<"English weather : Wear a heavy coat\n\n";
    	i=1;
    	}
    	else if (temp>10 && temp<32)
    	{
    	cout <<"wrap up warm\n\n";
    	i=1;
    	}
    	else if (temp<0 && temp <10)
    	{
    	cout <<"Temperature below freezing stay inside\n\n";
    	i=1;
    	}
    	else if (temp<0) //Finally we test if the temperature is under 0 (we are assuming that it's the minimum value), if so we prompt an error message.
    	{
    	cout <<"The temperature is too low please re-enter temperture\n\n";
    	i=1;
    	}
    	else //If finally all previous test failed, we assume that it's not a valid temperature input, and send adapted message to the user. 
         { 
         cout <<"You haven't entered a valid temperature\n\n"; 
         return 0; //This ends the loop from entering a key stroke
         } 
         } 
         return 0; //Ends the program. 
    }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>//allows input of temperature
    //variable to hold temperature

    >>//If previous condition is not validated we are cheking
    Spelling (checking)

    >>else if (temp<0 && temp <10)
    Should be temp>0

    And the else statement will never be reached.

    Also, please work on your indentation.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User [BIO]Asgard's Avatar
    Join Date
    Feb 2004
    Posts
    8
    Thank you for you reply i have checked the spelling and the indentation i see your canadian and proud of it. Alot of candians gave there lives in france not many know that esp at dunkirk and the last bit of the program is giving me a headache getting it to reckonise minus temps.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    That long else-if statement gave me the idea of making a map container that instead of using a single number as the key you use an interval. Using such an object would shorten that code a lot

    Assuming the intervals may not overlap, it could be implemented as a (balanced) binary tree for decent access time.

    None exists already, right?

    Code:
    interval_map<int, std::string> Comment;
    
    Comment.AddInterval(111, INT_MAX, "The temperature is too high please re-enter\n\n\n");
    Comment.AddInterval(91, 110, "Spanish Weather : Wear shorts!\n\n");
    Comment.AddInterval(71, 90, "Ideal weather : Short sleaves are fine\n\n");
    ...
    
    int temp;
    cout <<"Please input the temperature\n\n:";
    cin >>temp;
    cout << Comment[temp];
    Last edited by Magos; 02-27-2004 at 03:10 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User [BIO]Asgard's Avatar
    Join Date
    Feb 2004
    Posts
    8
    Thanks Guys this is my finished program that i am handing in.

    I will incorperate the colours and remove the .h for the next program.

    Its my First Program!

    Code:
    //What to wear according to temperature program
    
    #include<iostream.h> //Allows cin and cout to be used
    #include<conio.h> //Allows the use of Getch()
    #include<stdlib.h>//allows use of Atio()
    int main( void ) //Allows starting of main program 
    
    {
    	char input[4];//character for input and limits the lenth of the string
    	int i=0; //This gives the command to allow the program to carry on
    	while (i==0) //This gives the command to reinitialise the program
    	{
    	int temp; //variable to hold temperature 
    	cout <<"Please input the temperature\n\n:"; //Asking for input from user
    	cin >>input;//Allows inputing of keystrokes
    temp = atoi(input);//converts input Char to integer nought and places the value into temp
    //We will now prompt output messages based on user temperature input, next lines will test temperature value and prompt an adapted message.
    if (temp>110) //First checking for temperature not to be higher than 110 (we are assuming that it's the maximum value)
    	{
    cout <<"The temperature is too high please re-enter\n\n\n"; //If so, 'cout' command will be used to send an error message
    	}
    else if (temp>=90) //If previous condition is not validated we are checking if the temperature is above 90 value, then prompt adapted message with 'cout'.
    	{
    	cout <<"Spanish Weather : Wear shorts!\n\n"; //Output to user
    	i=1;
    	}
    	else if (temp>=70 && temp<90) 
    	{
    	cout <<"Ideal weather : Short sleaves are fine\n\n"; //Output to user
    	i=1;
    	}
    	else if (temp>=50 && temp<70)
    	{
    	cout <<"A little chilly : Wear a light jacket\n\n"; //Output to user
    	i=1;
    	}
    	else if (temp>=32 && temp<50)
    	{
    	cout <<"English weather : Wear a heavy coat\n\n";  //Output to user
    	i=1;
    	}
    else if (temp>0)//If finally all previous test failed, we assume that it's not a valid temperature input, and send adapted message to the user. 
        	{ 
        	cout <<"Stay inside\n\n"; //Output to user
    	cout.flush(); //Clears buffers
    	getch(); //Holds screen for input
        	return 0; //This ends the loop from entering a key stroke
        	} 
        	} 
    	cout.flush(); //Clears buffer
    	getch(); //Holds screen for input
    	return 0; //Ends the program. 
    }

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    FYI

    The previous post about "That long else-if statement" prompted me to suggest:
    Code:
    if (temp>110) 
    {
        cout <<"The temperature is too high please re-enter\n\n\n"; 
    }
    else if (temp>=90) 
    {
        cout <<"Spanish Weather : Wear shorts!\n\n";
        i=1;
    }
    else if (temp>=70) 
    {
        cout <<"Ideal weather : Short sleaves are fine\n\n";
        i=1;
    }
    else if (temp>=50)
    {
        cout <<"A little chilly : Wear a light jacket\n\n";
        i=1;
    }
    else if (temp>=32)
    {
        cout <<"English weather : Wear a heavy coat\n\n";  //Output to user
        i=1;
    }
    else if (temp>0)
    { 
        cout <<"Stay inside\n\n"; //Output to user
        cout.flush(); //Clears buffers
        getch(); //Holds screen for input
        return 0; //This ends the loop from entering a key stroke
    } 
    else  // what about below zero? Normal for my area
    {
        cout << "it's too cold to live" << endl;
    } 
    cout.flush(); //Clears buffer
    Each previous if limits the top end of temp already, so the compound conditionals are not needed.

    Also, you forgot to check for below 0.

    If your cout's end in endl you won't need one of the \n's nor the cout.flush()
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User [BIO]Asgard's Avatar
    Join Date
    Feb 2004
    Posts
    8
    I did do the below zero but it cuased problems so it had to come out gonna have to study where i went wrong on that had a prob of it reckonising the under 0 and above or something tired form late nights working on it will take it up on monday as a project for my self. with what you suggest on here i can work it into a little program such as this.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21