Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-24-2012, 02:22 PM   PM User | #1
staalkoppie
New Coder

 
Join Date: Apr 2012
Posts: 15
Thanks: 8
Thanked 0 Times in 0 Posts
staalkoppie is an unknown quantity at this point
Lightbulb C++ noob needs advise with if / else and loop statements

Hi guys, I'm learning C++ and I'm trying to write a rainmeter, which is working fine, however, I'd like to add a function that allocated a month name, instead of a month number to the output of the loop. However, it only shows January every loop. Please assist with the correct layout to get such funtionality working. this is my code so far:

[CODE]

#include <iostream>
#include <string>

using namespace std;
int main( )
{
float totalRain = 0.0;
float rainfall;
int month = 1;
string noMonth;

if (month=1)
{
noMonth="January";
}
else if (month=2)
{
noMonth="February";
} //will continue to add more months once I get the functionality working
else
cout<<"Invalid selection"<<endl;

while (month <=12)
{
cout<<"please enter rainfall for month "<<noMonth;
cin>>rainfall;
totalRain+=rainfall;
month++;
}
cout<<"the total rainfall is "<<totalRain<<"mm."<<endl;
return 0;
}
[CODE]
staalkoppie is offline   Reply With Quote
Old 04-24-2012, 02:39 PM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
if (month=1)
That's not the comparison operator, but the assignment operator. You need to use == instead.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 04-24-2012, 02:45 PM   PM User | #3
staalkoppie
New Coder

 
Join Date: Apr 2012
Posts: 15
Thanks: 8
Thanked 0 Times in 0 Posts
staalkoppie is an unknown quantity at this point
Ok thanks...corrected it, however, it doesn't change over from "January">
[CODE]
#include <iostream>
#include <string>

using namespace std;
int main( )
{
float totalRain = 0.0;
float rainfall;
int month = 1;
string noMonth;

if (month==1)
{
noMonth="January :";
}
else if (month==2)
{
noMonth="February :";
} //will continue to add more months once I get the functionality working
else
cout<<"Invalid selection"<<endl;

while (month <=12)
{
cout<<"please enter rainfall for month "<<noMonth;
cin>>rainfall;
totalRain+=rainfall;
month++;
}
cout<<"the total rainfall is "<<totalRain<<"mm."<<endl;
return 0;
}
[CODE]

Did I implement your suggestion correctly?
staalkoppie is offline   Reply With Quote
Old 04-24-2012, 02:57 PM   PM User | #4
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
however, it doesn't change over from "January">
It's because you have
Code:
int month = 1;
at the top and it's value never get changed. I don't get your ultimate aim, though you might need to move that if-else sections into the loop and add a cin to read the choice of the user for the value of month
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 04-24-2012, 03:24 PM   PM User | #5
staalkoppie
New Coder

 
Join Date: Apr 2012
Posts: 15
Thanks: 8
Thanked 0 Times in 0 Posts
staalkoppie is an unknown quantity at this point
The idea is to get a counter based loop, so I can input 12 measurements of rainfall (one for each month of the year).
"please enter rainfall for month "<<month , and as long I leave var "month" in the cout statement, the command output will read:"please enter rainfall for month 1"
The second loop reads:"please enter rainfall for month 2",as the counter ++ in value for every loop.
My aim is getting the program to replace the "1" (for month 1) into a word (January) and the second output, the "2" into February.
So in other words, I want to allocate a new month-name for every loop, instead of the cout statement display "month 1" or "month 2".
staalkoppie is offline   Reply With Quote
Old 04-27-2012, 04:12 PM   PM User | #6
HunterBran
New to the CF scene

 
Join Date: Apr 2012
Location: Las Vegas
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
HunterBran is an unknown quantity at this point
Have you asked about this problem from your Professor?
HunterBran is offline   Reply With Quote
Old 05-07-2012, 01:37 PM   PM User | #7
staalkoppie
New Coder

 
Join Date: Apr 2012
Posts: 15
Thanks: 8
Thanked 0 Times in 0 Posts
staalkoppie is an unknown quantity at this point
Unfortunately, this specific module is correspondence, and the book they supplied is a terrible excuse for a textbook i m o. any way, thanks to the guys on here, I am slowly making progress.
staalkoppie is offline   Reply With Quote
Old 05-07-2012, 02:40 PM   PM User | #8
const_antine
New to the CF scene

 
Join Date: May 2012
Posts: 3
Thanks: 0
Thanked 1 Time in 1 Post
const_antine is an unknown quantity at this point
Put the code that reads month and sets noMonth into a separate function and call it in the while() loop you have. If you have not yet covered functions - move the entire code snippet in that loop. That's the short answer.
const_antine is offline   Reply With Quote
Users who have thanked const_antine for this post:
staalkoppie (05-10-2012)
Old 05-08-2012, 10:56 AM   PM User | #9
ArmLock
New to the CF scene

 
Join Date: May 2012
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
ArmLock is an unknown quantity at this point
Hi, why don't you set up two arrays? One will hold all the months and one can hold them amount of rainfall in that month.

something like:

string month[12] = {blah......}
string rainAmount[12] = {0,0,0.....};

for (int i = 0; i < 12; i++)
{
cout << "Showing month: " << month[i] << endl;
cout << " Please enter amount of rain: ";
cin >> rainAmount[i];
}

This is just of the top of my head and untested but should work well enough.
ArmLock is offline   Reply With Quote
Users who have thanked ArmLock for this post:
staalkoppie (05-10-2012)
Old 05-10-2012, 09:56 AM   PM User | #10
staalkoppie
New Coder

 
Join Date: Apr 2012
Posts: 15
Thanks: 8
Thanked 0 Times in 0 Posts
staalkoppie is an unknown quantity at this point
Cool, thanks for the advice! You guys are the best. turns out that I tried to make it to complicated, and was not suppose to be able to do what I tried to do, and the more complicated ideas is still to be discussed in later stages of the textbook. but yeah, the Arrays idea is what we eventually end up doing. Oh well,no it turns out I'm ahead of the rest of the guys studying this module
staalkoppie is offline   Reply With Quote
Old 05-10-2012, 09:58 AM   PM User | #11
staalkoppie
New Coder

 
Join Date: Apr 2012
Posts: 15
Thanks: 8
Thanked 0 Times in 0 Posts
staalkoppie is an unknown quantity at this point
Can't "resolve" this page, it doesn't offer the option like my other threads.
staalkoppie is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:29 PM.


Advertisement
Log in to turn off these ads.