code help



We are asked by our professor to make an c++ file that contains an array, function, class,loop and a username and password thing at the beginning.
I know guys its to easy, but my professor is not good at teaching this c++ thingy.
I already read a book but it needs more explanation. please guys help me.

i can't finish the code below -___-
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
  #include<iostream>
#include<windows.h>
#include<fstream>
#include<string>
using namespace std;


int main()
{       string password;
        string username;
		string answer;

		c:
             cout <<"Enter Username : ";
             cin >> username;
			     if (username == "charlton")
{			 cout <<"Enter Password : ";
}				 else
	         cout << "INVALID USERNAME!";
             cout << "\nACCESS DENIED" ;
			    Sleep(2000);
				system("cls");
				goto x;


			 cin >> password;
			     if (password == "1234")
{					 cout << " ACCESS GRANTED!";
				 Sleep(2000);
				 system("cls");
}
				 else {
					cout << "INVALID PASSWORD";
					cout << "\nACCESS DENIED" ;
				 
				 }

x:
			{	 cout<< " try again? (y/n)";
			}	 if  (answer == "y")
                 	 goto c;
				 if  (answer == "n")
						exit(0);
					 

}
Last edited on
At line 18, the else condition should have braces { } around the code for that condition.

At line 39, the user is asked "try again?" but there is no cin to get the user's response.

Generally, the use of goto should be avoided as it encourages the use of unstructured and chaotic code which is difficult to read or maintain. Normally the code can be replaced with some sort of loop, such as a while or do-while loop with a suitable condition, for example:
1
2
3
4
    do
    {
        // your code here
    } while (answer == "y" || answer == "Y");
thanks chervil
but how to put some classes and function there?
just a simple class and function that asks you to enter something and just save it to a text file?
I recommend you look at the relevant pages on the tutorial regarding functions, file I/O and classes. http://www.cplusplus.com/doc/tutorial/
Depending on how much of a grasp you have of other topics, the entire tutorial may be useful too, but you can just focus on the parts you need for now.

After you write some more code, feel free to post it and ask for further help. As it is, in my opinion, the questions you have seem so open-ended it's almost like "how long is a piece of string".

Those are just my thoughts, others may see things differently.
sorry man. ahaha
btw thanks alot !!
Topic archived. No new replies allowed.