Need help with my final program.

My program doesnt seem to be working correctly. Can someone 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
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
#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) { // while a value was successfully read
         cout<<num1<<" "<<num2<<" "<<num3<<endl; // process it
         infile>>num1>>num2>>num3; // try to get another value
     }
    
    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";
         count_valid++;
         classify(num1,num2,num3);
    }
    else if(valid==false){
              cout<<"The set of temperatures is invalid";
              count_invalid++;
              }
    totalgroups++;          
    infile >> num1 >> num2 >> num3;
    }
    
    
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==1;
    }
       
    if (y>=-10&&y<=105){
         y==1;
    }
    else{ 
         y==1;
    }     
    
    if (z>=-10&&z<=105){
         z==1;
    }     
    else{ 
         z==1;
    }     
    
    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 "<<x<<endl;
       
       if(x>=100)
            cout<<"It is roasting season";
       else if(x>=80&&x<100)
                 cout<<"It is summer";
       else if(x>=60&&x<80)
                 cout<<"It is spring";
       else if(x>=40&&x<60)
                 cout<<"It is fall";
       else if(x>=0&&x<40)
                 cout<<"It is winter";
       else if(x<0)
                 cout<<"It is freezin' season";
}


HERE IS THE DATA IT IS RECEIVING FROM THE DATA FILE:

12 15 18
23 27 25
56 59 63
44 49 44
-12 1 -9
100 110 102
105 103 102
99 95 91
67 75 80
72 77 69
34 40 41
-15 -17 -8
106 107 100
22 25 19
96 93 97
77 79 83
50 47 51
-10 -1 0
106 108 109
-11 -15 -19
29 34 37
71 65 69
101 102 99
45 55 65
37 37 37

All it seems to do when i run it is print out the sets of data from the data file.
Well, I do notice that no matter what the values are in the validtemperature() routine, you are setting the variables x, y and z to 1. Shouldn't the variables be set to 0 after the else?
Yes that was my mistake I fixed that....but still nothing has changed.
Btw then you doing inting alot at the top, you dont need to write =0 until very last one
What does the file look like?
Which file? I posted my data file above. Here is my output.

12 15 18
The set of temperatures is invalid23 27 25
The set of temperatures is invalid56 59 63
The set of temperatures is invalid44 49 44
The set of temperatures is invalid-12 1 -9
The set of temperatures is invalid100 110 102
The set of temperatures is invalid105 103 102
The set of temperatures is invalid99 95 91
The set of temperatures is invalid67 75 80
The set of temperatures is invalid72 77 69
The set of temperatures is invalid34 40 41
The set of temperatures is invalid-15 -17 -8
The set of temperatures is invalid106 107 100
The set of temperatures is invalid22 25 19
The set of temperatures is invalid96 93 97
The set of temperatures is invalid77 79 83
The set of temperatures is invalid50 47 51
The set of temperatures is invalid-10 -1 0
The set of temperatures is invalid106 108 109
The set of temperatures is invalid-11 -15 -19
The set of temperatures is invalid29 34 37
The set of temperatures is invalid71 65 69
The set of temperatures is invalid101 102 99
The set of temperatures is invalid45 55 65
The set of temperatures is invalid37 37 37
The set of temperatures is invalid
anyone?
@Cosimo Vilardo
I fixed up the program a bit. You can compare the code I'm posting to what you had. A few brackets were in the wrong places and you had too many infile >> ... Removed the checks of == true or == false. Just used if ( valid) or if (!valid). Anyway, the code works using Visual C++ 2008 Express.
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
// Temperature Checking.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include <fstream>

using namespace std;

ifstream infile("in1.dat");
ofstream outfile("out1.txt");

int validtemperature(int,int,int);
void classify(int,int,int);
double findavg(int,int,int);
void whatseason(double);

int main()
{
    
    int num1 = 0;
	int num2 = 0;
	int num3 = 0;
	int count_valid = 0;
	int count_invalid = 0;
	int totalgroups = 0;
    bool valid = false;
    
    if(!infile)
    {
         cout << "Unable to open file for reading...\n";
    }    
        
    while(infile) 
	{ // while a value was successfully read
         
		
		infile >> num1 >> num2 >> num3; // try to get another value
			if ( infile )
		{
			cout << num1 << " " << num2 << " " << num3 << endl; // process it
			valid = validtemperature(num1,num2,num3);
		}
     	if (valid && infile)
		{
         cout<<"The set of temperatures is valid" << endl;
         count_valid++;
         classify(num1,num2,num3);
		 }
		else if(!valid)
		{
         cout<<"The set of temperatures is invalid" << endl;
         count_invalid++;
		}
		 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 && y && z )
	{
            return true;
        }
    else
	{
            return false;
        }
}
//calls find average and then calls whatseason with average
void 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
void whatseason(double x)
{
	cout << "The average is "<<x<<endl;
	outfile << "The average is " << x << endl;

	if(x>=100)
	{
		cout<<"It is roasting season." << endl;
		outfile << "It is roasting season." << endl;
	}
	else if(x>=80&&x<100)
	{
		cout<<"It is summer." << endl;
		outfile << "It is summer." << endl;
	}
	else if(x>=60&&x<80)
	{
		cout<<"It is spring." << endl;
		outfile << "It is spring." << endl;
	}
	else if(x>=40&&x<60)
	{
		cout<<"It is fall." << endl;
		outfile << "It is fall." << endl;
	}
	else if(x>=0&&x<40)
	{
		cout<<"It is winter." << endl;
		outfile << "It is winter." << endl;
	}
	else if(x<0)
	{
		cout<<"It is freezin' season." << endl;
		outfile << "It is freezin' season." << endl;
	}
}
Last edited on
Topic archived. No new replies allowed.