If-else problem.

I noticed my output is always going through with the first part of my if-else statement. Can anyone tell me why?

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
#include<iostream>
#include <fstream>
using namespace std;
ifstream infile("in1.dat");
ofstream outfile("out1.txt");
int validtemperature(int,int,int);
int classify(int,int,int);
double findavg(int,int,int);
double whatseason(double);
int main()
{
    
    int num1, num2, num3, result, count_valid=0, count_invalid=0;
    int totalgroups=0;
    bool valid;
    
    if(!infile)
    {
         cout << "unable to open file for reading";
    }    
       
    infile >> num1 >> num2 >> num3;
    while (infile) {
    
    cout<<num1<<" "<<num2<<" "<<num3<<endl;
    valid=validtemperature(num1,num2,num3);
    if (valid=true){
         cout<<"The set of temperatures is valid"<<endl;
         count_valid++;
         classify(num1,num2,num3);
         infile >> num1 >> num2 >> num3;
    }
    else if(valid=false){
              cout<<"The set of temperatures is invalid"<<endl;
              count_invalid++;
              infile >> num1 >> num2 >> num3;
              }
    totalgroups++;          
    }
    
infile.close();
outfile.close();    
system ("pause");
return 0;
}


//Check to see if temperature>=-10 and <=105
int validtemperature(int x,int y,int z){

    if(x>=-10&&x<=105){
         x==1;
    }
    else{
         x==0;
    }
       
    if (y>=-10&&y<=105){
         y==1;
    }
    else{ 
         y==0;
    }     
    
    if (z>=-10&&z<=105){
         z==1;
    }     
    else{ 
         z==0;
    }     
    
    if (x==1&&y==1&&z==1){
         return true;
    }
    else{
         return false;
    }

}
//calls find average and then calls whatseason with average
int classify(int x,int y,int z){
    
    double average;
    
    average=findavg(x,y,z);
    whatseason(average);
    
}

//finds average of the 3 intergers from data file
double findavg(int x,int y,int z){
    
    int sum;
    double avg;
    
    sum=x+y+z;
    avg=sum/3;
    
    return avg;
}

//tells you what season it is according to the temperature
double whatseason(double x){
       cout<<"The average is "<<(double)x<<endl;
       
       if(x>=100)
            cout<<"It is roasting season"<<endl;
       else if(x>=80&&x<100)
                 cout<<"It is summer"<<endl;
       else if(x>=60&&x<80)
                 cout<<"It is spring"<<endl;
       else if(x>=40&&x<60)
                 cout<<"It is fall"<<endl;
       else if(x>=0&&x<40)
                 cout<<"It is winter"<<endl;
       else if(x<0)
                 cout<<"It is freezin' season"<<endl;
return 0;
}


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
Here is my output:
12 15 18
The set of temperatures is valid
The average is 15
It is winter
23 27 25
The set of temperatures is valid
The average is 25
It is winter
56 59 63
The set of temperatures is valid
The average is 59
It is fall
44 49 44
The set of temperatures is valid
The average is 45
It is fall
-12 1 -9
The set of temperatures is valid
The average is -6
It is freezin' season
100 110 102
The set of temperatures is valid
The average is 104
It is roasting season
105 103 102
The set of temperatures is valid
The average is 103
It is roasting season
99 95 91
The set of temperatures is valid
The average is 95
It is summer
67 75 80
The set of temperatures is valid
The average is 74
It is spring
72 77 69
The set of temperatures is valid
The average is 72
It is spring
34 40 41
The set of temperatures is valid
The average is 38
It is winter
-15 -17 -8
The set of temperatures is valid
The average is -13
It is freezin' season
106 107 100
The set of temperatures is valid
The average is 104
It is roasting season
22 25 19
The set of temperatures is valid
The average is 22
It is winter
96 93 97
The set of temperatures is valid
The average is 95
It is summer
77 79 83
The set of temperatures is valid
The average is 79
It is spring
50 47 51
The set of temperatures is valid
The average is 49
It is fall
-10 -1 0
The set of temperatures is valid
The average is -3
It is freezin' season
106 108 109
The set of temperatures is valid
The average is 107
It is roasting season
-11 -15 -19
The set of temperatures is valid
The average is -15
It is freezin' season
29 34 37
The set of temperatures is valid
The average is 33
It is winter
71 65 69
The set of temperatures is valid
The average is 68
It is spring
101 102 99
The set of temperatures is valid
The average is 100
It is roasting season
45 55 65
The set of temperatures is valid
The average is 55
It is fall
37 37 37
The set of temperatures is valid
The average is 37
It is winter
Press any key to continue . . .
if (valid=true){} is supposed to be if (valid == true){}.
Ok I have fixed what you said but now my out put is this.

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
12 15 18
The set of temperatures is valid
The average is 15
It is winter
23 27 25
The set of temperatures is valid
The average is 25
It is winter
56 59 63
The set of temperatures is valid
The average is 59
It is fall
44 49 44
The set of temperatures is valid
The average is 45
It is fall
-12 1 -9
The set of temperatures is valid
The average is -6
It is freezin' season
100 110 102
The set of temperatures is valid
The average is 104
It is roasting season
105 103 102
The set of temperatures is valid
The average is 103
It is roasting season
99 95 91
The set of temperatures is valid
The average is 95
It is summer
67 75 80
The set of temperatures is valid
The average is 74
It is spring
72 77 69
The set of temperatures is valid
The average is 72
It is spring
34 40 41
The set of temperatures is valid
The average is 38
It is winter
-15 -17 -8
The set of temperatures is valid
The average is -13
It is freezin' season
106 107 100
The set of temperatures is valid
The average is 104
It is roasting season
22 25 19
The set of temperatures is valid
The average is 22
It is winter
96 93 97
The set of temperatures is valid
The average is 95
It is summer
77 79 83
The set of temperatures is valid
The average is 79
It is spring
50 47 51
The set of temperatures is valid
The average is 49
It is fall
-10 -1 0
The set of temperatures is invalid
106 108 109
The set of temperatures is valid
The average is 107
It is roasting season
-11 -15 -19
The set of temperatures is valid
The average is -15
It is freezin' season
29 34 37
The set of temperatures is valid
The average is 33
It is winter
71 65 69
The set of temperatures is valid
The average is 68
It is spring
101 102 99
The set of temperatures is valid
The average is 100
It is roasting season
45 55 65
The set of temperatures is valid
The average is 55
It is fall
37 37 37
The set of temperatures is valid
The average is 37
It is winter 


Only 1 is invalid... I have at least 6 that are supposed to be invalid.
why doesn't this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    infile >> num1 >> num2 >> num3;
    while (infile) {
    
    cout<<num1<<" "<<num2<<" "<<num3<<endl;
    valid=validtemperature(num1,num2,num3);
    if (valid=true){
         cout<<"The set of temperatures is valid"<<endl;
         count_valid++;
         classify(num1,num2,num3);
         infile >> num1 >> num2 >> num3;
    }
    else if(valid=false){
              cout<<"The set of temperatures is invalid"<<endl;
              count_invalid++;
              infile >> num1 >> num2 >> num3;
              }
    totalgroups++;          
    }


look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    while (infile) 
    {
           infile >> num1 >> num2 >> num3;
    
           cout<<num1<<" "<<num2<<" "<<num3<<endl;
           valid=validtemperature(num1,num2,num3);
           if (valid==true)
           {
               cout<<"The set of temperatures is valid"<<endl;
               count_valid++;
               classify(num1,num2,num3);
           }
           else
           {
                cout<<"The set of temperatures is invalid"<<endl;
                count_invalid++;
            }
            totalgroups++;          
    }


both code do the same thing, which I am thinking you screwing up the pattern of the reads.
Last edited on
Topic archived. No new replies allowed.