Problems with some old C++ coding ive found.

Ive found some old C++ project that my son did in his course for college from a few years back, and having previously enjoyed doing programming, i decided to try some of them, i consider myself mediocre at best, anyway one of his later things he was supposed to do was to design a program which should; read in 5 temperatures of the past week put them into an array, by reading the names of the past days into a separate parallel array, find the average of the temperature of the past week and finally and most importantly detect which days the the temperature of the school was below 10 and state them.
This is my code as of now, there seems to be a persistent problem with the line
83 if getline(days, day_temps[loop_counter]);
It keeps showing up as an error. I was hoping that someone could shed a little light on where i'm going wrong with it. 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
159
160
161
162
163
164
165
#include "stdafx.h"
#include <iostream>
#include <fstream>              // Allows for the use of read from/write too arrays
#include <conio.h>
#include <string>
#include <ctime>               


using namespace std;

// Constant Varibles

int MAX =5,  array_elements = 5, loop_counter;
char dat[10], tim[10];
// Declare global varibles

double day_temps[5];

// Declare Methods

void menu();
void get_temps();
void get_average();
void get_days_closed();
void initialise_the_array();

void menu()

{
	// this uses a local variable 

	int choice;

    // To Print the menu choices

	do
	{
		     

		cout << "\t\t -- School Temperature Imput Area --\n" << endl << endl << endl;
		cout << " -----------------------------------------------" << endl;
		cout << "\t1.  Enter in the weekly temperatures" << endl;
		cout << "\t2.  Average tempretures of the week "        << endl;
		cout << "\t3.  Days the School had been Closed " << endl;
		cout << "\t4.  Exit Program                " << endl;
		cout << " ------------------------------------------------" << endl;

		cout << " Please make a selection: ";
		cin  >> choice;

		switch (choice)   // Switch statement allow the user to choose an option.

		// Start of the switch statements
		{
		case 1:
			get_temps();
		    break;

		case 2:
			get_average();
			break;

		case 3:
			get_days_closed();
			break;

		} // End of switch statement

		system ("cls");   // Clears the screen

	} 
	while (choice != 4); // 4 will exit the program

}//end of methods

void initialise_days_array()

{
	ifstream days ("days.txt");

		for (loop_counter =0; loop_counter < MAX; loop_counter++)
		{
		   if getline(days, day_temps[loop_counter]);
		  
		} // End if statement

		{
			cout << "Unable to read file!!" <<endl <<endl ;
			cout << "Press any key to continue. ";
			_getch();

			cout << endl  << endl;
	
		} // end the else statement

days.close();

} // end of initialise_days method


void get_temps()

{
	system("cls");

		for (loop_counter = 0; loop_counter < MAX; loop_counter++)

		{
			cout << "Please enter the temperature for " << day_temps[loop_counter] << ":";

			cin  >>day_temps[loop_counter];
			cout << endl;
		} // end of for loop

	

} // end of get_temps method

void get_average()

{
	double total, average;
	int count;

	total = 0;

	for (count = 0; count < MAX; count ++)

		total = (total + day_temps[count]);

		average = (total / 5);

		cout << endl << "The average temperature for the week was " << average << "degrees";

		_getch();

}

void get_days_closed()

// states the days the school was closed

{
	int count;
    string line;

	// Opens text file

	ifstream days("days.txt");

	for (count =0; count < MAX; count ++)

	if(day_temps[count] <10) // States the days the school had been closed

		cout << endl << "The school was CLOSED on";
	
	else // states the school was closed on that day

	{ 
		cout << endl << endl << "The school was OPEN on:";
		if (days.is_open())
		_getch();
	}
	_getch();
}


First, if conditions must be surrounded by parentheses. Second, getline works with a string, not a variable of type double.
Never put a semi colon at the end of the if statement parenthesis.

should just be
1
2
3
if( ... )
{
}


notice there is no semi colon after the )
OK Ive removed the the semi colon, but still error C2061: syntax error : identifier 'getline'
Comes up and im still completely stumped with what to do.
What is that if there for anyway? It does nothing. Get rid of it.

Let's look at the function getline:
http://www.cplusplus.com/reference/string/getline/

So, it takes two parameters. The first parameter is an istream, and the second parameter is a string.

How are you calling it?
getline(days, day_temps[loop_counter])

What kind of object is day_temps[loop_counter] (which, as you will have noted above, should be a string)?
Last edited on
Topic archived. No new replies allowed.