Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,671 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 3,726 people online right now. Registration is fast and FREE... Join Now!




Rain fall problem

3 Pages V  1 2 3 >  
Reply to this topicStart new topic

Rain fall problem

ginobili
post 21 Nov, 2005 - 01:17 PM
Post #1


New D.I.C Head

*
Joined: 5 Nov, 2005
Posts: 46


My Contributions


Hello everyone, I am having trouble with a switch statement,
I put the switch statement into a function but when i call the function in the middle of a cout statement it shows up front of my statement.

the program also prompts the user to enter what month it is and the program needs to go twelve months back without the need to input the years and prompts us again to input the ActualRain.

This program is a pain in you know what

Any feed back will be greatly appreciated.

Ginobili.

This is the code i got so far:

CODE

// Chapter 10 Arrays
// Problem #1

#include <iostream>
#include <string>
using namespace std;

int getActualRain(int j);

//char January, February;
char print_month(int month)
{
    switch (month)
    {
    case 1:
 cout <<  "January";
 break;
    case 2:
 cout << "February";
    break;
    case 3:
 cout << "March";
 break;
    case 4:
 cout << "April";
 break;
    case 5:
 cout << "May";
 break;
    case 6:
 cout << "June";
 break;
    case 7:
 cout << "July";
 break;
    case 8:
 cout << "August";
 break;
    case 9:
 cout << "September";
 break;
    case 10:
 cout << "October";
 break;
    case 11:
 cout << "November";
 break;
    case 12:
 cout << "December";
 break;
    default:
 cout << "Invalid Value!\a";
 break;
    }
 return 0;
}

int main()

{
    int i;
    float AverageRain[13];
    float ActualRain[13];

    
    for (i = 1; i < 13; i++)
    {
 cout << "Enter average Monthly Rainfall for " << print_month(i) << ": ";
 cin >> AverageRain[i];
    }
    
    int month;    
    cout << "Please enter current month: " << endl;
    cin >> month;
    


    int rm = month -1;
    int j = rm;
    do
    {
 ActualRain[i] = getActualRain(i);
 i--;
    }
    while(i != rm);
    return 0;
}


int getActualRain(int j)
{

    
return 0;
}

if ( month = January )
    rm = 1;
else if(month = 2)
    rm = 2;
else if (month = february )
    rm = 3;
.
.
.

.
.
.
}
User is offlineProfile CardPM

Go to the top of the page


Amadeus
post 21 Nov, 2005 - 06:57 PM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


For you function, try structuring it to return a string, and display that return value, like so:
CODE

#include <iostream>
#include <string>
using namespace std;

int getActualRain(int j);
string print_month(int month);
//char January, February;


int main()

{
int i;
float AverageRain[13];
float ActualRain[13];


for (i = 1; i < 13; i++)
{
cout << "Enter average Monthly Rainfall for " << print_month(i) << ": ";
cin >> AverageRain[i];
}

int month;
cout << "Please enter current month: " << endl;
cin >> month;

getch();
return 0;
}

string print_month(int month)
{
    string result;
switch (month)
{
case 1:
    result = "January";
break;
case 2:
result = "February";
break;
case 3:
result = "March";
break;
case 4:
result = "April";
break;
case 5:
result = "May";
break;
case 6:
result = "June";
break;
case 7:
result = "July";
break;
case 8:
result = "August";
break;
case 9:
result = "September";
break;
case 10:
result = "October";
break;
case 11:
result = "November";
break;
case 12:
result = "December";
break;
default:
cout << "Invalid Value!\a";
break;
}
return result;
}

AS for the second part of your code, you are currently telling the compiler to compare two variables, one named month, the other named January (or Feb, etc...). You are also using the assignment operator, not the equality operator. If you are trying to compare strings, it should look as follows:
CODE

if ( month == "January" )

Please note that I have not cleaned up the code, or optimized, simply pointed out a few areas in which errors are occurring. this is not meant to be copied and pasted, but used as a reference.
User is online!Profile CardPM

Go to the top of the page

ginobili
post 23 Nov, 2005 - 08:18 AM
Post #3


New D.I.C Head

*
Joined: 5 Nov, 2005
Posts: 46


My Contributions


Allright guys,
i am still struggling with this program,
I am at the section where the program prompts u to enter the current month. And i need to feed this into: int AverageRain[i]
My problem is how will i switch a string to a an int can i use the same switch statment or should i create a new one??

Thank you
Ginobili.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 23 Nov, 2005 - 08:31 AM
Post #4


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


If you are taking a month name from the user, and trying to match it to an integer representation of that month, make an array of strings (names of the month). Loop through the array looking for a match. The index of that match plus one will be the integer representation of the month.

I'd actually suggest you need only the array of month names, not the switch statement at all.
User is online!Profile CardPM

Go to the top of the page

ginobili
post 23 Nov, 2005 - 08:33 AM
Post #5


New D.I.C Head

*
Joined: 5 Nov, 2005
Posts: 46


My Contributions


Hello Amadeus,
Would you think an enum statment would work?
Ginobili.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 23 Nov, 2005 - 08:38 AM
Post #6


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


CODE

string month;
int index;
string months[12] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
cout<<"Please enter a month;"<<endl;
cin>>month;
for(int i=0;i<12;i++)
{
  if(month==months[i])
     index=i;
}
index+=1;

index now contains an integer representation of the month...but you don't really need a switch statement...you have the month names in the array.
User is online!Profile CardPM

Go to the top of the page

ginobili
post 23 Nov, 2005 - 08:47 AM
Post #7


New D.I.C Head

*
Joined: 5 Nov, 2005
Posts: 46


My Contributions


Thanks Amadeus
this is my version of it i just can not go back enough to cover all 12 months since PrevMonth run into negative numbers which it does not recognize.

Any idea?

Thank you again

CODE

int cmonth;
    cout << "Please enter current month: " << endl;
    cin >> cmonth;
    cout << print_month(cmonth) << endl;

    cout << "Enter average Monthly Rainfall for: " << endl;

int PrevMonth = cmonth - 1;
do
{
    cout << print_month(lastmonth) << endl;
    cin >> AverageRain[i];

    PrevMonth --;
}

while ( cmonth != lastmonth);

return 0;
}
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 23 Nov, 2005 - 08:50 AM
Post #8


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


is cmonth an int or a string?And what is lastmonth?
User is online!Profile CardPM

Go to the top of the page

ginobili
post 23 Nov, 2005 - 09:00 AM
Post #9


New D.I.C Head

*
Joined: 5 Nov, 2005
Posts: 46


My Contributions


Well cmonth is the current month and i put it in as an integer i will try to figure out a way to switch back to string later though.
And Lastmonth is actualy PrevMonth i renamed it to make the code clearer but i am still running into a problem where i can only go so far back to january....invalid value from that point .
Any suggestions?
Ginobili
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 23 Nov, 2005 - 09:07 AM
Post #10


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


You are encountering a problem because you need to take into account the ends of the list...for the purposes of this discussion, we will consider them to be one and 12.

You need to check the value of the int...if it is 0, set it to 12, if iot is 13, set it to 1.

That's why this would be a little easier with the array I mentioned.
User is online!Profile CardPM

Go to the top of the page

ginobili
post 23 Nov, 2005 - 09:14 AM
Post #11


New D.I.C Head

*
Joined: 5 Nov, 2005
Posts: 46


My Contributions


I see using the array would be easier. I am trying to use it but encouter a problem at:
CODE

if(cmonth=months[j])
    index=j;


i get an error saying:
error C2451: conditional expression of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' is illegal
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.

What can i do?

Thank you.
Ginobili.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 23 Nov, 2005 - 09:45 AM
Post #12


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


You should be using an == sign for the comparison (see my code example above), you have used an assignment operator...secondly, if cmonth is an integer, it will not evaluate correctly.

Or, if you prefer, you can use the .compare() method for strings.
User is online!Profile CardPM

Go to the top of the page

3 Pages V  1 2 3 >
Reply to this topicStart new topic
Time is now: 11/20/08 07:14AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month