Repetition Control Structures Help

Hello.

I've been having trouble with this code for the last few days now. While I've finally managed to get it to the state where it compiles without an error message, I haven't been able to actually get the output to what is needed.

Here is my code.

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
#include<iostream>

#include<fstream>

#include<iomanip>

#include<cstdio>

#include<string>

using namespace std;

int main()

{
	fstream fWrite;

	cout << " ========================" << endl;

	cout << "  Exam Question Creation";

	cout << "\n ========================" << endl;

	std::cout << "\n\n";

	ofstream outputFile;

	char fileName[32];

	char question[71];

	char ch;

	cout << "\n Enter the name of the file to be used: ";

	cin >> fileName;

	string que;

	char ans;

	ofstream otp_file;

	otp_file.open("DMVexam.txt", ios_base::app);

	int c = 1;

	while (c == 1)

	{

		cout << "\n Enter a YES/NO question (in one line, no more than 70 characters):" << endl;

		getline(cin, que);

		cout << " Enter Your Answer(Y/N):";

		cin >> ans;

		otp_file << que << "\n";

		otp_file << ans << "\n";

		

		outputFile.close();

		getchar();

		cout << " To continue, enter 1. To exit, enter 0. " << endl;

		cin >> c;

		cin.ignore();

	}

	otp_file.close();

}




However, the problem is that is where I'm stuck.

Below is what I'm trying to make it look like.


Exam Question Creation

========================

Enter the name of the file to be used: question.txt

Enter a Yes/No question (in one line, no more than 70 characters):Are there rules on 'turn on red light'?

Enter answer (Y or N): Y

Do you have more question to enter (Y or N): y

Enter a Yes/No question (in one line, no more than 70 characters): Do you need a license to drive?

Enter answer (Y or N): Y

Do you have more question to enter (Y or N): y

11 questions were added this run.

Thank you for using my program!!

PROGRAMMER: Zeke Armstrong

CMSC140 Common Project 3

Due Date: 10/28/2018

Program ended with exit code: 0



Instead of it coming out like that, I can't seem to get the actual question bit to run correctly and it just skips right over into the Y/N answer part and even then, I'm not sure where to go from there.



Any help would seriously be appreciated.
THANK YOU.
Last edited on
PLEASE.

Seriously, I need help really badly.
There's a number of issues in your code, mainly because you declare too many variables. But the main question is: is this an assignment?
Why do you have a variable named "outputFile" and then another variable named "opt_file"? It's confusing. Likewise, remove your "question" variable if you don't ever use it.

I can't seem to get the actual question bit to run correctly and it just skips right over into the Y/N answer part

Because you used cin >> some_variable; before using getline, you need call cin.ignore(); before getline.

Add cin.ignore(); to the line before you call getline on line 54.
And then remove the extra cin.ignore() on line 74.

Your program then runs like this for me:
D:\code\cplusplus244965>main
 ========================
  Exam Question Creation
 ========================



 Enter the name of the file to be used: question.txt

 Enter a YES/NO question (in one line, no more than 70 characters):
Are you okay?
 Enter Your Answer(Y/N):Y
...
 To continue, enter 1. To exit, enter 0.
1

 Enter a YES/NO question (in one line, no more than 70 characters):
Are you sure?
 Enter Your Answer(Y/N):N
...
 To continue, enter 1. To exit, enter 0.
0


Why do you ask the user for a file name if you hardcode the file to be "DMVexam.txt" anyway?
Last edited on
I don't know.

I was kind of stumbling through the whole thing.

Thank you for the help, though.
closed account (SECMoG1T)
tried to comment your code a bit.

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
#include<iostream>

#include<fstream>

//#include<iomanip> not used

//#include<cstdio> not needed

#include<string>

using namespace std;

int main()

{
	///fstream fWrite;  remove not used

	cout << " ========================" << endl;

	cout << "  Exam Question Creation";

	cout << "\n ========================" << endl;

	std::cout << "\n\n";

	///ofstream outputFile; remove not used

	///char fileName[32]; this variable is not used anywhere: i mean to do useful work

	///char question[71]; use std::string instead, behaves much better
	string question;

	///char ch; remove not used

	///cout << "\n Enter the name of the file to be used: ";

	///cin >> fileName; not used after this

	string que; ///declare variables close to where they are used

	char ans;

	ofstream otp_file("DMVexam.txt", ios_base::app);

	int c = 1,total_qstns=0;

	while (c == 1)

	{

		cout << "\n Enter a YES/NO question (in one line, no more than 70 characters):" << endl;

		getline(cin, que, '\n');

		cout << " Enter Your Answer(Y/N):";

		cin >> ans;

		otp_file << que << "\n";

		otp_file << ans << "\n";

		++total_qstns;



		///outputFile.close(); this is not needed: outputFile isn't used anywhere

		///getchar(); remove

		cout << " To continue, enter 1. To exit, enter 0. " << endl;

		cin >> c;

		cin.ignore(1000,'\n');///ignore as much as you can

	}

	otp_file.close();

	cout<<"\n\n"<<total_qstns<<"questions were added this run.\n";
	cout<<"Thank you for using my program!!\n";
	cout<<"PROGRAMMER: Zeke Armstrong\n";
	cout<<"CMSC140 Common Project 3\n";
	cout<<"Due Date: 10/28/2018";
}
Last edited on
Topic archived. No new replies allowed.