Odd error.

I think it has something to do with my function to be honest fellow programmers I'm at a total lost. Here's my main followed by my header file.

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
95
96
97
//Course: 4002-208
//Author : Jared Stroud
//Date   : 10/30/12
//Project: 5
// Purpose: analyze average monthly temperature data for Rochester from 1940 to 2011.


#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include "functions.h"
#include <fstream>

using namespace std;
// function prototypes
int mainMenu(string prompt);



int main ()
{

    // variable declarations :
    int option;
    int menuoption;
    ifstream in_stream;   //create a file object
    in_stream.open("RocTempDataV1.txt");


 //check if file has been opened
    if( ! in_stream.is_open( ) )
    {   //file opened
        cout << "Unable to open file - does not exit in current directory" << endl;
        exit(0);
    }


   mainMenu(menuoption);
    // switch statement for menu options.
       switch (menuoption)
    {
    // choice for menuoption 1
        case 1:
        if (menuoption == 1)
        {
            cout << "option1" << endl;
        }
        break;

    // choice for option 2
        case 2:
        if (menuoption == 2)
        {

        }
        break;

        case 3:

    // choice for option 3
        if (menuoption == 3)
        {

        }
        break;

        default:
        cout << " Error Invalid number this is the switch default";


    }












    // error checking for valid input
     if (option <=0 || option >=4)
    {
        while (option <= 0|| option >=4)
        {
            cout << "Error invalid input\n" << endl;
            cout << "Please enter another option : " << endl;
            cin  >> option;
        }
    }
}





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
using namespace std;
//------------------------
//Name: mainMenu
//Purpose: lists main menu options
//Parameters: menuoption.
//Returns:  integer for menu selection.
//------------------------
int mainMenu(string prompt)
{
    int num;

    cout <<setw(12) << "Options" << setw(32) << " Description" <<  endl;
    cout <<setw(9)<< "1" << setw(52) << " Calculate yearly information" << endl;
    cout <<setw(9)<< "2" << setw(56) << " Calculate historical information" << endl;
    cout <<setw(9)<< "3" << setw(40) << " Exit the program" << endl;

    cin >> num;
    return (num);
}
And the error?
mainMenu is asking for a string and returning an int. You are passing an int to this function.

Try passing by reference:

1
2
3
4
5
6
7
8
9
10
int mainMenu(int& prompt)
{
    cout <<setw(12) << "Options" << setw(32) << " Description" <<  endl;
    cout <<setw(9)<< "1" << setw(52) << " Calculate yearly information" << endl;
    cout <<setw(9)<< "2" << setw(56) << " Calculate historical information" << endl;
    cout <<setw(9)<< "3" << setw(40) << " Exit the program" << endl;

    cin >> prompt;
    return (prompt);
}



you also don't need to check to see what menu choice was chosen in the switch statement, that's what the switch statement is for:

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
mainMenu(menuoption);
    // switch statement for menu options.
       switch (menuoption)
    {
    // choice for menuoption 1
        case 1:
       // if (menuoption == 1)     don't need this
      //  {
       //     cout << "option1" << endl;
       // }
        break;

    // choice for option 2
        case 2:                          //   thats what this does
      //  if (menuoption == 2)
      //  {

       // }
        break;

        case 3:

    // choice for option 3
      //  if (menuoption == 3)
      //  {

      //  }
        break;

        default:
        cout << " Error Invalid number this is the switch default";


    }
Last edited on
Topic archived. No new replies allowed.