I have been able to get the program to put out the random numbers for the addition problem and tell the user if they're correct, but I have been unsuccessful in my attempts to get the program to loop when the user inputs 'Y'. The teacher's instructions are as follows: "Each time the tutor is played the program should output the current question as well as the result of the users input. In addition, once the user quits the tutor, statistics should be displayed showing the total number of questions this session and the number the user was correct. Finally, using data in the provided input file the user should be shown how many questions they have been asked overall and the number of correct answers overall they have. This data should then be written to the math.txt file so that the numbers are accurate and can be read in the next time the user plays." This is what I have so far.
Welcome to the forum and thank you for using code tags.
Line 10 does not need to be in a function. You can replace line 30 with line 10 and it will work fine.
Line 17 does not need to be a global variable. It will work fine inside main and it should be initialized to 'Y' when defined. Also it is a good practice to initialize all you variables.
Initializing "userLetter" when line 35 can be deleted.
Line 37 - 63 could be put into a while or do/while loop. I would normally use a do/while loop.
I will have to work on it for awhile because that existing code may need changed a little. You will have to add some code to ask the user to play again before the loop ends.
Ok, so I scrapped a lot of stuff and have made some progress with the loops, but i need it to start over if the input is invalid. I can also send you the random files if I need to.
Again the function "seedRandon" is not needed. "srand" should only be called once at the beginning of main. Calling "srand" more than once could lead to problems. After seeing the warnings from my compiler this is what should be used above the do/while loop: srand(static_cast<size_t>(time(NULL))); because the call to "time returns a "size_t" or "unsigned int".
The function "getRandomInt" works, but is not needed. This is most likely optimized to an inline function by the compiler at compile time. For two uses you could just as easily call "rand" where the function call is.
Line 17 does not need to be a global variable. It should be defined inside main where it is used.
The problem you are having starts at line 33 with the first "cin". It is asking the user to enter "Y" or "N" which is entered as "Y enter key". This will extract the "Y" into "userLetter" leaving the "\n" in the input buffer. The next call to "cin" will extract the "\n" from the input buffer and not wait for any keyboard entry. I put a comment on lines 32 and 33 for now and it worked.
The problem with the do/while loop not working starts at line 31 when you set "userLetter" to "\0". Because "userLetter" is a "char" it is not the number zero.but ASCII "0" or "\0". So, when you reach the do/while condition "\0" is not "Y" or "y" and it fails. Line 31 should not be there. "userLetter" should be defined and initialized to "Y".
The two while loops starting at line 56 should not be while loops and if either would be true it would end up being an endless loop.
The first while loop at line 56 is not needed at all. The "cout" statement needs to go before the return.
The second while loop either needs to be an else to an if before the do/while loop or use an outer do/while loop moving lines 32 ans 33 up to be part of the outer do/while loop. The if statement approach only lets the program work once if there is bad input where a do/while loop will continue until you enter good input.
My first rework of your first code had some minor changes to make what you had work. The same is true for the second version. I was able to keep what you had just rearranged it a little.
Earlier I said: The problem you are having starts at line 33 with the first "cin". It is asking the user to enter "Y" or "N" which is entered as "Y enter key". This will extract the "Y" into "userLetter" leaving the "\n" in the input buffer. The next call to "cin" will extract the "\n" from the input buffer and not wait for any keyboard entry. You can disregard this because I found that "char userLetter" some how changed to "int userLetter" and I did not do this. This caused a problem when getting user input because it was not storing a letter, but a number.
Just before the do/while loop ends I added this:
1 2
std::cout << "\n Would yo like to play again? ";
std::cin >> userLetter;
and with "userLetter" defined as a char" it works now.
Hey Andy,
I'm having one last problem that I can't figure out. I have an if statement at the end to total up the number of times the user has attempted questions if the file "math.txt" has already been opened and if not it should open the file and save the information from that attempt which should be the initial. I believe my problem is in the logic of the if statement at the end, because it will not save my overall attempts and will only overwrite what I have saved instead of adding to it.
With just a quick look on line 104 try this writeFile.open("math.txt, std::ios::app");.
You need to open the file for append if you want to add to it.
The same applies to line 119.
You may have a problem opening the same file for input and output at the same time the way you are doing. You could define the file stream as a "fstream" and open the file with "in | out | app". That is one I have not worked with yet, so I am not exactly sure how the write and read would work.
Hey Andy,
I have it reading and writing from the file as it should and I thought I was done, but I have a problem where I say 'N' when asked if I want to continue. The program behaves the same no matter what I input as long as it's not 'Y' or 'y' and outputs both "Thanks For Playing!" and "Invalid Input.". It is suppose to output one or the other then the stats for the session. Thanks for the help and hopefully this will be my last question.
This should not even be where it is that is why you are seeing it just before the program ends because you tell the output to be there. This bit of code should be an else statement at line 76. Which is an else statement to line 51. This way you will stay in the loop until either "Y" or "N", either case, is entered.
FYI line 97 the use of "!=" and "||" does not work well together. Normally when more than one "!=" is used "&&" tends to work better. Not something you need to worry about in your program because it should not be there at all.
I have not checked the file reading and writing yet because I do not know what the input file looks like, so I will have to work on it.