problem with function

I'm trying to convert the numerical value of months to the name using a void function but I cant get it to compile it give me a strange error that is too long to put in the post. I appreciate any help as I've been working on this for a few days. here is a snippet of the area that's giving me a headache.
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;



void monthName(int num);//this is the function protocol for the problem function

int main()
   {

       char response;
       
       int monthCalc;
       
       int convert_month;
       
       int subscribe_info;
       
       int order_date;

       int monthNumber;
       
       int currentYear;
       
       int begin_date;

       int currentMonthNumber;
       
      int currentMonthName;
       
       int expire_month;

       double expire_year;
       
       double buyYear;
       
       double buy_year;
       
       int subsciptMonthName; 
                 
       string firstName;
       
       string lastName;

       cout <<"Enter first letter of the current month: ";

       currentMonthNumber = months();
       
  

       cout <<"Enter current year ";

       currentYear = yearReading(begin_date);

     do
      {
       cout << endl << endl;
       
       cin.ignore();
   
       cout << "Enter subscriber first name: ";

       getline (cin, firstName);
              
       cout << "Enter subscriber last name: ";

       getline (cin, lastName);
       
       cout << endl;

       cout << "Enter first letter of the subscription month: ";

       monthNumber = months();
       
       buy_year = yearReading (buyYear);
       
       subscribe_info = subscriber_month (order_date);

       cout << fixed << showpoint << setprecision(2) << endl;

       cout << endl << endl << endl;
       
       

       cout <<"Current Date: "<<  monthName(currentMonthNumber)<< " " << currentYear <<endl;//this is where I'm having the problem 

       cout <<"Subscriber: " << lastName<<", "<< firstName << endl << endl;
       
       cout <<"Start Date: " << monthName(monthNumber) << " " ;//same here

       cout << static_cast<int>(buyYear) <<endl;

       if(monthNumber == 1)
        {
       
           expire_month = monthNumber + 11;
       
        }
       else
        {
           expire_month = monthNumber - 1;
       
           expire_year = buyYear + subscribe_info;
              
           cout <<"Expiration Date: " << monthName(expire_month)<<" " <<static_cast<int> (expire_year)<< endl;// and here
           
           cout << "Account Status: " << "Current " << endl;
           
           monthCalc =  ((subscribe_info * 12) - expire_month - 12);
           
           cout <<setw(18)<< monthCalc << "months remaining on subsciption " << endl;
        }

          cout << endl << endl << endl;
        
          cout << "Would you like to run the program again (Y/N)? " ;
          
          cin >> response;
          
          response = toupper(response);
    }     
        
       while (response =='Y'); 
            
          cout << endl;
          
          return 0;
  }







void monthName(int number) // this is the function thats giving me problems 
  {
   
   
      switch (number)  
        {
              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;  
         
        }
    
         
     }     
          
  
Last edited on
closed account (Lv0f92yv)
Can you tell us the line to which the error points? I have found that looking at and just before the problematic line (the one to which the error points) can yield useful results hen debugging. Give this a shot, if you haven't already.

Sometimes the error is actually far before the flagged line, but the compiler can't detect it. They're not perfect after all.
I added comments to the code to show where the problem is happening also this is the error I'm getting : 105 ")) << monthName(currentMonthNumber)' note candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] ......
closed account (Lv0f92yv)
I believe one problem is that the function you are using in cout, "monthName(...)", does not return a value (void). You cannot use a function that returns void in a print statement, nor any other statement in which a value is required. Try letting monthName return some value.

Since your current function prints out a value itself, perhaps you can use the call to it as a seperate statement. Assuming the rest of your program is error free, I think this would get the results you're looking for. Alternatively, build a string instead of coutting it in your monthName function, and return it to the caller. This way, you should be able to use it in cout.
Last edited on
Lookup tables are your friends:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

//...

const char * month_names[]={"January","February","March","April","May",
"June","July","August","September","October","November","December"}

//...

int main()
{
     //...
     
     int month_num;
     cout << "enter month (1-12): ";
     cin >> month_num;
     cout << "you picked: " << month_names[month_num-1] << endl;

     //...
}
Topic archived. No new replies allowed.