I can't figure out what my prof is talking about.

Pages: 12
ok im going to try. this is a tough one. im taking an intro class so im still new to the language.
Quick question. If I take out isvalid() then it wont call that function. So if I condense it to

1
2
3
4
if (X)
return true;
else
return false;


then I wont be calling isvalid() anymore...and I need that to continue my program.


Here is my 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
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
//Cosimo Vilardo
//Assignment 4
//This program will show 3 daily temperatures in NYC.
//It will then let us know if the temperatures are valid,
//the average temperature, and what season we are in.
#include<iostream>
#include <fstream>
using namespace std;
ifstream infile("in1.dat");
ofstream outfile("out1.txt");
void introduction();
bool validtemperature(int,int,int);
void classify(int,int,int);
double findavg(int,int,int);
void whatseason(double);
bool isvalid(int);
int main()
{
    
    int num1, num2, num3, result, count_valid=0, count_invalid=0;
    int totalgroups=0;
    bool valid;
    
    cout.setf(ios::fixed, ios::floatfield);
    cout.precision(2);
    
    introduction();
    
    infile>>num1>>num2>>num3;
    while(infile){
         outfile<<num1<<" "<<num2<<" "<<num3<<endl;
         
         valid=validtemperature(num1,num2,num3);
         
         if (valid==true){
    
              outfile<<"The set of temperatures is valid"<<endl;
    
              count_valid++;
    
              classify(num1,num2,num3);
         }
         else if(valid==false){
    
                   outfile<<"The set of temperatures is invalid"<<endl<<endl;
    
                   count_invalid++;
              }
    
    infile >> num1 >> num2 >> num3;
    
    totalgroups++;          
    }
    
    outfile<<totalgroups<<" total groups"<<endl;
    
    outfile<<count_valid<<" valid groups"<<endl;
    
    outfile<<count_invalid<<" invalid groups"<<endl;
    
infile.close();

outfile.close();    

system ("pause");

return 0;
}

//Introduction to Program
void introduction(){
     outfile<<"Cosimo Vilardo"<<endl;

     outfile<<"Assignment #4"<<endl;

     outfile<<"This program will show 3 daily temperatures in NYC."<<endl;

     outfile<<"It will then let us know if the temperatures are valid,"<<endl;

     outfile<<"the average temperature, and what season we are in."<<endl<<endl;

return;
}
//Check to see if temperature>=-10 and <=105
bool validtemperature(int x,int y,int z){
     
     
     validx=isvalid(x);
     validy=isvalid(y);
     validz=isvalid(z);

     if (validx&&validy&&validz)
          return true;
     else 
          return false;
     
}

//is it valid?
bool isvalid(int x){
     
     if (x>=-10&&x<=105){
          return true;
     }     
     else if(x<-10){
               outfile<<x<<" is too low."<<endl;
               return false;
          }
     else{
          outfile<<x<<" is too big."<<endl;
          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);
    
     return;
}

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

//tells you what season it is according to the temperature
void whatseason(double x){
     outfile<<"The average is "<<x<<endl;
       
     if(x>=100)
          outfile<<"It is roasting season"<<endl<<endl;
     else if(x>=80)
               outfile<<"It is summer"<<endl<<endl;
     else if(x>=60)
               outfile<<"It is spring"<<endl<<endl;
     else if(x>=40)
               outfile<<"It is fall"<<endl<<endl;
     else if(x>=0)
               outfile<<"It is winter"<<endl<<endl;
     else
          outfile<<"It is freezin' season"<<endl<<endl;
return;
}
You still want that function.
The only changes you will make is in the validtemperature function
Last edited on
Quick question. If I take out isvalid() then it wont call that function.

Right, you still need to call isvalid() (once for each parameter to follow your logic). But, you can do the same work as your current function just with one line.
1
2
3
4
5
6
7
8
9
     validx=isvalid(x);
     validy=isvalid(y);
     validz=isvalid(z);

     if (x&&y&&z)
          return true;
     else 
          return false;
  


Is this what your talking about?
sorry i meant this..
1
2
3
4
5
6
7
8
     isvalid(x);
     isvalid(y);
     isvalid(z);

     if (x&&y&&z)
          return true;
     else 
          return false;
my whole output is messed up now and i have no idea what happened -_-
Put your function back to what it was:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Check to see if temperature>=-10 and <=105
bool validtemperature(int x,int y,int z){

     bool validx, validy, validz;

     validx=isvalid(x);
     validy=isvalid(y);
     validz=isvalid(z);
    
     if (validx&&validy&&validz)
          return true;
     else 
          return false;
 }


Don't worry if you can't condense it any further. Your teacher shouldn't take off (m)any marks for it.
Thanks a lot everyone. This was really helpful. I highly appreciate it.

Cheers
Topic archived. No new replies allowed.
Pages: 12