problem with while loop

Hi, I'm really new to c++. This code is compiling but after the while loop in ask(), the screen starts flooding up and scrolling down, i dont know whats causing it:

also i know the questions dont really make any sense lol.

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
#include <iostream>
#include <conio.h>

using namespace std;

int i = 0;
int nofqs = 5;
int inscount = 0; //  ins count
int insans[60];

int ask()
{
cout << "enter 'n' when finished adding ins sheets\n\n";
while (insans[inscount] != 'n')
{
inscount++;
cout << "Insert ins #: ";
cin >> insans[inscount];
}

}


int main()
{
string question[nofqs]; //no of q's
string answer[nofqs];

question[0] = "hello";
question[1] = "hello???";
question[2] = "are you ignorant?";
question[3] = "please talk to me!";
question[4] = "im so lonely!!";

for (i = 0; i < nofqs; i++)
{
cout << question[i] << "\n";
cin >> answer[i];
if (answer[i] == "y"){ask();}
}

for (int j = 0; j < nofqs; j++)
{
cout << question[j] << "\n" << answer[j] << "\n";
}

for (int k = 0; k < inscount; k++)
{
cout << insans[k] << "\n";
}






return 0;
}
Last edited on
You're missing a return statement in your ask() function
Your ask function claims to return an int, but it doesn't.

You are leaving junk on the input buffer. Flush it like this:
1
2
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

at the start of the while loop.

Your test will work, but you're testing for a character in something that stores int values; the int value corresponding to the char 'n' is 110, so your user will have to enter 110 to break out of the while loop.

You're using a non-constant variable to define the size of an array:
string question[nofqs];
That is forbidden in C++, although many compilers let you get away with it.
Topic archived. No new replies allowed.