Questions about college basic C++

The function is designed to
1.post the temperatures that come in from the various weather stations
2.display the temperatures in both fahrenheit and celsius for each weather station
3.calculate and display the highest temperature as well as the lowest among the five stations.

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
#include <std_lib_facilities.h>
using namespace std;

struct WeatherStation  {
	string StationDesignation;   
	double Temperature; 
};

void Post(vector<WeatherStation>&);
void HighLow(vector<WeatherStation>&);
void Daily(vector<WeatherStation>&);
void Menu();

int main()
{
	vector<WeatherStation> List;
	string Command="";
    while(Command!="Quit") 
	{
		Menu();
		cout<<"Command: ";
		getline(cin,Command);
		if(Command=="Post")
			Post(List);
		else if(Command=="HighLow")
			HighLow(List);
		else if(Command=="Daily")
			Daily(List);
		else if(Command!="Quit"&&Command!="Post"&&Command!="HighLow"&&Command!="Daily")	
			cout<<"Please type one of the four words--'Post', 'HighLow', 'Daily', or 'Quit'.\n\n";
	}
	system("pause");
 }
 
void Post(vector<WeatherStation>&List)
{
	WeatherStation Loc;
	Loc.StationDesignation="";
	Loc.Temperature=0;
	Loc.StationDesignation="Tciitcgatc"; 
	List.push_back(Loc);
	Loc.StationDesignation="Redwood Haven";
	List.push_back(Loc);
	Loc.StationDesignation="Barrier Mts.";
	List.push_back(Loc);
	Loc.StationDesignation="Nina`s Folly";
	List.push_back(Loc);
	Loc.StationDesignation="Sooly`s Hill";
	List.push_back(Loc);
	Loc.StationDesignation="Twin Cones Park";
	List.push_back(Loc);
	cout << "\nEnter Temperatures Below..." << endl;

	int i;
	double Temp;

	for(i=0;i<List.size();++i)  
	{
		cout<<"\nTemperature Station: " << List[i].StationDesignation << endl;  
		cout<<"Temperature: ";  
		cin>>Temp;
		List[i].Temperature=Temp;
	}
	cout<<endl;
	cin.ignore(100, '\n');
}

void HighLow(vector<WeatherStation>&List)
{
	int i;
	double low=List[0].Temperature;
	for(i=1;i<List.size();++i)
		low=List[i].Temperature>low?low:List[i].Temperature;
	double high=List[0].Temperature;
	for(i=1;i<List.size();++i)
		high=List[i].Temperature<high?high:List[i].Temperature;
	cout<<"\n\n===============================Temperature Report=============================="<<endl
		<<"\n\t\t\tFahrenheit\tCelsius"<<endl
		<<"-------------------------------------------------------------------------------"
		<<setprecision(4)
		<<"\nLowest Temperature =\t\t"<<low<<"\t"<<(low-32)*5/9<<endl
		<<"-------------------------------------------------------------------------------"
		<<"\nHighest Temperature =\t\t"<<high<<"\t"<<(high-32)*5/9<<endl<<endl
		<<"-------------------------------------------------------------------------------\n\n";
}

void Daily(vector<WeatherStation>&List)
{
	double sum=0, mean=0;
	int i;
	for (i=0;i<List.size();++i)
		sum+=List[i].Temperature;
	mean=sum/List.size();
	cout<<"\n\n===============================Temperature Report=============================="
		<<"\n\n\t\t\tFahrenheit\tCelsius"<<endl
		<<"-------------------------------------------------------------------------------"
		<<setprecision(4)
		<<"\nMean Temperature =\t"<<mean<<"\t\t"<<(mean-32)*5/9<<endl
		<<"-------------------------------------------------------------------------------\n"
		<<"Raw Data:\n\n";
	for (i=0;i<List.size();++i)
		cout<<List[i].StationDesignation<<"\t\t"<<List[i].Temperature<<"\t\t"<<(List[i].Temperature-32)*5/9<<endl;
	cout<<"-------------------------------------------------------------------------------\n\n\n";
}

void Menu()
{
    cout<<"Choices........................................................................" << endl
		<<"-------------------------------------------------------------------------------"<<endl
		<<"\tPost......allows the user to post temperatures" << endl
		<<"\tHighLow...displays the report of high and low temperatures" << endl
		<<"\tDaily.....display the daily report" << endl
		<<"\tQuit......exits the program"<<endl
		<<"-------------------------------------------------------------------------------\n";
}


But if I "Post" the data twice, the first time is normal--there will shows the 6 stations. But for the second time, it shows the 6 stations twice, which makes it 12. Anyone has any idea how can I fix it?

Thank you very much!!
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
#include <cstdlib>
#include <iostream> //my guess to which libraries you are using 
#include <vector>
#include <string> 
using namespace std;//maybe move past this

struct WeatherStation  {
	string StationDesignation;   
	double Temperature; 
};

void Post(vector<WeatherStation>&);
void HighLow(vector<WeatherStation>&);
void Daily(vector<WeatherStation>&);
void Menu();

int main()
{
	vector<WeatherStation> List;
	string Command="";
    while(Command!="Quit") 
	{
		Menu();
		cout<<"Command: ";
		getline(cin,Command);
		if(Command=="Post")//maybe a switch statment here? 
			Post(List);
		else if(Command=="HighLow")
			HighLow(List);
		else if(Command=="Daily")
			Daily(List);
		else if(Command!="Quit"&&Command!="Post"&&Command!="HighLow"&&Command!="Daily")	
			cout<<"Please type one of the four words--'Post', 'HighLow', 'Daily', or 'Quit'.\n\n";
        List.erase(List.begin(), List.end()); //should fix your problem 
	}
	//system("pause"); this is bad
    return 0; //you forgot to return a value  
}

void Post(vector<WeatherStation>&List)
{
	WeatherStation Loc;
	Loc.StationDesignation="";
	Loc.Temperature=0;
	Loc.StationDesignation="Tciitcgatc"; 
	List.push_back(Loc);
	Loc.StationDesignation="Redwood Haven";
	List.push_back(Loc);
	Loc.StationDesignation="Barrier Mts.";
	List.push_back(Loc);
	Loc.StationDesignation="Nina`s Folly";
	List.push_back(Loc);
	Loc.StationDesignation="Sooly`s Hill";
	List.push_back(Loc);
	Loc.StationDesignation="Twin Cones Park";
	List.push_back(Loc);
	cout << "\nEnter Temperatures Below..." << endl;
    
	int i;
	double Temp;
    
	for(i=0;i<List.size();++i)  
	{
		cout<<"\nTemperature Station: " << List[i].StationDesignation << endl;  
		cout<<"Temperature: ";  
		cin>>Temp;
		List[i].Temperature=Temp;
	}
	cout<<endl;
	cin.ignore(100, '\n');
}

void HighLow(vector<WeatherStation>&List)
{
	double low=List[0].Temperature;
	for(int i=0;i<List.size();++i)//forgot to set i to 0 and declare in for scope
		low=List[i].Temperature>low?low:List[i].Temperature;
	double high=List[0].Temperature;
	for(int i=0;i<List.size();++i)
		high=List[i].Temperature<high?high:List[i].Temperature;
	cout<<"\n\n===============================Temperature Report=============================="<<endl
    <<"\n\t\t\tFahrenheit\tCelsius"<<endl
    <<"-------------------------------------------------------------------------------"
    <<"\nLowest Temperature =\t\t"<<low<<"\t"<<(low-32)*5/9<<endl
    <<"-------------------------------------------------------------------------------"
    <<"\nHighest Temperature =\t\t"<<high<<"\t"<<(high-32)*5/9<<endl<<endl
    <<"-------------------------------------------------------------------------------\n\n";
}

void Daily(vector<WeatherStation>&List)
{
	double sum=0, mean=0;
	for (int i=0;i<List.size();++i)//declare i in the for loop scope 
		sum+=List[i].Temperature;
	mean=sum/List.size();
	cout<<"\n\n===============================Temperature Report=============================="
    <<"\n\n\t\t\tFahrenheit\tCelsius"<<endl
    <<"-------------------------------------------------------------------------------"
    <<"\nMean Temperature =\t"<<mean<<"\t\t"<<(mean-32)*5/9<<endl
    <<"-------------------------------------------------------------------------------\n"
    <<"Raw Data:\n\n";
	for (i=0;i<List.size();++i)
		cout<<List[i].StationDesignation<<"\t\t"<<List[i].Temperature<<"\t\t"<<(List[i].Temperature-32)*5/9<<endl;
	cout<<"-------------------------------------------------------------------------------\n\n\n";
}

void Menu()
{
    cout<<"Choices........................................................................" << endl
    <<"-------------------------------------------------------------------------------"<<endl
    <<"\tPost......allows the user to post temperatures" << endl
    <<"\tHighLow...displays the report of high and low temperatures" << endl
    <<"\tDaily.....display the daily report" << endl
    <<"\tQuit......exits the program"<<endl
    <<"-------------------------------------------------------------------------------\n";
}

you also have the option so do List.resize(6) and then instead of push_back() you can just do List[iterator] = what_ever
Last edited on
First off, your code gets a bit messy at times. For instance, the line: else if(Command!="Quit"&&Command!="Post"&&Command!="HighLow"&&Command!="Daily") could be replaced with just else

for(i=0;i<List.size();++i) could be changed to for(i=0;i<6;++i) and it would solve the problem.

The last thing is that I had to capitalize my commands. You should use the tolower() function to allow them user to put commands in whether they have capitals or not.
Hi ui uiho and paulthepenguin,

Thank you for your help,
but instead of system("pause"), what else should I use?

and I am confused that how can I use the tolower() function?

I tried to replaced else if(Command!="Quit"&&Command!="Post"&&Command!="HighLow"&&Command!="Daily") with just else, but it shows the "Please type one of the four words--'Post', 'HighLow', 'Daily', or 'Quit'." after I input "Quit". Any other ways to solve that?



p.s. to use switch, it has not to be string.
Last edited on
to use tolower() you need to #include <cctype>

tolower() makes everything in a string or a char array lowercase, so no matter what case they type in, it works. Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <cctype>
using namespace std;

int main()
{
    string str = "THIS IS ALL CAPS";
    int length = str.size();
    for(int i = 0; i < length; i++)
    str[i] = tolower(str[i]);
    
    cout << "You entered: " << str;
    return 0;
}


this is all caps
But if I "Post" the data twice, the first time is normal--there will shows the 6 stations. But for the second time, it shows the 6 stations twice, which makes it 12. Anyone has any idea how can I fix it?


In your Post function, you add 6 weather stations to the list every time you call it, so this isn't terribly surprising. The solution is to move the list building out of Post, because it doesn't belong there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
vector<WeatherStation> CreateStations()
{
	static WeatherStation stations[] =
	{
		{"Tciitcgatc",      0.0},
		{"Redwood Haven",   0.0},
		{"Barrier Mts.",    0.0},
		{"Nina's Folly",    0.0},
		{"Sooly's Hill",    0.0},
		{"Twin Cones Park", 0.0}
	};

	return vector<WeatherStation>(stations, stations + sizeof(stations) / sizeof(stations[0]) ) ;
}


Then in main:

vector<WeatherStation> List = CreateStations();

While Post becomes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Post(vector<WeatherStation>&List)
{
	cout << "\nEnter Temperatures Below..." << endl;

	int i;
	double Temp;

	for(i=0;i<List.size();++i)  
	{
		cout<<"\nTemperature Station: " << List[i].StationDesignation << endl;  
		cout<<"Temperature: ";  
		cin>>Temp;
		List[i].Temperature=Temp;
	}
	cout<<endl;
	cin.ignore(100, '\n');
}
but instead of system("pause"), what else should I use?

std::cin.ignore();
std::cin.get();
Topic archived. No new replies allowed.