Works in Dev C++ Compiler but not Microsoft Visual Studio

This compiles just fine in DEV C++ but in Microsoft Visual Studio it tells me that "finally is uninitialized", but it is initialized. So I'm not sure what to do to fix it so it will work in Visual Studio. Could anyone help me out?

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
#include <iostream>
#include <iomanip>
#include "math.h"
#define WAGE 8

using namespace std;
float final;

float decimalhour (int hours, float minutes)
{
      float decimalmin, total;
      decimalmin = minutes/60;
      
      total = hours + decimalmin;
      
      return (total);
}

float dayhours (float firsttotal, float secondtotal)
{

      float result;
      if (firsttotal >= 5)  
      {      
             firsttotal = 12 - firsttotal;
             result = firsttotal + secondtotal;
      }
      else
      {
             secondtotal = 12 - secondtotal;
             firsttotal = 12 - firsttotal;
             result = firsttotal - secondtotal;
      }
      return (result);        
}

float totalhours (float final)
{
     float finally;
     finally += final;
     cout << "They worked ";
     cout << finally << " hours!" << endl << endl;
     return (finally);
}

float grosspay (float hours)
{
      cout << hours << endl;
      float gross = hours * WAGE;
      return (gross);
}

void input ()
{ 
    int hourstart, hourstop, minutestart, minutestop;
    float firsttotal, secondtotal;
    int i=1;
    
    while (i <= 7)
{
    cout << "How many hours did they work on day " << i << " ?" << endl << endl;
    cout << "Clock in: ";
    cin >> hourstart >> minutestart;

    cout << "Clock out: ";
    cin >> hourstop >> minutestop;
    
    cout << endl << "They worked ";
    cout << hourstart << ":" << minutestart;
    cout << " - ";
    cout << hourstop << ":" << minutestop << endl << endl;   

    firsttotal = decimalhour (hourstart, minutestart);
    secondtotal = decimalhour (hourstop, minutestop);
    
    final += dayhours (firsttotal, secondtotal);
    
    cout << fixed << showpoint << setprecision(2);
    cout << "They worked ";
    cout << dayhours (firsttotal, secondtotal) << " hours that day." << endl << endl;
    cout << "________________________________________________________________" << endl << endl;
    i++;
}
        float grosshours=totalhours (final);
    
        cout << "They made $" << grosspay (grosshours);
	cout << " this week." << endl << endl;
}
 
Just looked quickly but finally dosn't look initialized to me.
39
40
float finally; //<--finally is declared but not initialized
finally += final; //<--finally is used but not initialized 
Last edited on
- Well, there you go.
WTF, it works fine as is in Dev C++, but doesn't work in Visual Studio until I initialize it to freaking zero!?!?!?! Dumb.

Anyways, thank you very much. Good to know it was just a simple fix.
Don't blame the compiler. You should as a matter of routine make sure that all variables are initialised before you try to make use of their existing value.
closed account (3qX21hU5)
Actually you compiler is doing you a big favor not being "dumb".

Also I have a feeling you are using Bloodshed Dev C++ which comes with a compiler that hasn't been updated for what 8 years now? So that is probably why the compiler doesn't complain because it is almost a decade old and is no where near standard compliant ;p
Dev C++ example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

    using namespace std;

float totalhours (float final)
{
     float finally;
     finally += final;
     cout << "They worked ";
     cout << finally << " hours!" << endl << endl;
     return (finally);
}

int main()
{
    totalhours(5);

    return 0;
}


Output:
They worked 7.92252e+033 hours!


Note, if using Dev C++, make sure it is the Orwell version:
http://orwelldevcpp.blogspot.co.uk/
Last edited on
closed account (3qX21hU5)
Was that bloodshed Dev or Orwell Chervil?
@Zereo Orwell.

Note, the compiler issues a warning. Depending on the configuration, the program may still execute with warnings present, though it is good practice to correct the code so there are no warnings.
...\Untitled8.cpp	In function 'float totalhours(float)':
9	22	...\Untitled8.cpp	
[Warning] 'finally' is used uninitialized in this function [-Wuninitialized]

Last edited on
Topic archived. No new replies allowed.