Problem i need help with...

Ok so i'm trying to compare a string to a certain string in an array, my code is as follows:


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
#include <iostream>
 using namespace std;
 
 int tu;
 int nu=0;
 int login();
 int Register();
 string input;
 string usr;
 string pass;
 
 int main(){
  if(nu>=1){
   cout<<"Enter 1 to login or 2 to register: ";
   cin>>input;
   if(input=="1"){
    login(); 
   }else if(input=="2"){
   Register(); 
   }else{
   cout<<"Enter 2 to register: ";
   cin>>input;
  if(input=="2"){
   Register();
  }else{
   main();
  }}}}
 
 
 int Register(){
  ++nu;
  string nu[2];
  cout<<endl<<"Enter username now: \t";
  cin>>nu[0];
  cout<<"Enter password now: \t";
  cin>>nu[1];
  cout<<"Returning to menu... \n";
  main();
 }
 
 
 int login(){
  cout<<"\n Please enter username: ";
  cin>>usr;
  cout<<"\n Please enter password: ";
  cin>>pass;
  
   for(tu=1;tu<=nu;tu++){
    if(usr==tu[0]){
     if(pass==tu[1]){
      cout<<"\n Correct!\n";
      main();
      break;
     }else{
      cout<<"";
     }
    }else{
     cout<<"";
    }
   }
  main();
  
 }
 



when i try to compile it i get this error:


1
2
3
4
5
6
7
main.cpp: In function 'int login()':                                                                                                
main.cpp:49:17: error: invalid types 'int[int]' for array subscript                                                                 
     if(usr==tu[0]){                                                                                                                
                 ^                                                                                                                  
main.cpp:50:19: error: invalid types 'int[int]' for array subscript                                                                 
      if(pass==tu[1]){  


So anyone know how to fix this?
You defined tu as an int, so it's not an array!
Do you know how i can fix that @BasV? How i imagine it, the program would compare usr to (for example) 1[0].
Do you know how i can fix that

I can't tell from your total lack of comments and meaningless variables names what tu is supposed to be.

Lines 26, 38, 52, 61: It is NOT legal to call main().

Line 31-32: Very poor style to use the same variable name for global and local variables.

Line 37: Since nu[] is a local variable, it goes out of scope when Register() exits. What is the point of asking for the user name and password, if these variables go out of scope?

Lines 49-50: You're trying to compare strings to an int (or int array). Whichever you intended, these are not types that can be compared.



Sorry, i'm not used to having others debug my code, I'm trying to get ahead of myself here with using this program I think.
Did not mean to sound harsh. You did post in a public forum asking strangers to read and understand your code.

I see lots of login programs posted on this forum with varing degrees of sophistication. Since you did not post a description of what your login program is supposed to do, so I can only guess.
Topic archived. No new replies allowed.