understanding problem

i have the following code, ignore the language, but after running it it just shows me press 1 to continue and it doesnt what is supposed to do, can you point me to where i did the mistake please?

#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
#include <stdlib.h>
#include <windows.h>


using namespace std;
void gotoxy(int x, int y)

{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void starline();
int main()

{
int no=0;
bool har=0;
int b=0;
int ans[10];
float score=0;
int anseasy[10]= {1,1,1,1,1,1,1,1,1,1};
int anshard[10]= {1,1,1,1,1,1,1,1,1,1};
int ansmedium[10]= {1,1,1,1,1,1,1,1,1,1};
int ansin;
int k=0;
int h=0,i=0,j=0,l=0,ea=0,q=0;
float correct=0;
char ch;
string x[10];
string y[10];
string z[10];

char s[100];
ifstream medium;
ifstream hard;
ifstream easy;


int e=0, m=0,n=0, main=0;
starline();
starline();
gotoxy(50,2);
cout<<"Salut, bine ai venit la quiz-ul meu!"<<endl;
starline();
starline();
cout<<" Apasa 1 sa continui\n";
cin>>ch;

while(main<=9)
medium.open("medium.txt");
for(int i=0; i<=9; i++)
{
getline(medium,x[i],'\n') ;
if(i==main)
{


no++;
system("cls");
cout<<"Q="<<endl;
cout<<"("<<no<<")"<<x[i]<<endl;
cout<<"Raspunde cu ce consideri tu 1,2,3,4 :"<<endl;
cin>>ansin;
}
}
if(ansin==ansmedium[main])
{
correct++;
score=score+1;
q=main-ea;
h++;
hard.open("hard.txt");
for(int j=0; j<=9; j++)
{
getline(hard,y[j],'\n');


if(j==q)

{
no++;
system("cls");
cout<<"Q="<<endl;
cout<<"("<<no<<")"<<y[j]<<endl;
cout<<"Raspunde cu ce consideri tu 1,2,3,4 :"<<endl;
cin>>ansin;
if(ansin==anshard[j])
{
correct++;
score=score+5;

}
}
}
}
else

{


ea++;
l=main-h;
easy.open("easy.txt");
for (int k=0; k<=9; k++)
{
getline(easy,z[k],'\n');
if(k==1)
{
no++;
system("cls");
cout<<"Q="<<endl;
cout<<"("<<no<<")"<<z[k]<<endl;
cout<<"Raspunde cu ce consideri tu 1,2,3,4 :"<<endl;
cin>>ansin;
if(ansin==anseasy[k])
{

correct++;
score=score+5;
}

}


}

main++;


if (main==10)
{
if(ea<=9||h<=9)
{

while(ea<=9)
{

easy.open("easy.txt");
for(int t=0; t<=10; t++)
{

getline(easy,z[t],'\n');
if(t==ea)
{
no++;
system("cls");
cout<<"Q="<<endl;
cout<<"("<<no<<")"<<z[t]<<endl;
cout<<"Raspunde cu ce consideri tu 1,2,3,4 :"<<endl;
cin>>ansin;
if(ansin==anseasy[t])
{

correct++;
score=score+3;
}
}
}
ea++;
}
if(ea==10)
{

while(h<=10)
{
hard.open("hard.txt");
for(int w=0; w<=10; w++)
{
getline(hard,y[w],'\n');
if(h==w)
{
no++;
system("cls");
cout<<"Q="<<endl;
cout<<"("<<no<<")"<<y[w]<<endl;
cout<<"Raspunde cu ce consideri tu 1,2,3,4 :"<<endl;
cin>>ansin;
if(ansin==anshard[w])
{
correct++;
score=score+5;
}
}

}
h++;
if (h==10)
{
break;
}
}
}
}
}
}
system("cls");
cout<<"Scorul tau este= "<< score<<" din 30";
cout<<endl<<"Ai raspuns"<<correct<<" intrebari corecte";
cout<<endl<<"Ai raspuns"<<(30-correct)<<"intrebari gresite";
return 0;
}
void starline()
{
for(int j=0; j<119; j++)
{
cout<<"*";
}
cout<<endl;
}

Edit your post and add code formatting.
http://www.cplusplus.com/articles/jEywvCM9/

It would help us, and yourself, if you can specifically find which part of the code isn't doing what you want it to. Go through the logic in your code, line by line, and check (either with a debugger or print statements) if the code is being reached or in a correct state.
Last edited on
It's hard to know what's wrong when you haven't said what the right behavior is.

Still, when I apply indenting to the code, a few things stick out:

Line 52 prompts for you to press 1. Did you press one? It won't continue unless you enter a non-space character.

Lines 55 and 56 are an infinite loop. I suspect that you mean to add { and } somewhere.

There's a lot of repeated code. Can you factor that out into a function?
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
#include <stdlib.h>
#include <windows.h>

using namespace std;
void
gotoxy(int x, int y)
{
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void starline();

int
main()
{
    int no = 0;
    bool har = 0;
    int b = 0;
    int ans[10];
    float score = 0;
    int anseasy[10] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
    int anshard[10] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
    int ansmedium[10] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
    int ansin;
    int k = 0;
    int h = 0, i = 0, j = 0, l = 0, ea = 0, q = 0;
    float correct = 0;
    char ch;
    string x[10];
    string y[10];
    string z[10];

    char s[100];
    ifstream medium;
    ifstream hard;
    ifstream easy;

    int e = 0, m = 0, n = 0, main = 0;
    starline();
    starline();
    gotoxy(50, 2);
    cout << "Salut, bine ai venit la quiz-ul meu!" << endl;
    starline();
    starline();
    cout << " Apasa 1 sa continui\n";
    cin >> ch;

    while (main <= 9)
	medium.open("medium.txt");
    for (int i = 0; i <= 9; i++) {
	getline(medium, x[i], '\n');
	if (i == main) {

	    no++;
	    system("cls");
	    cout << "Q=" << endl;
	    cout << "(" << no << ")" << x[i] << endl;
	    cout << "Raspunde cu ce consideri tu 1,2,3,4 :" << endl;
	    cin >> ansin;
	}
    }
    if (ansin == ansmedium[main]) {
	correct++;
	score = score + 1;
	q = main - ea;
	h++;
	hard.open("hard.txt");
	for (int j = 0; j <= 9; j++) {
	    getline(hard, y[j], '\n');

	    if (j == q)
	    {
		no++;
		system("cls");
		cout << "Q=" << endl;
		cout << "(" << no << ")" << y[j] << endl;
		cout << "Raspunde cu ce consideri tu 1,2,3,4 :" << endl;
		cin >> ansin;
		if (ansin == anshard[j]) {
		    correct++;
		    score = score + 5;

		}
	    }
	}
    } else
    {

	ea++;
	l = main - h;
	easy.open("easy.txt");
	for (int k = 0; k <= 9; k++) {
	    getline(easy, z[k], '\n');
	    if (k == 1) {
		no++;
		system("cls");
		cout << "Q=" << endl;
		cout << "(" << no << ")" << z[k] << endl;
		cout << "Raspunde cu ce consideri tu 1,2,3,4 :" << endl;
		cin >> ansin;
		if (ansin == anseasy[k]) {

		    correct++;
		    score = score + 5;
		}

	    }

	}

	main++;

	if (main == 10) {
	    if (ea <= 9 || h <= 9) {

		while (ea <= 9) {

		    easy.open("easy.txt");
		    for (int t = 0; t <= 10; t++) {

			getline(easy, z[t], '\n');
			if (t == ea) {
			    no++;
			    system("cls");
			    cout << "Q=" << endl;
			    cout << "(" << no << ")" << z[t] << endl;
			    cout << "Raspunde cu ce consideri tu 1,2,3,4 :" << endl;
			    cin >> ansin;
			    if (ansin == anseasy[t]) {

				correct++;
				score = score + 3;
			    }
			}
		    }
		    ea++;
		}
		if (ea == 10) {

		    while (h <= 10) {
			hard.open("hard.txt");
			for (int w = 0; w <= 10; w++) {
			    getline(hard, y[w], '\n');
			    if (h == w) {
				no++;
				system("cls");
				cout << "Q=" << endl;
				cout << "(" << no << ")" << y[w] << endl;
				cout << "Raspunde cu ce consideri tu 1,2,3,4 :" << endl;
				cin >> ansin;
				if (ansin == anshard[w]) {
				    correct++;
				    score = score + 5;
				}
			    }

			}
			h++;
			if (h == 10) {
			    break;
			}
		    }
		}
	    }
	}
    }
    system("cls");
    cout << "Scorul tau este= " << score << " din 30";
    cout << endl << "Ai raspuns" << correct << " intrebari corecte";
    cout << endl << "Ai raspuns" << (30 - correct) << "intrebari gresite";
    return 0;
}

void
starline()
{
    for (int j = 0; j < 119; j++) {
	cout << "*";
    }
    cout << endl;
}
Topic archived. No new replies allowed.