12 Replies - 927 Views - Last Post: 28 March 2011 - 05:13 AM Rate Topic: -----

#1 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

showing the string month of the array rather than the value

Posted 26 March 2011 - 06:30 PM

The problem I am having is the lowest and highest values are displaying as numbers, and I would like the highest and lowest to display the month name. I tried highest[month] but that does not work
Example
I want it to say The month the rainfall was the highest was: January
#include <iostream>

using namespace std;



int main()
{
   const int NUM_MONTHS = 12;           //number of values
   int values[NUM_MONTHS];              //each value
   int month;                           //loop counter
   int lowest, highest, rainfall, count;
   double average;
   double total = 0;   
   
   //Array with the names of the months
   string months[NUM_MONTHS] = {"January", "February", "March", "April", "May", "June",                         
                         "July", "August", "September", "October", "November", "December"};
   //Input rainfall for each month
   for(int month = 1; month <= NUM_MONTHS; month++)
   {
       cout << "Enter the total rainfall for " << months[month-1] <<  ": ";
       cin >> rainfall;   
   
   //Input validation -  enter a value over or equal to 0
   if ( rainfall < 0 )
   {
             cout << "The number you have entered is invalid." << endl;
             cout << "Please reenter: ";
             cin >> rainfall;
   }
    //Calculate total rainfall for the year
             total += rainfall;
}
   
   //Calculate the average monthly rainfall
   average = total/12;
   
   //Find the months with the highest rainfall and lowest rainfall
  highest = values[0];
  lowest = values[0];

        for(int count = 1; count < NUM_MONTHS; count++)
        {
            if(values[count] > highest)
            highest = values[count];
            if(values[count] < lowest)
            lowest = values[count];
        }
   //Display total rainfall for year, monthly average, highest and lowest months
   cout << "\n\nThe total rainfall for the year is: " << total << " inches"
        << "\nThe average monthly rainfall is: " << average << " inches";
   cout << "\nThe month with the highest rainfall is: " << highest
        << "\nThe month with the lowest rainfall is: " << lowest << endl;
   
          system("pause");
          return 0;
          }


This post has been edited by tricket_7: 26 March 2011 - 06:52 PM


Is This A Good Question/Topic? 0
  • +

Replies To: showing the string month of the array rather than the value

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: showing the string month of the array rather than the value

Posted 26 March 2011 - 06:41 PM

and your question is? you would think after 87 posts that you might figure out to ask a question when posting but apparently that's too much to ask.
Was This Post Helpful? 0
  • +
  • -

#3 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: showing the string month of the array rather than the value

Posted 26 March 2011 - 06:43 PM

View Postishkabible, on 26 March 2011 - 07:41 PM, said:

and your question is? you would think after 87 posts that you might figure out to ask a question when posting but apparently that's too much to ask.


Really? No need to be nasty, just a simple mistake.
Was This Post Helpful? 0
  • +
  • -

#4 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: showing the string month of the array rather than the value

Posted 26 March 2011 - 06:46 PM

sorry, i wasn't trying offend you, i just get frustrated with people who post like that. it would be nice if you could edit in your question in your first post.

edit: thank you :)

This post has been edited by ishkabible: 26 March 2011 - 06:48 PM

Was This Post Helpful? 1
  • +
  • -

#5 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: showing the string month of the array rather than the value

Posted 26 March 2011 - 06:47 PM

View Postishkabible, on 26 March 2011 - 07:46 PM, said:

sorry, i wasn't trying offend you, i just get frustrated with people who post like that. it would be nice if you could edit in your question in your first post.

That is ok, I was actually editing when you posted because I realized that I did not post what I was struggling with.
Was This Post Helpful? 0
  • +
  • -

#6 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: showing the string month of the array rather than the value

Posted 26 March 2011 - 06:54 PM

why are you always starting your array indexing at one, and then when you use the code you use the index - 1?
This looks messy, and it is standard to start array indexing at 0 in your for loops.
Also, I believe that you are intending to put the value of rainfall into values[month] during your first for loop.
Was This Post Helpful? 0
  • +
  • -

#7 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: showing the string month of the array rather than the value

Posted 26 March 2011 - 06:55 PM

a way to do this would be to add 2 more variables that store the month number then use a switch case to print the string according to the number.

int HighMonth=0,LowMonth=0;
if(values[count] > highest) {
   highest = values[count];
   HighMonth = count;
}
if(values[count] < lowest) {
   lowest = values[count];
   LowMonth = count;
}



void PrintMonth(int M) {
   switch(M) {
   case 0:
      cout<<"Jan";
   case 1:
   case 2:
   case 3:
   case 4:
   case 5:
   case 6:
   case 7:
   case 8:
   case 9:
   case 10:
   case 11:
      cout<<"Dec";
   }
}



i didn't fill in all the months but you get the idea.
Was This Post Helpful? 0
  • +
  • -

#8 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: showing the string month of the array rather than the value

Posted 26 March 2011 - 06:56 PM

@ishkabible
you would also need break statements :)
Was This Post Helpful? 0
  • +
  • -

#9 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: showing the string month of the array rather than the value

Posted 26 March 2011 - 08:01 PM

just set up an int high and low to save the index location of the high and low months then call

cout << month[high];
cout << month[low];



to display the month's name
Was This Post Helpful? 1
  • +
  • -

#10 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: showing the string month of the array rather than the value

Posted 27 March 2011 - 04:27 PM

Still trying to work on the Highest/ Lowest display, I did make the changes on the for statement month = 0 and took away months[month-1] to months[month], but now when I run the program is runs an infinite loop or something, there is an error. I am still not completely understanding how to get the month name to display rather than the acutal lowest and highest value. It will also need to display average and total which was working.

#include <iostream>

using namespace std;



int main()
{
    const int NUM_MONTHS = 12;           //number of values
   int values[NUM_MONTHS];              //each value
   int month;                           //loop counter
   int lowest, highest, rainfall, count;
   double average;
   double total = 0;   
   
   //Array with the names of the months
   string months[NUM_MONTHS] = {"January", "February", "March", "April", "May", "June",                         
                         "July", "August", "September", "October", "November", "December"};
   //Input rainfall for each month
   for(int month = 0; month <= NUM_MONTHS; month++)
   {
       cout << "Enter the total rainfall for " << months[month] <<  ": ";
       cin >> rainfall;   
   
   //Input validation -  enter a value over or equal to 0
   if ( rainfall < 0 )
   {
             cout << "The number you have entered is invalid." << endl;
             cout << "Please reenter: ";
             cin >> rainfall;
   }
    //Calculate total rainfall for the year
             total += rainfall;
}
   
   //Calculate the average monthly rainfall
   average = total/12;
   
   //Find the months with the highest rainfall and lowest rainfall
  
   //Display total rainfall for year, monthly average, highest and lowest months
   cout << "\n\nThe total rainfall for the year is: " << total << " inches"
        << "\nThe average monthly rainfall is: " << average << " inches";
   
   
          system("pause");
          return 0;
          }


Was This Post Helpful? 0
  • +
  • -

#11 JaKWaC  Icon User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 234
  • Joined: 15-November 10

Re: showing the string month of the array rather than the value

Posted 27 March 2011 - 05:06 PM

Check out luckless post above. You have an array of the name of the month, each month has a unique index value for the array. Instead of storing the highest and lowest values for the month you need to store the array index value.
Was This Post Helpful? 0
  • +
  • -

#12 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: showing the string month of the array rather than the value

Posted 28 March 2011 - 03:57 AM

View PostJaKWaC, on 27 March 2011 - 06:06 PM, said:

Check out luckless post above. You have an array of the name of the month, each month has a unique index value for the array. Instead of storing the highest and lowest values for the month you need to store the array index value.

I think I understand now. So for example.....I could use the if/else or the switch statement
if(month[0]== highest)
{
 highest = "January";
}
else if(month[1]== highest)
{
highest = "February";
} 


and so on..... or
switch(months[]) {
   case 0:
      cout<<"January";
      break;
   case 1:
   case 2:
}


etc.....

This post has been edited by tricket_7: 28 March 2011 - 04:39 AM

Was This Post Helpful? 0
  • +
  • -

#13 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: showing the string month of the array rather than the value

Posted 28 March 2011 - 05:13 AM

almost, but there is no need to have a ton of if statements because months[] already has the names of the months in it. So, if you store the index of the highest month, just call

System.out.println("Highest month: " + months[indexOfHighest]); //gets the months name for you


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1