double to int conversion errors

Hey all, new to this forum. I'm struggling in my first C++ college course and needed some help. I've written a program for grade caclulations which has modules for sorting and statistics. I was nearly done, with just strange unused variable errors when my teacher helped me out by using identifiers () and typing classes in each module. Now, I cannot get my code to debug at all, I am getting "double to int" conversion errors, and haven't a clue how to get around them. I've worked really hard on this code and am really bummed out, any help would be greatly appreciated!

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
// LolzGeorge
// 11-13-10
// Bonus Assignment

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>
#include <cstdlib>

using namespace std;
void Sort( int ID[], string NAME[], double GRADES[], int size );
void FindHigh(string NAME[],double GRADES[]);
void FindLow(string NAME[],double GRADES[]);
void Stats(double GRADES[]);

int main()
{
    int i,x,m,n,M,size=10;
    int id[]={1,2,3,4,5,6,7,8,9,10};
    double grades[]={99.32, 100, 83.67, 73.67, 62.12, 35.22, 83.66, 23.11, 99.25, 72.86};
    string name[]={"Tom","Mary","John","Jerry","Susan","David","Andy","Ken","Kathy","Frank"},S1,S2,S3,ID,NAME,GRADE;
    double maxVal,minVal,mean,median,variance,stdev,sum,N;
    char selection;
    ofstream outfile;
    ifstream infile;
    outfile.open("Bonus.dat");
    infile>>S1>>S2>>S3;
    cout<<setw(5)<<S1<<setw(10)<<S2<<setw(10)<<S3<<endl;

    for(i=0;i<ID[10];i++)
    {
        infile>>ID>>NAME>>GRADE;
        cout<<setw(5)<<ID[i]<<setw(10)<<NAME[i]<<setw(10)<<GRADE[i]<<endl;
    }
    infile.close();

    for(int m=0; m<N;m++)
    {
        cout<<setw(5)<<id[m]<<setw(10)<<name[m]<<setw(10)<<grades[m]<<endl;
    }

do
  {
   cout  << "MENU\n\n"
         << "1. Sort the grade in a descending order. \n"
         << "2. Find the highest score of the class. \n"
         << "3. Find the lowest score of the class. \n"
         << "4. Calculate the mean, median, variance and standard deviation for the class. \n"
         << "5. Quit\n\n"
         << "SELECTION:\n\n ";

    selection = cin.get();

    switch(selection)
    {
        case '\n':  break;
        case '1': Sort(id,name,grades,N); break;  //Errors in all these cases going double to int.
        case '2': FindHigh(grades,N); break;        //Same
        case '3': FindLow(grades,N); break;        //Same
        case '4': Stats(grades);   break;              //Same
        default: cout << '\a';
    }
   }    
    while( selection != '5' );
    {
      system("pause");
      return 0;
    }
}

void Sort(int ID[],string NAME[],double GRADES[],int N)
{
    double tempg;
    int tempi;
    string temps;

    for (int x=0;x<size-1;x++)
    {
        for(int i=x+1;i<size;i++)
        {
                if (GRADES[i]>GRADES[x])
                {
                    tempi=ID[x];
                    ID[x]=ID[i];
                    ID[i]=tempi;

                     tempg=GRADES[x];
                    GRADES[x]=GRADES[i];
                    GRADES[i]=tempg;

                    temps=NAME[x];
                    NAME[x]=NAME[i];
                    NAME[i]=temps;
                }        
        }
    }

    for(int m=0; m<size;m++)
    {
     cout<<setw(5)<<ID[m]<<setw(10)<<NAME[m]<<setw(10)<<GRADES[m]<<endl;
    }

    return;
}

void FindHigh(double GRADES[],int size)  //and errors at the declarations for each module
{
    system("cls");

    for (int M=1;M<size-1;++M) 
    {
        if (GRADES[M]>maxVal)
        maxVal=GRADES[M];
    }
    cout<<maxVal;
    cout.precision(2);   
    return;
}

void FindLow(double GRADES[], int size)
{
    system("cls");

    minVal=grades[0];
    for (int m=1;m>size-1;m--) 
    {
        if (grades[m]<minVal)
        minVal=grades[m];
    }
    cout<<minVal;
    cout.precision(2);
    return;
}

void Stats(GRADES)
{
    system("cls");

    for (int i=0;i<10;++i)
    {
        sum+=grades[i];
        mean=sum/10;
    }
    cout<<"Mean: "<<mean<<endl;
    
    median=Sort(int ID[],string NAME[],((double GRADES[4]+GRADES[5])/2),int size);
    cout<<"Median: "<<median<<endl;

    variance=_sum/(i-1);
    cout<<"Variance: "<<variance<<endl;

    stdev=sqrt(((pow(sum+2))-((1/sum)*(pow(sum+2))))/(sum-1));
    cout<<"Standard Deviation: "<<stdev<<endl;
    
    system("pause");
}
Last edited on
closed account (1yvXoG1T)
Please post your code inside of [code] tags and where in the program you're getting this error. It'll be a lot easier to answer.
are the edits above sufficient?
The last code tag needs to be this: [/code]
got it! now it looks like normal runtime, thanks! noob mistake >.<
you didn't implement 'FindHigh', 'FindLow', and 'Stats' according to the prototypes.

within those 3 function you're using variables declared at main (and not used there). put the variable to the function where it's used.

N is an uninitialized float variable which you use as int. I think it's supposed to be the size of the arrays used?

median=Sort(int ID[],string NAME[],((double GRADES[4]+GRADES[5])/2),int size); You cannot do it this way. Instead pass them as parameters to 'Stats'

for(i=0;i<ID[10];i++) if you want to access the last element of an array use the size of the array - 1 (9 in this case) since the index is 0 based
coder, I agree with you, my teacher, who holds a phd, told me all variables should go to the top, which perplexed me. I forgot what I assigned N to, I think she actually typed that in, I'll take a second look at it.

median=Sort(int ID[],string NAME[],((double GRADES[4]+GRADES[5])/2),int size); You cannot do it this way. Instead pass them as parameters to 'Stats'

Not sure what you mean here, could you elaborate?
told me all variables should go to the top
I guess she meant to the top of the function they belong to.

Sort(int ID[], ...
int ID[] is a declaration but you want to pass it as a parameter.



this is how it should be declared
1
2
3
void Sort( int ID[], string NAME[], double GRADES[], int size );
...
void Stats(int ID[], string NAME[], double GRADES[], int size );




this is how it should be implemented and called
1
2
3
4
5
6
7
void Stats(int ID[], string NAME[], double GRADES[], int size )
{
...
    Sort(ID, NAME, GRADES, size);
    median=(GRADES[4]+GRADES[5])/2 // since Sort doesn't return anything.
...
}

Thanks for the help, i've tried applying those changes, and will move the corresponding variables from the giant list at the beginning of main() to each individual module. However, I am unsure how to correct the following issue:

1
2
3
4
5
6
7
8
9
10
switch(selection)
    {
        case '\n':  break;
        case '1': Sort(id,name,grades,N); break;  
        case '2': FindHigh(grades,N); break;
        case '3': FindLow(grades,N); break;  
        case '4': Stats(grades);   break;
        default: cout << '\a';
    }
   }    



where I get the following:

1
2
3
4
5
1>warning C4244: 'argument' : conversion from 'double' to 'int', possible loss of data
1>error C2664: 'FindHigh' : cannot convert parameter 1 from 'double [10]' to 'std::string []'
1>Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>error C2664: 'FindLow' : cannot convert parameter 1 from 'double [10]' to 'std::string []'
1>Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

well this is the prototype (line 14):
void FindHigh(string NAME[],double GRADES[]);

this is how you implemented it (line 108):
void FindHigh(double GRADES[],int size)

you see the difference. Same applies to 'FindLow' btw.

first of all your implementation must match the prototype!

on line 60 the compiler expects parameter according to line 14 (NAME and GRADES) but you provide grades and N.
So change line 14 - 16 to the paramater you really want and the compiler will stop complaining.

I wrote:
N is an uninitialized float variable which you use as int.
that problem still exists though and hence the warning
Does the implementation also have to match the cases for the switch operation?
such as in:

1
2
3
4
5
6
7
8
9
10
switch(selection)
    {
        case '\n':  break;
        case '1': Sort(id,name,grades,N); break;  
        case '2': FindHigh(grades,N); break;
        case '3': FindLow(grades,N); break;  
        case '4': Stats(grades);   break;
        default: cout << '\a';
    }
   }    


because I applied your principle, which makes sense, and I agree with, but I am getting more identifier errors :(
Sorry for the double post: this is what I currently have, and its frustrating!

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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>
#include <cstdlib>

using namespace std;
void Sort(int ID[], string NAME[], double GRADES[], int size );
void FindHigh(string NAME[], double GRADES[]);
void FindLow(string NAME[], double GRADES[]);
void Stats(int ID[], string NAME[], double GRADES[], int size );

int main()
{
    int i,x,m,n,M,size=10;
    int ID[]={1,2,3,4,5,6,7,8,9,10};
    double GRADES[]={99.32, 100, 83.67, 73.67, 62.12, 35.22, 83.66, 23.11, 99.25, 72.86};
    string NAME[]={"Tom","Mary","John","Jerry","Susan","David","Andy","Ken","Kathy","Frank"},ID,NAME,GRADE;
    double maxVal,minVal,mean,median,variance,stdev,sum,N;
    char selection;
    ofstream outfile;
    ifstream infile;
    outfile.open("Bonus.dat");
    infile>>S1>>S2>>S3;
    cout<<setw(5)<<S1<<setw(10)<<S2<<setw(10)<<S3<<endl;

    for(i=0;i<ID[10];i++)
    {
        infile>>ID>>NAME>>GRADE;
        cout<<setw(5)<<ID[i]<<setw(10)<<NAME[i]<<setw(10)<<GRADE[i]<<endl;
    }
    infile.close();

    for(int m=0; m<N;m++)
    {
        cout<<setw(5)<<id[m]<<setw(10)<<name[m]<<setw(10)<<grades[m]<<endl;
    }

do
  {
   cout  << "MENU\n\n"
         << "1. Sort the grade in a descending order. \n"
         << "2. Find the highest score of the class. \n"
         << "3. Find the lowest score of the class. \n"
         << "4. Calculate the mean, median, variance and standard deviation for the class. \n"
         << "5. Quit\n\n"
         << "SELECTION:\n\n ";

    selection = cin.get();

    switch(selection)
    {
        case '\n':  break;
        case '1': Sort(ID,NAME,GRADES,size); break;  
        case '2': FindHigh(NAME,GRADES); break;
        case '3': FindLow(NAME,GRADE); break;  
        case '4': Stats(ID,NAME,GRADES,size);   break;
        default: cout << '\a';
    }
   }    
    while( selection != '5' );
    {
      system("pause");
      return 0;
    }
}

void Sort( int ID[], string NAME[], double GRADES[], int size )
{
	system("cls");

	int i,x,m,size=10;
    int ID[]={1,2,3,4,5,6,7,8,9,10};
    double GRADES[]={99.32, 100, 83.67, 73.67, 62.12, 35.22, 83.66, 23.11, 99.25, 72.86};
    string NAME[]={"Tom","Mary","John","Jerry","Susan","David","Andy","Ken","Kathy","Frank"},ID,NAME,GRADE;

    double tempg;
    int tempi;
    string temps;

    for (int x=0;x<size-1;x++)
    {
        for(int i=x+1;i<size;i++)
        {
                if (GRADES[i]>GRADES[x])
                {
                    tempi=ID[x];
                    ID[x]=ID[i];
                    ID[i]=tempi;

                     tempg=GRADES[x];
                    GRADES[x]=GRADES[i];
                    GRADES[i]=tempg;

                    temps=NAME[x];
                    NAME[x]=NAME[i];
                    NAME[i]=temps;
                }        
        }
    }

    for(int m=0; m<size;m++)
    {
     cout<<setw(5)<<ID[m]<<setw(10)<<NAME[m]<<setw(10)<<GRADES[m]<<endl;
    }

    return;
}

void FindHigh(string NAME[], double GRADES[]);
{
    system("cls");

    double GRADES[]={99.32, 100, 83.67, 73.67, 62.12, 35.22, 83.66, 23.11, 99.25, 72.86};
    string NAME[]={"Tom","Mary","John","Jerry","Susan","David","Andy","Ken","Kathy","Frank"},ID,NAME,GRADE;

    for (int M=1;M<size-1;++M) 
    {
        if (GRADES[M]>maxVal)
        maxVal=GRADES[M];
    }
    cout<<maxVal;
    cout.precision(2);   
    return;
}

void FindLow(string NAME[], double GRADES[]);
{
    system("cls");

	int i,x,m,size=10;
    double GRADES[]={99.32, 100, 83.67, 73.67, 62.12, 35.22, 83.66, 23.11, 99.25, 72.86};
    string NAME[]={"Tom","Mary","John","Jerry","Susan","David","Andy","Ken","Kathy","Frank"},ID,NAME,GRADE;
	double maxVal,minVal,mean,median,variance,stdev,sum,N;
    
	minVal=grades[0];
    for (int m=1;m>size-1;m--) 
    {
        if (grades[m]<minVal)
        minVal=grades[m];
    }
    cout<<minVal;
    cout.precision(2);
    return;
}

void Stats(int ID[], string NAME[], double GRADES[], int size )
{
    system("cls");

	int i,x,m,size=10;
    double GRADES[]={99.32, 100, 83.67, 73.67, 62.12, 35.22, 83.66, 23.11, 99.25, 72.86};
    string NAME[]={"Tom","Mary","John","Jerry","Susan","David","Andy","Ken","Kathy","Frank"},ID,NAME,GRADE;
	double maxVal,minVal,mean,median,variance,stdev,sum,N;

    for (int i=0;i<10;++i)
    {
        sum+=grades[i];
        mean=sum/10;
    }
    cout<<"Mean: "<<mean<<endl;
 
	Sort(ID, NAME, GRADES, size);
    median=(GRADES[4]+GRADES[5])/2;
    cout<<"Median: "<<median<<endl;

    variance=_sum/(i-1);
    cout<<"Variance: "<<variance<<endl;

    stdev=sqrt(((pow(sum+2))-((1/sum)*(pow(sum+2))))/(sum-1));
    cout<<"Standard Deviation: "<<stdev<<endl;
    
    system("pause");
}
Line 37, you reference id[m] name[m] grades[m], not only are there no lower case definitions of these variables anywhere but you have a couple of the upper case variants of these variables defined as 2 different types. Are you using a compiler?

"id" is not defined anywhere. C is case sensitive. But ID is defined as both an array of ints and a string.

The INT[] ID or the string ID, neither is the same as id[].
name[] is referring to which one, the array or the string?
string NAME[] ,ID,NAME,GRADE;

case '3': FindLow(NAME,GRADE); break; gives you that error because 'GRADE' is defined as a string and GRADES an array of doubles.

Your function definitions for FindLow() and FindHigh() end before the body of the function due to the semicolon at the end of the function signature.
void FindHigh(string NAME[], double GRADES[]); <---- that semicolon denotes the end of the function body.


You are referencing variables inside those two functions that are not defined inside those functions. The functions cant 'see' them; grades, MaxVal, size. etc


There are a couple of other errors that your compiler should point you right to. Some advice?
1 - Put your variable declarations at the top of the functions they are used in.
2 - Don't reuse variable names, ie.. int GRADES[] and string GRADE. Its confusing the heck out of you.
3 - Pick a convention and stick with it. (Unless your paying a buddy to write this for you). All of a sudden in Stats() you decide to start using _sum? Where is the declaration? Why use underscores? If you find yourself using variations of variable names like sum, sum1, _sum in the same function, you need to rethink your naming standard.




Topic archived. No new replies allowed.