Error: expected 'while' before numeric constant.

Nov 18, 2016 at 8:07pm
Hello,

I have been trying to make a program where the user is presented with an array of 10 variables between the numbers 50 and 150. I have several other questions but right now I'm running into this issue.

The specific errors are these:

main.cpp|59|error: expected 'while' before numeric constant|
main.cpp|59|error: expected '(' before numeric constant|
main.cpp|59|error: expected ')' before ';' token|

Here is the code I am working on.

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
#include <iostream>
#include <iomanip>
#include <cmath>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main()
{

    float avg;
    float evn;
    float mini;
    char opt;
    int randval[10];
    srand(time(NULL));
    int n;
    bool loop = true;

    do
    {
        for(n=0; n<10; n++)
        {
            randval[n]=50 + rand( ) % (150 + 1-50);
            cout << randval[n] << "    " <<endl;
        }
     cout << endl;
     cout << "[A]verage    [E]ven    [M]inimum     [Q]uit";
     cout << "Select an option";
     cin >> opt;

    if(opt=='a'||opt=='A')
        {

            cout << "The average value of the array is:" << avg;
            cout << setiosflags(ios::showpoint) << setprecision(2);
        }
        else if (opt=='e'||opt=='E')
        {


            cout << "The even number of the array are:" << evn;
            cout << setiosflags(ios::showpoint) << setprecision(2);

        }
        else if (opt=='m'||opt=='M')
        {

            cout << "The minimum number of the array is:" << mini;
            cout << setiosflags(ios::showpoint) << setprecision(2);
        }
        while(opt=='q'||opt=='Q')
        {
            loop=false;
        }
    }

 return 0;
}          \\this line give off all three errors.


I know its not 100% complete, but if I can get rid of this error then maybe I'll be able to finish it.
Nov 18, 2016 at 8:18pm
Line 4-5: The correct headers are <cstdlib> and <ctime>.

Line 21: You start a do/while loop. Where is the while associated with the do? Note: It's not line 53 as this is a separate while loop.

Line 53-56: This while loop will loop forever if opt is 'q' or "Q'. Why is this a loop?

Line 60: The slashes in your comment are the wrong direction.


Last edited on Nov 18, 2016 at 8:19pm
Nov 18, 2016 at 10:27pm
I Didn't realize I used the wrong headers.

Isn't a do while loop supposed to have while at the end of the do loop?

The program is supposed to repeat unless 'q' or 'Q' is selected. When it repeats the array is supposed to generate a new set of numbers. The user will be prompted to choose from a small selection of options. the Choice of 'q' and/or 'Q' is supposed to end the program. Is it somehow not a part of the do loop?
Nov 18, 2016 at 10:39pm
Ok! I got it!

the end needed to look like:

1
2
3
4
5
6
7
8
9
10
11
12
            cout << "The minimum number of the array is:" << mini;
            cout << setiosflags(ios::showpoint) << setprecision(2);
        }

    }
    while(opt=='q'||opt=='Q');
    {
     loop = false;
    }

 return 0;
}


Now I all I have to do is get the math equations done right.
Nov 19, 2016 at 4:12pm
I would suggest removing the {} at lines 7 and 9. Although the braces are allowed, they are not needed and make line 8 appear as if it is part of the while statement at line 6.

In fact line 8 doesn't do anything. You never check the variable loop and immediately exist after setting it.
Last edited on Nov 19, 2016 at 4:56pm
Topic archived. No new replies allowed.