Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    15
    Thanks
    8
    Thanked 0 Times in 0 Posts

    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]

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    if (month=1)
    That's not the comparison operator, but the assignment operator. You need to use == instead.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Apr 2012
    Posts
    15
    Thanks
    8
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    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
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    New Coder
    Join Date
    Apr 2012
    Posts
    15
    Thanks
    8
    Thanked 0 Times in 0 Posts
    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".

  • #6
    New to the CF scene
    Join Date
    Apr 2012
    Location
    Las Vegas
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Have you asked about this problem from your Professor?

  • #7
    New Coder
    Join Date
    Apr 2012
    Posts
    15
    Thanks
    8
    Thanked 0 Times in 0 Posts
    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.

  • #8
    New to the CF scene
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post
    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:

    staalkoppie (05-10-2012)

  • #9
    New to the CF scene
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.

  • Users who have thanked ArmLock for this post:

    staalkoppie (05-10-2012)

  • #10
    New Coder
    Join Date
    Apr 2012
    Posts
    15
    Thanks
    8
    Thanked 0 Times in 0 Posts
    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

  • #11
    New Coder
    Join Date
    Apr 2012
    Posts
    15
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Can't "resolve" this page, it doesn't offer the option like my other threads.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •