Bool Statement Issue

Hello,

I have made a lot of good progress here, but I can't seem to get my simple bool statement to work.

The problem being solved is irrelevant at this point...What I need to know is why when my chkFun statement is set to false, at line 50, the code still executes line 57 before running my "COUNTING LOOP" again.

The statement to set chkFun back to true does not happen until after the COUNTING LOOP initiates again, at line 17.

I've had similar issues with bool statements in the past. Am I missing something?

Thank you for taking the time to analyze the code.

Like I said, you don't really need to grasp what the code is trying to solve; its not even done. It compiles and executes correctly as it stands, it just always displays the "is a funny #!" at the end, on every #, to include the ones that also displayed "is not a funny #!"

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
 
//START CODE//////////////////////////////
            //totNum to count all numbers. It starts at 7. 7 is the first #
            //that is not in the Funny Sequence (first prime above 5)
            unsigned int totNum=7;
            //primeA5 is meant to be a counter that is going to create 
            //all prime numbers, above 7, and below totNum
            unsigned int primA5;
            //funChk is a bool that gets set to false, if it is found that the #
            //is not a funny #.
            bool funChk = true;
            //COUNTING LOOP
            //Counts all #s between 7,  the first non-funny #, and up, until the
            //1475th # is found. 
            for (totNum;totNum<=24;totNum++) {
                cout<<endl<<"/"<<totNum<<"/"<<" is our #"<<endl;
                        //Reset funChk bool with every new totNum. 
                        funChk = true;
                        //DIVISIBILITY FOR LOOP
                        //Below is the for loop that runs all numbers, between 6
                        //(above 5) & the totNum being analyzed. 
                            int praC=1;
                        for (int i=6; i<=totNum; i++) {
                            cout<<"For "<<"*"<<totNum<<"*"<<endl;
                    cout<<i<<" is the "<<praC<<" # between 5 & "<<totNum<<endl;
                            praC++;
                        bool check = true;
                                //PRIME FOR LOOP
                                //Checks each number to see if it is a prime #
                                for (int j=2; j<=sqrt(i); j++) {
                                        if (i%j==0) {
                                        check = false;
                                        }
                                }
                                //If it is a prime #, then this executes
                                if (check==true) {
                                cout<<" & "<<i<<" is prime"<<endl;
                                //Convert values to floats so totNum/i is
                                //precise
                                float totNumF=static_cast<float>(totNum);
                                float iF=static_cast<float>(i);
                                //totNum/i to see if divisible 
                                float funCalc=static_cast<float>(totNumF/iF);
                                cout<<"float:"<<funCalc<<endl;
                                        if (funCalc==1 ||
                                        funCalc==2 ||
                                        funCalc==3 ||
                                        funCalc==5) {
                                        cout<<totNum<<" is not a funny #"<<endl;
                                        //Set funChk bool to false
                                        bool funChk = false;
                               //This statement ends the DIVISIBILITY FOR LOOP
                                        i=totNum+1;
                                        }
                                }
                                
                        }
                if (funChk==true) {
                    cout<<totNum<<" is a funny #!"<<endl;
                } 
            }
Last edited on
For the funChk bool, I changed them all to an unsigned short that is switching between 1 and 0. It works fine with that. What am I missing here?
On line 51, you are declaring and defining a new, more localized bool with the same name ("funChk").

The life of the localized bool is bound by the scope of the if-control structure, which terminates two lines below.
lol. Xismn, I appreciate you taking the time to find that for me. I feel so much better now... and stupid at the same time.

Thanks again!
Topic archived. No new replies allowed.