Doubles lose their decimal places!?

Hey can someone help me figure this problem out?

The point of this program is to create 10 random ints and doubles, and chars.
It then sorts sorts them from least to greatest. Everything is fine for ints and chars, but not for doubles. When I initialize them, its perfect, (ex: 204.953,645.365) but when I sort them, most of them lose their decimal places.


Here is the program.
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
  #include "C:\Users\Child\OneDrive\Documents\ToolBox.h"

const int n=10;

int main(){
    ofstream myOut;
    myOut.open("C://Users/Child/Desktop/Output.txt");
    int ints[n];
    double dub[n];
    char cha[n];
    srand(time(NULL));
    for(int c=0;c<n;c++){
        ints[c]=rand()%999;
    }
    for(int c=0;c<n;c++){
        dub[c]=(rand()%999)/1000.;
        dub[c]=dub[c]+rand()%999;

    }
    for(int c=0;c<n;c++){
        cha[c]=rand()%254;
    }
    isort(ints,n);
    dsort(dub,n);
    csort(cha,n);
    for(int c=0;c<n;c++){
        myOut<<ints[c]<<"  ";
        if(c%10==9 && c!=0){
            myOut<<"\n";
        }
    }
      for(int c=0;c<n;c++){
        myOut<<dub[c]<<"  ";
        if(c%10==9 && c!=0){
            myOut<<"\n";
        }
    }
      for(int c=0;c<n;c++){
        myOut<<cha[c]<<"  ";
        if(c%10==9 && c!=0){
            myOut<<"\n";
        }
    }

    return 0;
}


Here is the header which the function is located in. The problem is here, when I move the doubles over here they get broken. I just don't know how to fix it. Thanks!
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
#include<iostream>
#include<iomanip>
#include<time.h>
#include<stdlib.h>
#include<fstream>
#include<string>
#include<vector>

using namespace std;

void isort(int ints[],int n){
        int x=0;
        int y;
        while(x<n){
            y=x+1;
            while(y<n){
                if(ints[x]>ints[y]){
                    int hold=ints[x];
                    ints[x]=ints[y];
                    ints[y]=hold;
                }
                y++;
            }
            x++;
        }
}

void dsort(double dub[],int n){
        int x=0;
        int y;
        while(x<n){
            y=x+1;
            while(y<n){
                if(dub[x]>dub[y]){
                    double hold=dub[x];
                    dub[x]=dub[y];
                    dub[y]=hold;
                }
                y++;
            }
            x++;
        }
}

void csort(char cha[],int n){
        int x=0;
        int y;
        while(x<n){
            y=x+1;
            while(y<n){
                if(cha[x]>cha[y]){
                    char hold=cha[x];
                    cha[x]=cha[y];
                    cha[y]=hold;
                }
                y++;
            }
            x++;
        }
}


Also, please note that while everything I do may not be the most efficient, it is what my teacher explicitly told me to do.
Last edited on
Also, please note that while everything I do may not be the most efficient, it is what my teacher explicitly told me to do.

If your teacher told you to place executable code inside a header file then the teacher doesn't know what he is doing. You should never, unless working with templates, place executable code inside a header. The header should only have thing like the function prototypes and class or structure definitions. The implementations of the functions should be inside a source file (.cpp) that you have added to your project.

By the way I don't notice any problem when I run the code.

So in the your output file, the doubles section had decimals?
Topic archived. No new replies allowed.