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]
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]
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)
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".
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.
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.
Users who have thanked const_antine for this post:
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