how to skip a function in main program? program can be runned

closed account (STCXSL3A)
i want my main program to skip classify if the group is invalid.
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
  int main()
{
    int x,y,z;
    char answer;

    do  {
         cout <<    "Type in the first score" << endl;
         cin >> x;
         cout <<    "Type in the second score" << endl;
         cin >> y;
         cout <<    "Type in the third score" << endl;
         cin >> z;
         validset (x,y,z);
         classify (x,y,z);
         cout <<    "Type c to continue; s to stop";
         cin >> answer;
         } while (answer == 'c');
         return 0;
}

void validset(int x, int y, int z)
{

    if (x < 0 || x > 300)
        cout << "The group is invalid" << endl;
    else if (y < 0 || y > 300)
        cout << "The group is invalid" << endl;
    else if (z < 0 || z > 300)
        cout << "The group is invalid" << endl;
    else
        cout << "The group is valid" << endl;
    return ;
}
Modify your design so that validset returns a Boolean value that indicates if the scores are valid. Then use:
1
2
3
4
if (validset(x, y, z)) 
{
    classify(x, y, z);
}
Last edited on
change validset to return a bool. If the group is invalid, have it return either true or false. Modify your main function to reflect whether it returned true or false, and either do or don't do whatever you want based on this returned value.

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
bool validset(int x, int y, int z)
{

    if (x < 0 || x > 300)
{
        cout << "The group is invalid" << endl;
        return(false);
}
    else if (y < 0 || y > 300)
{
        cout << "The group is invalid" << endl;
        return(false);
}
    else if (z < 0 || z > 300)
{
        cout << "The group is invalid" << endl;
        return(false);
}
    else
{
        cout << "The group is valid" << endl;
        return(true);
}
cout << "This doesn't make sense, error"
return(false);
}


in your main() function after line 13:
1
2
3
if (validset(x,  y, z) == true)
classify (x,y,z);
else;
Last edited on
closed account (STCXSL3A)
you guys are so helpful thank you so much!!!
You're welcome. Why did you delete the original question? Others may be searching for a similar one ;)
Topic archived. No new replies allowed.