scientific notation?

can someone please help me understand why totaldivsales, average, and at the bottom, total yearly corporate sales, show up in scientific notation instead of values?

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
//Include Files
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <fstream>
#include <string>
using namespace std;

struct comDiv
{
	string divName;
	float sales[4];
	float totalDivSales;
	float average;
};

int main()
{
fstream quarterSales;
quarterSales.open ("quartersales.txt", ios::out);

float max = 0;
float min = 0;

int flag_high = 0, flag_low = 0 ;
int flag_high_2 = 0, flag_low_2 = 0;
int tmp = 0;


comDiv division[4];


for (int i=0; i < 4; i++)
{
cout << "enter name: ";
cin >> division[i].divName;
for ( int j = 0; j < 4; j++)
{
cout << "enter sales for quarter " << j+1 << " : ";
cin >> division[i].sales[j] ;
division[i].totalDivSales += division[i].sales[j];
if ( i == 0 && j == 0 )
{
tmp = division[0].sales[0];
max = tmp;
min = tmp;
}
if ( division[i].sales[j] > max)
{
flag_high = i;
flag_high_2 = j;
max = division[i].sales[j];
}
else if ( division[i].sales[j] < min)
{
flag_low = i;
flag_low_2 = j;
min = division[i].sales[j];
}
}
division[i].average = division[i].totalDivSales/4.0;
}

cout << "------------------------------------------------------" << endl;

cout << "Division\t Sales\t\t\t Div. Total\t Div Avg\t" << endl;
cout << "\t\tQtr 1\t Qtr 2\t Qtr3\t Qtr4\t" << endl;
for (int i=0; i < 4; i++)
{
cout << division[i].divName << "\t\t";
for (int j=0; j < 4; j++)
cout << division[i].sales[j] << "\t";

cout << division[i].totalDivSales << "\t";
cout << division[i].average << endl;
}

quarterSales.close();
cout << "------------------------------------------------------" << endl;

quarterSales.open("quartersales.txt", ios::in);

cout << "Total Yearly Corporate Sales: " << division[4].totalDivSales << endl;

cout << "Highest Quarter: " << endl;
cout << "Division: " << division[flag_high].divName << "\tQuarter: " << flag_high_2 << "\t Sales: " << division[flag_high].sales[flag_high_2]<< endl;
cout << "Lowest Quarter: " << endl;
cout << "Division: " << division[flag_low].divName << "\tQuarter: " << flag_low_2 << "\t Sales: " << division[flag_low].sales[flag_low_2];

quarterSales.close();
getch();
return 0;
}
You have to set fixed notation manually:
1
2
cout.setf(ios::fixed); // set fixed notation
cout.precision(2); // set decimal precision (if you want) 


http://www.cplusplus.com/reference/iostream/ios_base/precision/
http://www.cplusplus.com/reference/iostream/ios_base/setf/
http://www.cplusplus.com/reference/iostream/manipulators/fixed/

Hope this helps.
Last edited on
ok, if i add that, then my numbers for those are large and negative. wtf?
Initialize comDiv::totalDivSales and comDiv::average to zero. Otherwise their initial values are undefined

1
2
3
4
5
6
7
 
// put this on line 32
   for(int i=0;i<4;i++)
      {
          division[i].totalDivSales=0;
          division[i].average=0;
      }

CL2 Declare each variable with the smallest possible scope and initialize it at the same time.


This quote is found in CERN's C++ Coding standard which can be easily googled. I find it a very useful guide to keep my code readable and less prone to common errors while I learn.

Different organizations have different standards which mostly vary according to style. CERN's was the first one I found and it's quite readable and short, which is why I try to use it.
ok but now yearly corporate sales still isn't working.
On line 83 array index is out of bounds (array indexes start at 0) so it should be
cout << "Total Yearly Corporate Sales: " << division[3].totalDivSales << endl; but I think that's not what you want - you wan to display sum of all comDiv::totalDivSales, right?
Topic archived. No new replies allowed.