-2

Thank you all for the help on my previous post! I'm still having problems with converting to string from an integer when pulling the information from a function. I am trying to have the actual name of the month display to the output instead of the number that's associated with it. I'm not sure how to convert to the string successfully. It's working below with the number in it's place, but I just can't get it to display the string to the screen.

for example, I'm required to have the sentence: "The largest amount of rainfall was 12 inches for the month of January"

edit:To sum it up, I cannot get the functions to bring over the string, instead of the integer value. The declared functions at the top of my code are what my professor have given us to use, so I cannot do anything different with how they're declared.

// Declare const variables
const int TOTALMONTHS = 12;

// Declare array for months and rainfall
string months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
double rainFall[TOTALMONTHS];

// Declare functions
double getLowest(double[], int, int&);
double getHighest(double[], int, int&);

int main()
{
    int subscript;
    for (int months = 0; months < TOTALMONTHS; months++)
    {
         // Get this month's rainfall.
         cout << "Enter the rainfall (in inches) for ";
         cout << monthNames[months] << ": ";
         cin >> rainFall[months];
    }

    // Display the largest amount of rainfall.
    cout << "The largest amount of rainfall was ";
    cout << getHighest(rainFall, TOTALMONTHS, subscript)
        << " inches in month ";
    cout << (subscript + 1) << "." << endl;

    // Display the smallest amount of rainfall.
    cout << "The smallest amount of rainfall was ";
    cout << getLowest(rainFall, TOTALMONTHS, subscript)
        << " inches in month ";
    cout << (subscript + 1) << "." << endl << endl;

    // End of program
    system("pause");
    return 0;
}

double getLowest(double rainFall[], int TOTALMONTHS, int &subscript)
{
    double smallest;
    smallest = rainFall[0];

    for (int months = 0; months < TOTALMONTHS; months++)
    {
        if (rainFall[months] < smallest)
        {
            smallest = rainFall[months];
            subscript = months; 
        }
    }
    return smallest;
}

double getHighest(double rainFall[], int TOTALMONTHS, int &subscript)
{
    double largest;
    largest = rainFall[0];

    for (int months = 0; months < TOTALMONTHS; months++)
    {
        if (rainFall[months] > largest) 
        {
            largest = rainFall[months];
            subscript = months; 
        }
    }

    return largest;
}
Denise
  • 11
  • 3
  • Possible duplicate of [Alternative to itoa() for converting integer to string C++?](https://stackoverflow.com/questions/228005/alternative-to-itoa-for-converting-integer-to-string-c) – Joseph D. Apr 23 '18 at 07:38
  • 1
    You're already doing this successfully inside the first loop. Do the same thing again. – molbdnilo Apr 23 '18 at 07:38

2 Answers2

0

Use simple indexing

std::string months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

std::string IntToString(int id)
{
    if ( id < 1 || id > 12 )
    {
        return "BadMonth";
    }
    return months[id-1];
}

int main()
{
    std::cout << IntToString(1) << std::endl;
    std::cout << IntToString(2) << std::endl;
    std::cout << IntToString(3) << std::endl;
    std::cout << IntToString(0) << std::endl;
}

Output is:

January
February
March
BadMonth

In your context I think you should replace

cout << (subscript + 1) << "." << endl << endl;

with

cout << IntToString(subscript + 1) << "." << endl << endl;

after adding my method. Or simply

cout << months[subscript] << "." << endl << endl;

without adding any new code.

Alexey Usachov
  • 1,364
  • 2
  • 8
  • 15
  • In this assignment I must use the functions that I've included in my code. – Denise Apr 23 '18 at 07:40
  • @Denise , Add function `IntToString(int id)` to your code and use it. Or just use indexing `months[id-1]` only id must be `-1` if you want to start month from 1. Or I don't get the question in comment? – Alexey Usachov Apr 23 '18 at 07:44
  • Oh, my apologies. I guess I misunderstood your response. I'll work with your suggestion. Thank you! – Denise Apr 23 '18 at 07:48
  • @Denise , see my edit update at the end of the post. I wrote explanation – Alexey Usachov Apr 23 '18 at 07:49
0

Note that I also added subscript = 0 to the functions; simple as this:

#include <string>
#include <iostream>
#include <limits>

using namespace std;

// Declare const variables
const int TOTALMONTHS = 12;

// Declare array for months and rainfall
string monthNames[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
double rainFall[TOTALMONTHS];

// Declare functions
double getLowest(double[], int, int&);
double getHighest(double[], int, int&);

int main()
{
    int subscript;
    for (int months = 0; months < TOTALMONTHS; months++)
    {
        // Get this month's rainfall.
        cout << "Enter the rainfall (in inches) for ";
        cout << monthNames[months] << ": ";
        cin >> rainFall[months];
    }

    // Display the largest amount of rainfall.
    cout << "The largest amount of rainfall was ";
    cout << getHighest(rainFall, TOTALMONTHS, subscript)
        << " inches in month ";
    cout << monthNames[subscript] << "." << endl;

    // Display the smallest amount of rainfall.
    cout << "The smallest amount of rainfall was ";
    cout << getLowest(rainFall, TOTALMONTHS, subscript)
        << " inches in month ";
    cout << monthNames[subscript] << "." << endl << endl;

    // End of program
    //system("pause");
    return 0;
}

double getLowest(double rainFall[], int TOTALMONTHS, int &subscript)
{
    double smallest;
    smallest = rainFall[0];
    subscript = 0;

    for (int months = 0; months < TOTALMONTHS; months++)
    {
        if (rainFall[months] < smallest)
        {
            smallest = rainFall[months];
            subscript = months; 
        }
    }
    return smallest;
}

double getHighest(double rainFall[], int TOTALMONTHS, int &subscript)
{
    double largest;
    largest = rainFall[0];
    subscript = 0;

    for (int months = 0; months < TOTALMONTHS; months++)
    {
        if (rainFall[months] > largest) 
        {
            largest = rainFall[months];
            subscript = months; 
        }
    }

    return largest;
}
kiloalphaindia
  • 561
  • 3
  • 7