6 Replies - 377 Views - Last Post: 28 May 2009 - 06:55 AM Rate Topic: -----

#1 suzi1811  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 06-February 09

print temp at intervals of 20

Post icon  Posted 28 May 2009 - 04:11 AM

Hi,
I'm just learning c++ and have to do a for loop with an if statement to print out the temperature at intervals of 20 and I can't do it. This is my very poor attempt
#include <iostream>
using namespace std;
int main(){
	//declare variables
	int temp = 0;
	//for loop, with if-else statement
	for (int a = 0; a >= temp; a++)
	{
		if (temp < 200)
		{
		cout<< "temperature is " <<temp;
		}//end if
		temp+20;
	}//end for
	return 0;
}


Any help would be greatly appreciated
Suzi

Is This A Good Question/Topic? 0
  • +

Replies To: print temp at intervals of 20

#2 AndyH1963  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 201
  • Joined: 22-June 08

Re: print temp at intervals of 20

Posted 28 May 2009 - 05:30 AM

What do you need the if statement for?

for (int temp = 0; temp < 200; temp+=20)
{
   cout << "temperature is " << temp;
}


Was This Post Helpful? 1
  • +
  • -

#3 suzi1811  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 06-February 09

Re: print temp at intervals of 20

Posted 28 May 2009 - 05:46 AM

View PostAndyH1963, on 28 May, 2009 - 04:30 AM, said:

What do you need the if statement for?

for (int temp = 0; temp < 200; temp+=20)
{
   cout << "temperature is " << temp;
}


The exercise I've been given says I have to use a for with if-else.
Suzi
Was This Post Helpful? 0
  • +
  • -

#4 AndyH1963  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 201
  • Joined: 22-June 08

Re: print temp at intervals of 20

Posted 28 May 2009 - 05:49 AM

Does the question state why use of if and else are required?

Suggest that you post the question as well.
Was This Post Helpful? 0
  • +
  • -

#5 suzi1811  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 06-February 09

Re: print temp at intervals of 20

Posted 28 May 2009 - 05:52 AM

Hey
That really helped I got it working!
#include <iostream>
using namespace std;
int main(){
	//declare variables
	int temp;
	
	//for loop, with if-else statement
	for (temp = 0; temp <=200; temp+=20)
	{	 		 
		if (temp != 200)
		{
			cout << "temperature is " << temp;
		}//end if
		cout<<"\n";		
	}//end for
	return 0;
}


Prints out on each line with temp at intervals of 20 I'd been doing the increment bit wrong. :rolleyes:
Thanks for helping me out
Suzi :)
Was This Post Helpful? 0
  • +
  • -

#6 AndyH1963  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 201
  • Joined: 22-June 08

Re: print temp at intervals of 20

Posted 28 May 2009 - 05:56 AM

Two points:

(1) you don't need to declare the variable temp outside of the for loop. Unless you are going to need it latter.

(2) I still don't see any need for the if condition.

#include <iostream>
using namespace std;
int main()
{
	for (int temp = 0; temp <=200; temp+=20)
	{
		 cout << "temperature is " << temp << endl;
	}

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#7 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: print temp at intervals of 20

Posted 28 May 2009 - 06:55 AM

hope this helps :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1