Newbie help needed!

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 12
Reputation: stan.joe1 is an unknown quantity at this point 
Solved Threads: 0
stan.joe1 stan.joe1 is offline Offline
Newbie Poster

Newbie help needed!

 
0
  #1
Apr 15th, 2008
I wants to input a list of day temperatures and count the number of days with
temperatures above 20 degrees. The C++ program below is supposed to do this and display the result. A temperature of -100 indicates the end of the data.

#include <iostream>
using namespace std;
int main( )
{
float temp;
int nrAbove;
// initialize
nrAbove = 0;
cout << "Temperature of first day (-100 for end of input): ";
cin >> temp;
30
// loop
while (temp > -100)
{
if (temp > 20) // temperature above 20 degrees?
nrAbove++;
} // end of while loop
// results
cout << "Number of days with temperature above 20 degrees C is ";
cout << nrAbove << endl;
return 0;
}


Thanks for taking time to look at this.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,836
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 750
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: Newbie help needed!

 
1
  #2
Apr 15th, 2008
Please tell us what kind of problem you need help with. We can't offer anything more than general advice if you don't specify what's wrong with your code.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,968
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Newbie help needed!

 
0
  #3
Apr 15th, 2008
I guess it will always show 0 or 1, because you can only enter 1 input. If you want to get more inputs, you should put this: cin >> temp; inside your while loop.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 12
Reputation: stan.joe1 is an unknown quantity at this point 
Solved Threads: 0
stan.joe1 stan.joe1 is offline Offline
Newbie Poster

Re: Newbie help needed!

 
0
  #4
Apr 15th, 2008
These are the error’s I am experiencing once I compile.

[Warning] In function `int main()':

expected `;' before "while"
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 177
Reputation: Black Magic is an unknown quantity at this point 
Solved Threads: 4
Black Magic's Avatar
Black Magic Black Magic is offline Offline
Junior Poster

Re: Newbie help needed!

 
0
  #5
Apr 15th, 2008
Maybe because you have "30" in your code?

If you don't need it get rid of it, else put a ; on end of 30
C Plus Plus Coder.
Fourteen Years Of Age
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 12
Reputation: stan.joe1 is an unknown quantity at this point 
Solved Threads: 0
stan.joe1 stan.joe1 is offline Offline
Newbie Poster

Re: Newbie help needed!

 
0
  #6
Apr 15th, 2008
Originally Posted by niek_e View Post
I guess it will always show 0 or 1, because you can only enter 1 input. If you want to get more inputs, you should put this: cin >> temp; inside your while loop.
i added the following code : cin >> temp; inside your while loop.[/QUOTE]
And i am getting these errors once I compile.

[Warning] In function `int main()':

[Warning] In function `int main()':

Thanks for taking the time to look at my problem.

// How many days with temperature above 20 degrees C?
#include <iostream>
using namespace std;
int main( )
{
float temp;
int nrAbove;
// initialize
nrAbove = 0;
cout << "Temperature of first day (-100 for end of input): ";
cin >> temp;
30
// loop
while (temp > -100)
{
if (temp > 20) // temperature above 20 degrees?
cin >> temp;
nrAbove++;
} // end of while loop
// results
cout << "Number of days with temperature above 20 degrees C is ";
cout << nrAbove << endl;
return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,968
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Newbie help needed!

 
0
  #7
Apr 15th, 2008
-remove the ' 30 ' in your code.
-swap lines: cin >> temp; and nrAbove++;
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 12
Reputation: stan.joe1 is an unknown quantity at this point 
Solved Threads: 0
stan.joe1 stan.joe1 is offline Offline
Newbie Poster

Re: Newbie help needed!

 
0
  #8
Apr 15th, 2008
Thanks people.

Narue
niek_e
Black Magic

It’s working well!

Complete code appears below.

// How many days with temperature above 20 degrees C?
#include <iostream>
using namespace std;
int main( )
{
float temp;
int nrAbove;
// initialize
nrAbove = 0;
cout << "Temperature of first day (-100 for end of input): ";
cin >> temp;
30;
// loop
while (temp > -100)
{
if (temp > 20) // temperature above 20 degrees?
cin >> temp;
nrAbove++;
} // end of while loop
// results
cout << "Number of days with temperature above 20 degrees C is ";
cout << nrAbove << endl;
return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 177
Reputation: Black Magic is an unknown quantity at this point 
Solved Threads: 4
Black Magic's Avatar
Black Magic Black Magic is offline Offline
Junior Poster

Re: Newbie help needed!

 
0
  #9
Apr 15th, 2008
Nice to see it working.
So jw, but was it homework or just boredom ?
Last edited by Black Magic; Apr 15th, 2008 at 1:10 pm.
C Plus Plus Coder.
Fourteen Years Of Age
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Newbie help needed!

 
0
  #10
Apr 15th, 2008
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC