The file is fine as a small program, but you're using some very unhealthy programming practices at the moment.
Goto
is messy and should be avoided 99% of the time, i cant even think of an example off my head where a goto is the only option. It makes reading code and the program a whole lot more complicated. Goto's can commonly be replaced with while loops or functions(which also make code a whole of easier to read!). Another bad habit is using the
system("cls")
function. Here is a list of what the system needs to do before calling the CLS function:
suspend your program
call the operating system
open an operating system shell (relaunches the O/S in a sub-process)
the O/S must now find the CLS command
allocate the memory to execute the command
execute the command
deallocate the memory
exit the OS
resume your program |
As you can see, the function can be very memory heavy and slow. Instead use this function(implying that you use windows):
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
void cls() //windows h function to replace screen with nulls
{
DWORD n;
DWORD size;
COORD coord = {0};
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
GetConsoleScreenBufferInfo ( h, &csbi );
size = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
GetConsoleScreenBufferInfo ( h, &csbi );
FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
SetConsoleCursorPosition ( h, coord );
}
|
This function simply reads all characters on the screen and replaces them with whitespaces. It takes a whole lot less memory and is simple to call.
---------------------------------------------------------------------------------------------------------------
Onto the actual program: As a note, make sure you are closing your files after opening them. Using your constructor to open files can be a lot more convenient also, as in:
1 2 3
|
ofstream info("Results.txt");
//...
info.close();
|
Your functions could also use some trimming, if you have say, 25 questions, thats 25 different ofstream objects you're creating. I used a reference to an ofstream object to save memory and only have to open the file once. Heres my revised code(works on my computer fine).
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
|
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
void q1(int &correct, ofstream& info)
{
char input;
gateway1:
system("cls");
cout << "What is my favorite color? \n";
cout << "\nA. Yellow";
cout << "\nB. Orange";
cout << "\nC. Purple";
cout << "\nD. Green \n";
cout << "\nInput: ";
cin >> input;
if (input == 'c')
{
correct++;
}
else if (input == 'a' || input == 'b' || input == 'd')
{
correct+= 0;
}
else
{
cout << "Invalid Input. Try again.";
system("pause>nul");
goto gateway1;
}
info << "Question 1: c:" << input;
}
void q2(int &correct, ofstream& info)
{
char input;
gateway1:
system("cls");
cout << "What is my favorite movie? \n";
cout << "\nA. Mulan";
cout << "\nB. Winnie The Pooh";
cout << "\nC. The Goofy Movie";
cout << "\nD. Tangled \n";
cout << "\nInput: ";
cin >> input;
if (input == 'd')
correct++;
else if (input == 'a' || input == 'b' || input == 'c')
correct += 0;
else
{
cout << "Invalid Input. Try again.";
system("pause>nul");
goto gateway1;
}
info << "\nQuestion 2: d:" << input << endl;
}
int main()
{
int correct;
ofstream info("Results.txt");
q1(correct, info);
q2(correct, info);
info.close();
return 0;
}
|