I struggle with two problems.

I keep struggling with two lines. It keeps saying like this

the left brace '{' was unmatched at the end of the file (line 8)

IntelliSense: expected 'while' (line 68)

I looked around. They seem fine.

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
  #include <iostream>
#include <string>
#include <stdio.h>
#include <cstdlib>
using namespace std;

int main()
{
	string a, b, c, d, e, g, h;
	int f, answer;
	g = "Username";
	h = "Password";
	do {
		std::cout << "Username:" << endl;
		std::getline(std::cin, h);

		if (h == "Username")
		{
			std::cout << "Password:" << endl;
			std::cin >> g;
		}
		if (g != "Password")
		{
			std::cout << "Invalid Password. Try again." << endl;
		}
		else
		{
			std::cout << "Invalid Username. Try again." << endl;
			exit(0);
		}
		while (g != "Password");
		std::cout << "Hello there!" << endl;
		std::cout << "What is your first name?" << endl;
		std::getline(std::cin, a);
		std::cout << "What is your middle name?" << endl;
		std::getline(std::cin, b);
		std::cout << "What is your last name?" << endl;
		std::getline(std::cin, c);
		std::cout << "Where are you from?" << endl;
		std::getline(std::cin, d);
		std::cout << "Where are you working?" << endl;
		std::getline(std::cin, e);
		std::cout << "How old are you?" << endl;
		std::cin >> f;
		std::cout << "My full name is \b " << a << " \b " << b << " \b " << c << "." << endl;
		std::cout << "I am from \b " << d << "." << endl;
		std::cout << "My work is in \b " << e << "." << endl;
		std::cout << "I am \b " << f << " years old." << endl;
		cin.ignore();
		std::cout << "How old am I?" << endl;
		std::cin >> answer;
		if (answer < 32)
		{
			std::cout << "Wrong! I wish I can go back to that age. :)" << endl;
		}
		else if (answer > 32)
		{
			std::cout << "Wrong! Do I look old to you?!" << endl;
		}
		else
		{
			std::cout << "Whoa! That's unbelievable! You got it!" << endl;
		}
		cin.get();
		system("Pause");

		return 0;
	}
Last edited on
Count your beginning braces { and the ending braces } the numbers should match.

It looks like you're missing and ending brace that matches your while() statement to the do statement.
What do you mean by the numbers should match? I put { in there. There are 15 braces in total.
There are 15 braces in total.

This should be an even number.

How many opening braces?

How many closing braces?

I actually solved that. A problem is that it won't go back to username if username is wrong. It keeps moving on to password instead of going back to username.

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
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;

int main()
{
	string a, b, c, d, e, g, h;
	int f, answer;
	g = "Password";
	h = "Username";
	do {
		std::cout << "Username:" << endl;
		std::getline(std::cin, h);
		if (h != "Username")
		{
			{
				std::cout << "Invalid username. Try again." << endl;
			
		}
		}
		std::cout << "Password:" << endl;
		std::getline(std::cin, g);
		if (g != "Password")
		{
			std::cout << "Invalid password. Try again." << endl;
		}
		else
		{
			std::cout << "Access Granted..." << endl;
			std::cout << "Hello there!" << endl;
			std::cout << "What is your first name?" << endl;
			std::getline(std::cin, a);
			std::cout << "What is your middle name?" << endl;
			std::getline(std::cin, b);
			std::cout << "What is your last name?" << endl;
			std::getline(std::cin, c);
			std::cout << "Where are you from?" << endl;
			std::getline(std::cin, d);
			std::cout << "Where are you working?" << endl;
			std::getline(std::cin, e);
			std::cout << "How old are you?" << endl;
			std::cin >> f;
			std::cout << "My full name is \b " << a << " \b " << b << " \b " << c << "." << endl;
			std::cout << "I am from \b " << d << "." << endl;
			std::cout << "My work is in \b " << e << "." << endl;
			std::cout << "I am \b " << f << " years old." << endl;
			cin.ignore();
			std::cout << "How old am I?" << endl;
			std::cin >> answer;
			if (answer < 32)
			{
				std::cout << "Wrong! I wish I can go back to that age. :)" << endl;
			}
			else if (answer > 32)
			{
				std::cout << "Wrong! Do I look old to you?!" << endl;
			}
			else
			{
				std::cout << "Whoa! That's unbelievable! You got it!" << endl;
			}
			cin.get();
		}
	} 
		while (g != "Password");
			system("Pause");

			return 0;
		}
Last edited on
First you really need to use your indentation style consistently, it'll make reading your program easier and following the program logic possible:
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
#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

int main()
{
   string a, b, c, d, e, g, h;
   int f, answer;
   g = "Password";
   h = "Username";

   do
   {
      std::cout << "Username:" << endl;
      std::getline(std::cin, h);

      if (h != "Username")
      {
         {
            std::cout << "Invalid username. Try again." << endl;

         }
      }

      std::cout << "Password:" << endl;
      std::getline(std::cin, g);

      if (g != "Password")
      {
         std::cout << "Invalid password. Try again." << endl;
      }
      else
      {
         std::cout << "Access Granted..." << endl;
         std::cout << "Hello there!" << endl;
         std::cout << "What is your first name?" << endl;
         std::getline(std::cin, a);
         std::cout << "What is your middle name?" << endl;
         std::getline(std::cin, b);
         std::cout << "What is your last name?" << endl;
         std::getline(std::cin, c);
         std::cout << "Where are you from?" << endl;
         std::getline(std::cin, d);
         std::cout << "Where are you working?" << endl;
         std::getline(std::cin, e);
         std::cout << "How old are you?" << endl;
         std::cin >> f;
         std::cout << "My full name is \b " << a << " \b " << b << " \b " << c << "." << endl;
         std::cout << "I am from \b " << d << "." << endl;
         std::cout << "My work is in \b " << e << "." << endl;
         std::cout << "I am \b " << f << " years old." << endl;
         cin.ignore();
         std::cout << "How old am I?" << endl;
         std::cin >> answer;

         if (answer < 32)
         {
            std::cout << "Wrong! I wish I can go back to that age. :)" << endl;
         }
         else if (answer > 32)
         {
            std::cout << "Wrong! Do I look old to you?!" << endl;
         }
         else
         {
            std::cout << "Whoa! That's unbelievable! You got it!" << endl;
         }

         cin.get();
      }
   } while (g != "Password");

   system("Pause");

   return 0;
}


A problem is that it won't go back to username if username is wrong. It keeps moving on to password instead of going back to username.


There are a couple of solutions of this. One would be to use another loop, the other would be to use the continue statement when the username is "wrong".



To solve your problem you can either create a new loop or break it.
You can do:

do while ( h != "Username")
{
std::cout << "Invalid username. Try again." << endl;
std::cout << "Please re-enter your username : "<<endl;
std::getline(std::cin, h);
}

Therefore, unless the Username is correct it will ask again the question

Last edited on
The best way is like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 do
   {
      Username: //I added this one.
      std::cout << "Username:" << endl;
      std::getline(std::cin, h);

      if (h != "Username")
      {
         {
            std::cout << "Invalid username. Try again." << endl;
            goto Username; //I added this one too. 

         }
      }


But I never thought about the best message you wrote. I tested the line you put before you said so. It caused a problem with putting certain username. For example:

Username: userna

It will say Invalid username. Try again.

Please re-enter your username:
Username: username
It would say Please re-enter your username again unless I add Username: and goto Username;

It solves the problem.

But I was thinking about counting 3 attempts or whatever it is before it uses break; But I am not sure about counting down.
Last edited on
I don't know whether it works but maybe you can try sth like that
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int count = 1; 

if (h != "Username")
{
       if (count <= 3 )
       {
               std::cout << "Invalid username. Try again." << endl;
               goto Username;
               count ++;
           
       }
  
      else
      {
              return 0; //or return EXIT_FAILURE; 
      }

}


Last edited on
Thank you! I am going to test this one next. I hope your code is awesome.
I tested the code you put for counting. It doesn't show me counts. I kept putting wrong username over and over.
This thread switched completely from a "Tell me where I missed a closing brace" to a "Worst coding practices" collection.
You have to put the
int count = 1;

Outside the do loop otherwise each time, the command will reinitilize the counter. So each time count =1 and therefore it will ask you for username over and over.

And maybe a do while( count < = 3) is better than " if "
Last edited on
And I put count ++; too. It should show me counts when username is wrong. It doesn't do that at all.
Topic archived. No new replies allowed.