Struct and Void functions

Pages: 12
Thank you,
I am having a hang up on your line 12(mine, 24).


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/// Function to print table
void printResults(hurricane hList[], int listSize)
{
    cout <<left << setw(15) << "Month"
         << left << setw(10) << "Name"
         << right << setw(10) << "Year"
         << right << setw(10) << "Wind Speed"
         << left << setw(15) << "Classification";
    cout << endl;

    cout <<left << setw(15) << "====="
         << left << setw(10) << "========"
         << right << setw(10) << "===="
         << right << setw(10) << "=========="
         << left << setw(15) << "==============";
    cout << endl;

    for (int i = 0; i < listSize; i++)
    {
        cout << left << setw(15) << hList[i].month
             << left << setw(10) << hList[i].name
             << right << setw(10) << hList[i].year
             << right << setw(10) << hList[i].windSpeed
             << left << setw(15) << categorize(hList[i].windSpeed, listSize);
        cout << endl;
    }
}


|error: invalid conversion from 'int' to 'hurricane*' [-fpermissive]|
I did get the debugger to accept a char function for categorize, instead of void like I had. No idea if that is right, it is certainly no the most elegant way to write it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/// Function to categorize storms
char categorize(hurricane hList[], int listSize)
{
    for (int i = 0; i < listSize; i++)
    {
        if (hList[i].windSpeed < 74)
            return 'Tropical Storm';
        else if (hList[i].windSpeed > 73 && hList[i].windSpeed < 96)
            return 'Category 1';
        else if (hList[i].windSpeed > 95 && hList[i].windSpeed < 111)
            return 'Category 2';
        else if (hList[i].windSpeed > 110 && hList[i].windSpeed < 130)
            return 'Category 3';
        else if (hList[i].windSpeed > 130 && hList[i].windSpeed < 157)
            return 'Category 4';
        else
            return 'Category 5';
    }
}
Or alternatively if the data extraction is part of the Hurricane structure:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

struct Hurricane
{
    string month;
    string name;
    int year;
    int windSpeed;
    
    string category()
    {
        if (windSpeed < 74)
            return "Tropical Storm";
        else if (windSpeed >= 74 && windSpeed <= 95)
            return "Category 1";
        else if (windSpeed >= 96 && windSpeed <= 110)
            return "Category 2";
        else if (windSpeed >= 111 && windSpeed <= 130)
            return "Category 3";
        else if (windSpeed >= 131 && windSpeed <= 155)
            return "Category 4";
        else
            return "Category 5";
    }
    
    void printResult()
    {
        cout
        << left << setw(15) << month
        << left << setw(10) << name
        << right << setw(10) << year
        << right << setw(10) << windSpeed
        << right << setw(20) << this->category() << endl;
    }
    
    static int extracted(Hurricane *hurricaneList) {
        ifstream myfile ("hurricane.txt");
        
        Hurricane temp;
        int count{0};
        
        if (myfile.is_open())
        {
            while( myfile
                  >> temp.month >> temp.name
                  >> temp.year >>temp.windSpeed
                  )
            {
                hurricaneList[count] = temp;
                temp.printResult();
            }
            
            myfile.close();
        }
        return count;
    }
};

int main()
{
    const int MAX_NO_OF_HURRICANES{100};
    Hurricane hurricaneList[MAX_NO_OF_HURRICANES];
    
    int count = Hurricane::extracted(hurricaneList);
    
    for(int i = 0; i < count; i++)
    hurricaneList[i].printResult();
    
    return 0;
}


(NEEDS HEADING BLAH BLAH)
January        Pali            2016       100          Category 2
February       Ekeka           1992       115          Category 3
March          Hali            1992        50      Tropical Storm
April          Carmen          1980        50      Tropical Storm
May            Amanda          2014       155          Category 4
June           Ava             1973       160          Category 5
July           Gilma           1994       160          Category 5
August         Ioke            2006       160          Category 5
September      Linda           1997       185          Category 5
October        Patricia        2015       215          Category 5
November       Sandra          2015       150          Category 4
December       Omeka           2010        60      Tropical Storm
Program ended with exit code: 0
Look at what you are returning from the categorize function.

It is a 'string' which is made up of multiple characters in simple terms, and not 'char' which is only a single character.
Intially your original post showed the output of your category function was via cout so void was appropriate. The result is printed out inside the function and there is nothing given back. (except for irrelevant here stream condition flags etc)

Now your function returns a string which is better practice because the string can be used in console applications as well as GUI and/or any other interfaces.

C++ is a strongly typed language which means (return in this case) types must be compatible.
And after all that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

struct Hurricane
{
    std::string month;
    std::string name;
    int year;
    int windSpeed;
    
    std::string category()
    {
        if (windSpeed < 74)
            return "Tropical Storm";
        else if (windSpeed >= 74 && windSpeed <= 95)
            return "Category 1";
        else if (windSpeed >= 96 && windSpeed <= 110)
            return "Category 2";
        else if (windSpeed >= 111 && windSpeed <= 130)
            return "Category 3";
        else if (windSpeed >= 131 && windSpeed <= 155)
            return "Category 4";
        else
            return "Category 5";
    }
    
    static void printHeader()
    {
        std::cout
        << "MONTH          NAME            YEAR     SPEED CATEGORY\n"
        << "=============================================================\n";
    }
    
    void printResult()
    {
        std::cout
        << std::left << std::setw(15) << month
        << std::left << std::setw(10) << name
        << std::right << std::setw(10) << year
        << std::right << std::setw(10) << windSpeed << ' '
        << std::left << std::setw(20) << this->category()
        << '\n';
    }
    
    static void printFooter(int cnt)
    {
        std::cout
        << "=============================================================\n"
        << "No. of hurricanes: " << cnt << '\n';
    }
    
    static int extracted(Hurricane h_list[], int)
    {
        std::ifstream myfile ("hurricane.txt");
        
        Hurricane temp;
        int count{0};
        
        if (myfile.is_open())
        {
            while
                ( myfile
                 >> temp.month >> temp.name
                 >> temp.year >>temp.windSpeed
                 )
            {
                h_list[count] = temp;
                //temp.printResult();
                count++;
            }
            
            myfile.close();
        }
        return count;
    }
};

int main()
{
    const int MAX_NO_OF_HURRICANES{100};
    Hurricane hurricaneList[MAX_NO_OF_HURRICANES];
    int count = Hurricane::extracted(hurricaneList, MAX_NO_OF_HURRICANES);
    
    Hurricane::printHeader();
    for(int i = 0; i < count; i++)
    hurricaneList[i].printResult();
    Hurricane::printFooter(count);
    
    return 0;
}





MONTH          NAME            YEAR     SPEED CATEGORY
=============================================================
January        Pali            2016       100 Category 2          
February       Ekeka           1992       115 Category 3          
March          Hali            1992        50 Tropical Storm      
April          Carmen          1980        50 Tropical Storm      
May            Amanda          2014       155 Category 4          
June           Ava             1973       160 Category 5          
July           Gilma           1994       160 Category 5          
August         Ioke            2006       160 Category 5          
September      Linda           1997       185 Category 5          
October        Patricia        2015       215 Category 5          
November       Sandra          2015       150 Category 4          
December       Omeka           2010        60 Tropical Storm      
=============================================================
No. of hurricanes: 12
Program ended with exit code: 0
1
2
3
        if (windSpeed < 74)
            //...
        else if (windSpeed >= 74 && windSpeed <= 95)
the `windSpeed >= 74' is already covered by the `else', so should simply write
1
2
3
        if (windSpeed < 74)
            //...
        else if (windSpeed <= 95)
@ne555 I think you'll find from here the now disappeared @OP is fixed on his/her original cascade which we can say 'works'. That's been proven.

However, not unlike other alternative cascades, working from high speed down to low speed is yet another one, viz:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::string category()
    {
        if(windSpeed > 155)
            return "Category 5";
        else if(windSpeed > 130)
            return "Category 4";
        else if(windSpeed > 110)
            return "Category 3";
        else if(windSpeed > 95)
            return "Category 2";
        else if(windSpeed > 73)
            return "Category 1";
        else
            return "Tropical Storm";
    }
Thank you @againtry
That code works.

Why did you put the functions in the struct?
Last edited on
@Fayezilla

Including the function in the class is to encapsulate as much as possible in the class.
Oops ... in the struct but class/struct same/same
Registered users can post here. Sign in or register to post.
Pages: 12