Geologic Time (enum)

I am working with Visual Studio 2010 and I am getting error codes for the equas sign indicated in the comments with all capital letters and I would love help knowing if I am using my enum correctly. Thanks already
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
{#include <iostream>
#include <string>
#include <fstream>

using namespace std;

enum Period {NEOGENE, PALEOGENE, CRETACEOUS, JURASIC, TRIASSIC, PERMIAN, CARBONIFEROUS, DEVONIAN, SILURIAN, ORDOVICIAN, CAMBRIAN, PRECAMBRIAN};

string PeriodCheck (ifstream& inData,
		    Period); // Subfunction to check appropriate period to time frame
int main()
{

	string answer;
	string guess;
	int num;
	ifstream inData;
	inData.open("input.txt");		// Opening text file with the data values (10 questions)

	inData >> num;			// Gets number from file
	while(inData)		// if inData is working
	{
		PeriodCheck;
		cout << "Which period is " << num << " in?";		//Prompts user for guess
		cin >> guess;
		
		if(guess == answer)		// compares guess to answer
		{		// if yes
			cout << "Your guess is correct! The year " << num << " is in the " << answer << "period." << endl;
		}
		if(guess != answer)
		// if answer does not match
		{
			cout << "Your guess is not correct! Guess again." << endl;
			cin >> guess;
		}
	}		// closes while loop

	inData.close();		// meant to close inData

	system("PAUSE");

	return 0;

}		// closes main

string PeriodCheck (ifstream& inData,
					Period)
{
	string answer;
	int num;
	inData >> num;

	while(num <=5000)		//sets up while statement
	{
		if(num<=23)		//begining period to matching answer
		{
			answer = Period(0);		// answer is the enumeration in the 0 spot THIS EQUALS SIGN PRESENTS A CHALLENGE
		}
		if(num<=65)
		{
			answer = Period(1);		// answer is the enumeration in the 1 spot
		}
		if(num<=136)
		{
			answer = Period(2);
		}
		if(num<=192)
		{
			answer = Period(3);
		}
		if(num<=225)
		{
			answer = Period(4);
		}
		if(num<=280)
		{
			answer = Period(5);
		}
		if(num<=345)
		{
			answer = Period(6);
		}
		if(num<=395)
		{
			answer = Period(7);
		}
		if(num<=435)
		{
			answer = Period(8);
		}
		if(num<=500)
		{
			answer = Period(9);
		}
		if(num<=570)
		{
			answer = Period(10);
		}
		if(num<=4500)
		{
			answer = Period(11);
		}

	}		// closes the while loop
}		// ends subfunction} 

Last edited on
I know tabs are important this happened when I copied the code onto the cite. Tabs are included in my code.
Period is an enum, not a function. You'd want to address it as answer = NEOGENE (and respectively for all the other options).

Also, you can put " [code ]" and "[/code]" (not including the space) around your code to make it look nicer.

Edit: Looked at your code more closely (sorry, it's hard to read):
The line PeriodCheck; does nothing because you aren't actually calling a function. Also, an enum such as Period can be assigned to a string (answer is a string and you're trying to assign a Period to it).

Also note that the variable in your PeriodCheck function "string answer;" is unrelated to variable of the same name in your main function.
Last edited on
So now I am using this for my subfunction and it works much better, however my code doesn't function at all. It will not even display my prompting question, it builds ok though.

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
{string PeriodCheck (ifstream& inData,
					Period)
{
	string answer;
	int num;
	inData >> num;

	while(num <=5000)		//sets up while statement
	{
		if(num<=23)		//begining period to matching answer
		{
			answer = NEOGENE;		// answer is the enumeration in the 0 spot THIS EQUALS SIGN PRESENTS A CHALLENGE
		}
		if(num<=65)
		{
			answer = PALEOGENE;		// answer is the enumeration in the 1 spot
		}
		if(num<=136)
		{
			answer = CRETACEOUS;
		}
		if(num<=192)
		{
			answer = JURASIC;
		}
		if(num<=225)
		{
			answer = TRIASSIC;
		}
		if(num<=280)
		{
			answer = PERMIAN;
		}
		if(num<=345)
		{
			answer = CARBONIFEROUS;
		}
		if(num<=395)
		{
			answer = DEVONIAN;
		}
		if(num<=435)
		{
			answer = SILURIAN;
		}
		if(num<=500)
		{
			answer = ORDOVICIAN;
		}
		if(num<=570)
		{
			answer = PRECAMBRIAN;
		}
		if(num<=4500)
		{
			answer = PRECAMBRIAN;
		}

	}		// closes the while loop

	return answer;
}	
// ends subfunction} 
Last edited on
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
int main()
{
    string answer;
    string guess;
    int num;
    ifstream inData;
    inData.open("input.txt"); // Opening text file with the data values (10 questions)
    
    inData >> num;
    while(inData)
    {
        PeriodCheck;
        cout << "Which period is " << num << " in?"; //Prompts user for guess
        cin >> guess;
        
        if(guess == answer) // compares guess to answer
        { // if yes
        cout << "Your guess is correct! The year " << num << " is in the " << answer << "period." << endl;
        }
        if(guess != answer)
        // if answer does not match
        {
        cout << "Your guess is not correct! Guess again." << endl;
        cin >> guess;
        }
    } // closes while loop 


See the part I underlined? You are not calling your function like you probably think. You need a pair of () and you need to pass the correct arguments into your function. http://www.cplusplus.com/doc/tutorial/functions/

You also should assign the return value of your function to something.
Also note that the stuff under line 12 is redundant now because you are handling the cin >> stuff in your function.

Unrelated, but you should also check to make sure your file is opened correctly:
1
2
3
4
    inData.open("input.txt"); // Opening text file with the data values (10 questions)
    if (!inData) {
        cout << "Error could not open input.txt, check your folder!" << endl;
    }
Last edited on
Okay, so after working on it some more now it won't even build. I am really struggling getting the code to work, any advice.
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

{#include <iostream>
#include <string>
#include <fstream>

using namespace std;

enum Period {NEOGENE, PALEOGENE, CRETACEOUS, JURASIC, TRIASSIC, PERMIAN, CARBONIFEROUS, DEVONIAN, SILURIAN, ORDOVICIAN, CAMBRIAN, PRECAMBRIAN};

string PeriodCheck (ifstream& inData,
					Period); // Subfunction to check appropriate period to time frame
int main()
{
	cout << "************************************" << endl;
	cout << " Welcome to Learning Geologic Time!" << endl;
	cout << "************************************" << endl << endl;
	string guess;
	string answer;
	int num;
	ifstream inData;
	inData.open("C://Users//Teresa//Documents//data.txt");		// Opening text file with the data values (10 questions)
	
	inData >> num;			// Gets number from file
	cout << "Which period is " << num << " in?";		//Prompts user for guess
	cin >> guess;
	for (int i = 0; i<=1000; i++)
	{
		PeriodCheck;
		if(guess == answer)		// compares guess to answer
		{		// if yes
			cout << "Your guess is correct! The year " << num << " is in the " << answer << "period." << endl;
		}
		if(guess != answer)
		// if answer does not match
		{
			cout << "Your guess is not correct! Guess again." << endl;
			cin >> guess;
		}
	}
	
	inData.close();		// meant to close inData

	system("PAUSE");

	return 0;

}		// closes main

string PeriodCheck (ifstream& inData,
					Period)
{
	string answer;
	int num;
	inData >> num;

	while(num <=5000)		//sets up while statement
	{
		if(num<=23)		//begining period to matching answer
		{
			answer = NEOGENE;		// answer is the enumeration in the 0 spot THIS EQUALS SIGN PRESENTS A CHALLENGE
		}
		if(num<=65 && num >23)
		{
			answer = PALEOGENE;		// answer is the enumeration in the 1 spot
		}
		if(num<=136 && num >65)
		{
			answer = CRETACEOUS;
		}
		if(num<=192 && num >136)
		{
			answer = JURASIC;
		}
		if(num<=225 && num >192)
		{
			answer = TRIASSIC;
		}
		if(num<=280 && num >225)
		{
			answer = PERMIAN;
		}
		if(num<=345 && num >280)
		{
			answer = CARBONIFEROUS;
		}
		if(num<=395 && num >345)
		{
			answer = DEVONIAN;
		}
               if(num<=435 && num >395)
               {
                       answer = SILURIAN;
                }
                if(num<=500)
               {
                        answer = ORDOVICIAN;
                }
                if(num<=570 && num >500)
                {
                        answer = PRECAMBRIAN;
                }
                if(num<=4500 && num >570)
               {
                        answer = PRECAMBRIAN;
                }


        }		// closes the while loop

        return answer;
}	
// ends subfunction} 
Last edited on
if someone could tell me how to post the fancy codes (like with line numbers and colors) that would be awful helpful too
Edit your post and put [code ] {code here} [ /code] around it, (not including the spaces inside the brackets).

[code]inData.open("C://Users//Teresa//Documents//data.txt");[/code]
Just make it be
inData.open("C:/Users/Teresa/Documents/data.txt");
No double slashes are needed.
Last edited on
I think I figured out the problem, my subfunction is not running inside the main function. I need to figure out how to get it to run and return answer. How do I compare that returned value to the guess?
I don't think you want to use an enum at all here. Ultimately you have to determine if the string that the user enters is correct. If you use an enum, then you'll need a way to convert the string to the enum or vice versa. Since you're already converting the year to an enum, why not cut out the middle man and convert directly to a string?

Also, PeriodCheck() is a little odd because it reads the number from a stream. It's better to have a function do just one think, so I suggest that you change PeriodCheck to:

// given a number, return the string for the period that it represents:
1
2
3
4
5
6
7
8
string numToPeriod(int num)
{
    if (num <= 23) {
        return "NEOGENE";
    } else if (num <= 65) {
        return "PALEOGENE";
    } ... etc.
}


THen in the main program, once you read the number from the file, you can get the corresponding period:
1
2
inData >> num;   //Gets number from file
answer = numToPeriod(num);    // Get the answer. 


Now you can enter the loop where you ask for the answer. Loops like this naturally end in the middle of the code, so it's easiest to use an infinite loop with a break statement:
1
2
3
4
5
6
7
8
9
10
11
while (true) {
    cout << "Which period is " << num << " in?"; //Prompts user for guess
    cin >> guess;
    if (guess == answer) {		 // compares guess to answer
	    cout << "Your guess is correct! The year " << num << " is in the " << answer
		<< "period." << endl;
	    break;  // break out of loop
    } else {
	    cout << "Your guess is not correct! Guess again." << endl;
    }
}
So, I finished it off and I used a random number generator so it can print out as many questions as it wants (yay).
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
{#include <iostream>
#include <string>
#include <cmath>
#include <time.h>

using namespace std;

enum Period {NEOGENE, PALEOGENE, CRETACEOUS, JURASIC, TRIASSIC, PERMIAN, CARBONIFEROUS, DEVONIAN, SILURIAN, ORDOVICIAN, CAMBRIAN, PRECAMBRIAN};
Period period;
string PeriodCheck (int num); // Subfunction to check appropriate period to time frame
int main()
{
	
	cout << "************************************" << endl;
	cout << " Welcome to Learning Geologic Time!" << endl;
	cout << "************************************" << endl << endl;
	string guess;
	string ans;
	int num;
	srand (time(NULL));
	num = rand()%5000 + 23;
	while(num)
	{
		cout << "Which period is " << num << " in? ";		//Prompts user for guess
		cin >> guess;
		for (int i = 0; i < guess.length(); i++)	//Converts the guess to uppercase
		{
			guess[i] = toupper(guess[i]);
		}
		ans = PeriodCheck(num);
		if (guess == ans)
		{
			cout << "Your guess is correct! The year " << num << " is in the " << ans << " period." << endl;
			num = rand()%5000 + 23;
		}
		else
		{
			cout << "Your guess is incorrect. The year " << num << " is not in the " << guess << " period." << endl;
			for (int i = 0; i < guess.length(); i++)
			{
				guess[i] = toupper(guess[i]);
			}
		}
	}

	system("PAUSE");

	return 0;

}		// closes main

string PeriodCheck (int num)
{
		string period;
		if(num<=23)		//begining period to matching answer
		{
			return "NEOGENE";		// answer is the enumeration in the 0 spot THIS EQUALS SIGN PRESENTS A CHALLENGE
		}
		if(num<=65 && num >23)
		{
			return "PALEOGENE";		// answer is the enumeration in the 1 spot
		}
		if(num<=136 && num >65)
		{
			return "CRETACEOUS";
		}
		if(num<=192 && num >136)
		{
			return "JURASIC";
		}
		if(num<=225 && num >192)
		{
			return "TRIASSIC";
		}
		if(num<=280 && num >225)
		{
			return "PERMIAN";
		}
		if(num<=345 && num >280)
		{
			return "CARBONIFEROUS";
		}
		if(num<=395 && num >345)
		{
			return "DEVONIAN";
		}
		if(num<=435 && num >395)
		{
			return "SILURIAN";
		}
		if(num<=500)
		{
			return "ORDOVICIAN";
		}
		if(num<=570 && num >500)
		{
			return "CAMBRIAN";
		}
		if(num<=4500 && num >570)
		{
			return "PRECAMBRIAN";
		}
		else
		{
			return "incorrect";
		}
}	
// ends subfunction} 
Registered users can post here. Sign in or register to post.