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
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.
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.
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;
}
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.
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:
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;
}
}