Output error using enum and for loop

I am having trouble with the output on my program. The Switch statement causes only the endingDate to output. When I added the For statement at line 126, it continued to only output the endingDate, but multiple times. I originally had an if-else-if in function Range, but was advised to change it to a nested if statement. It did not change my ouput results. I am uncertain of what my professor meant in part of his advice. (He is currently unavailable so I cannot ask him).

Professor's advice: "The enum does not lend itself to the for loop very well, particularly in this case. I would have the for loop check for particularly years that correspond to the geologic times.  For example if the input was 23 then the enum should be Neogene. An enum is just an identification and you can not do arithmetic or even display them easily."

I thought I did this. Can anyone explain what he might mean, or offer any other insight to fix my problem?

NOTE: I cannot use arrays or classes as we have not had instruction on these topics.

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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
  //This program helps students learn the periods of geological time.
//The user enters a range of prehistoric dates (in millions of years) and
//the program outputs the periods that are in that range.
//Creted by Kayla Lochman
//CISP1010R51
//Professor Adcock

#include <iostream>
#include <string>
#include <cctype>			//For toupper

using namespace std;

enum PeriodName {
	NEOGENE, PALEOGENE, CRETACEOUS, JURASSIC, TRIASSIC, PERMIAN, CARBONIFEROUS, DEVONIAN,
	SILURIAN, ORDOVICIAN, CAMBRIAN, PRECAMBRIAN
};
PeriodName name;

struct PeriodDate
{
	float startingDate;
	float endingDate;
};

PeriodDate date;

//Function Prototypes
bool NotValid(PeriodDate date);
//This function that both dates are in range. Startdate must be
//greater than 23 (million) and the end date must be greater than 
//the start date.

bool Continue(char answer);
//This function asks the user if they want to enter more dates.
//If true, do- while loop in main reruns.

void GetData(PeriodDate &date);
//This function prompts user for dates and loops if NotValid returns true.

PeriodName Range(string &name);
//This function assigns a variable in enum PeriodName for each range of millions of years.

int main()
{
	char answer;
	string eraName;
	do
	{
		GetData(date);				//Prompt user for input	

		//Print the prehistoric period name for the range entered
		cout << "The prehistoric dates included in the range "
			<< date.startingDate << "-" << date.endingDate << " are:" << endl;

		for (name = NEOGENE; name <= PRECAMBRIAN; name = static_cast<PeriodName>(name + 1))
		{
			switch (Range(eraName))
			{
			case NEOGENE: cout << "Neogene" << endl;
				break;
			case PALEOGENE: cout << "Paleogene" << endl;
				break;
			case CRETACEOUS: cout << "Creataceous" << endl;
				break;
			case JURASSIC: cout << "Jurassic" << endl;
				break;
			case TRIASSIC: cout << "Triassic" << endl;
				break;
			case PERMIAN: cout << "Permian" << endl;
				break;
			case CARBONIFEROUS: cout << "Carbonifereous" << endl;
				break;
			case DEVONIAN: cout << "Devonian" << endl;
				break;
			case SILURIAN: cout << "Silurian" << endl;
				break;
			case ORDOVICIAN: cout << "Ordovician" << endl;
				break;
			case CAMBRIAN: cout << "Cambrian" << endl;
				break;
			case PRECAMBRIAN: cout << "Precambrian" << endl;
				break;
			}
		}
		cout << "Would you like to enter more prehistoric dates?" << endl;
		cout << "Enter Y for yes, N for no." << endl;
		cin >> answer;
		answer = toupper(answer);

		while ((answer != 'Y') && (answer != 'N'))
		{
			cout << "Error. Enter Y or N." << endl;
			cin >> answer;
			answer = toupper(answer);
		}

	} while (Continue(answer));

	cin.get();
	cin.get();
	return 0;
}

//***************************************************************************************

bool NotValid(PeriodDate date)
{
	float startTest = date.startingDate;
	float endTest = date.endingDate;

	if (startTest <= 0.0 || endTest <= 0.0)
		return true;
	else if (endTest >= startTest)
		return true;
	else
		return false;
}

//**************************************************************************************
bool Continue(char answer)
{
	if (answer == 'N')
		return false;
	else
		return true;
}

//***************************************************************************************

void GetData(PeriodDate &date)
{
	//Loop prompt until user enters 2 valid dates.
	do
	{
		cout << "Please enter the range of prehistoric dates(in million of years)." << endl;
		cout << "Starting date: " << endl;
		cin >> date.startingDate;
		cout << "Ending date: " << endl;
		cin >> date.endingDate;

		if (NotValid(date))
		{
			cout << "Error. The ending date must be greater than the starting date." << endl;
			cout << "Example: 400 BC is greater than 4000 BC." << endl;
			cout << "Dates must be positive numbers in the form of millions of years." << endl;
			cout << "Example: 23,000,000 would be entered as 23." << endl;
		}
	} while (NotValid(date));
}

//************************************************************************************

PeriodName Range(string &name)
{
	if ((date.startingDate <= 23.0) || (date.endingDate <= 23.0))
	{
		return NEOGENE;
		if ((date.startingDate <= 65.0) || (date.endingDate <= 65.0))
		{
			return PALEOGENE;
			if ((date.startingDate <= 136.0) || (date.endingDate <= 136.0))
			{
				return CRETACEOUS;
				if ((date.startingDate <= 192.0) || (date.endingDate <= 192.0))
				{
					return JURASSIC;
					if ((date.startingDate <= 225.0) || (date.endingDate <= 225.0))
					{
						return TRIASSIC;
						if ((date.startingDate <= 280.0) || (date.endingDate <= 280.0))
						{
							return PERMIAN;
							if ((date.startingDate <= 345) || (date.endingDate <= 345.0))
							{
								return CARBONIFEROUS;
								if ((date.startingDate <= 395) || (date.endingDate <= 395.0))
								{
									return DEVONIAN;
									if ((date.startingDate <= 435.0) || (date.endingDate <= 435.0))
									{
										return SILURIAN;
										if ((date.startingDate <= 500.0) || (date.endingDate <= 500.0))
										{
											return ORDOVICIAN;
											if ((date.startingDate <= 570.0) || (date.endingDate <= 570.0))
											{
												return CAMBRIAN;
												if ((date.startingDate > 570.0) || (date.endingDate > 570.0))
												{
													return PRECAMBRIAN;
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}
Last edited on
Topic archived. No new replies allowed.