Can't find the bug. Please help me.

The error message is:

..\Playground\: In function 'int main()':
..\Playground\:105:14: error: expected '}' before 'else'
}else{
^
..\Playground\: At global scope:
..\Playground\:110:1: error: expected declaration before '}' token
}

I can't seem to find the extra '}' or lack of.

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
 #include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
	string pw="abcdefghijklmnopqrstuvwxyz";//26
	string scharacters="!@#$%^&*><!@#$%^&*><!@#$%^";//26
	string ucharacters="ABCDEFGHIJKLMNOPQRSTUVWXYZ";//26
	
	bool s=false;
	bool u=false;
	
	cout<<"How long does the password need to be? "; //First question
	int len;
	cin>>len;
	cout<<len<<endl;
	int hrb=1;
	while(hrb==1){
	    if (len>pw.length()){
	        pw+=pw;
	        scharacters+=scharacters;
	        ucharacters+=ucharacters;
	    }else{
	        hrb=0;
	    }
	}
    //--------------------------------------------------------------------------
    cout<<"Does it need to contain uppercase letters? "; //Second question
    string up;
    cin>>up;
    cout<<up<<endl;
    if (up=="yes" || up=="Yes" || up=="Yeah" || up=="yeah"){
        pw=pw+ucharacters;
        u=true;
    }else if (up=="No" || up=="no"){
        u=false;
    }
    //--------------------------------------------------------------------------
    cout<<"Does it need to contain special characters? "; //Third question
    string sp;
    cin>>sp;
    cout<<sp<<endl;
    if (sp=="yes" || sp=="Yes" || sp=="Yeah" || sp=="yeah"){
        pw=pw+scharacters;
        s=true;
    }
    else if (sp=="No" || sp=="no"){
        s=false;
    //--------------------------------------------------------------------------
    }
    srand(time(0));
    for(int x=0; x<len; x++){
        int ran=0 + (rand() % len);
        pw[x]=pw[ran];
        pw.erase(ran,1);
    }
    for(int x=len; x<pw.length();x++){
        pw.erase(len,30);
    }
    //--------------------------------------------------------------------------
    bool cs=false;
    if (s==true){
        for(int x=0; x<=len - 1; x++){
        char a=pw[x];
        int y=0;
        while (y<= scharacters.length()){//I had a for loop here but for some reason the for loop wasn't working so I just used a while loop instead. 
             if (scharacters[y]==a)
                cs=true; 
                y=y+1;
            }   
        }
    }
    //---------------------------------------------------------------------------
    if (cs==0 && s==true){
        srand(time(0));
        int random=0 + (rand() % pw.length());
        pw[random]=scharacters[random];
    }
    //---------------------------------------------------------------------------
    bool cu=false;
    if (u==true){
        for(int x=0; x<=len - 1; x++){
        char a=pw[x];
        int y=0;
        while (y<= ucharacters.length()){ 
             if (ucharacters[y]==a)
                cu=true; 
                y=y+1;
            }   
        }
    }
    //---------------------------------------------------------------------------
    int pizza=1;
    while (pizza==1){
        if (cu==0 && u==true){
            srand(time(0) + 1);
            int rando=0 + (rand() % pw.length());
            for(int x=0; x>=scharacters.length(); x++){
                if(rando==scharacters[x]){
                    int pizza=1;
                }
            }else{
                pizza=2;
            }
        }
    }
}
This is why I prefer Allman style indentation over K&R or its variants (https://en.wikipedia.org/wiki/Indent_style), missing/extra braces easier stand out more easily:
Your if loop that started on line 96 was not closed before an else was introduced on line 103:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  while (pizza==1)
    {
        if (cu==0 && u==true)
        {
            srand(time(0) + 1);
            int rando=0 + (rand() % pw.length());
            for(int x=0; x>=scharacters.length(); x++)
            {
                if(rando==scharacters[x])
                {
                        int pizza=1;
                }
            }
        }
        else
        {
            pizza=2;
        }
    }

And there is a hanging closing brace on line 108, the closing brace on line 107 already encloses main().

Topic archived. No new replies allowed.