Vectors beginner problem ..


Hi , i'm using vectors for the first time in a project , my project about Bnking system , my idea is the user enter a name and the program will search for it in the vector same as the password , How can code this ?
I try and this what i have so far . a lot of red lines in the code and i dont know why and who to fix it .
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
 #include<conio.h>
#include <vector >
#include<string.h>
using namespace std ;

int main () { 
	int index;
	vector<string>names;
	vector<int>passwords;
	vector<int>account;
	vector<string>::const_iterator i;
	names.push_back("mohammad");names.push_back("Khaled");names.push_back("Ahmmad");
	passwords.push_back(3421);passwords.push_back(1416);passwords.push_back(2776);
	account.push_back(800);account.push_back(1000000);account.push_back(5000);
	char answr;
	cout<<" Do you have account ? y\n";
	cin>>answr;
	if ( answr =='y'||'Y')
		{
			cout<<"Enter your name : "<<endl;
	char name;
		cin>>name;
		for( i=names.begin();i!=names.end();i++)
		{
				if (names at.(i)==name)//

	
	 index=i;
	
		}
		while(1)
		{
			char password ; 
		cout<<"ENTER YOUR PASSOWRD :\n";
		cin>>password;
		
		if(password==passwords at.[index])//
			{cout<<"loge in is completed";
		break;}
		else
			cout<<"the name dosn't match the password ,try again";
		}
21
22
char name;
cin>>name;

Is there any reason why you're asking the user to input only a single char as the name, when your vector contains strings?
Last edited on
no , i want the user to enter the name as string .
then why have you declared your name to be a char?
use a std::string with getline:
http://www.cplusplus.com/reference/string/string/getline/
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
#include <iostream>
#include <string>
#include <vector>
#include <cassert>

int main()
{
    const std::vector<std::string> names { "mohammad", "khaled", "ahmmad" } ;
    const std::vector<int> passwords { 3421, 1416, 2776 } ;
    assert( passwords.size() == names.size() ) ;

    std::cout << "name? " ;
    std::string user_name ;
    std::getline( std::cin, user_name ) ;

    std::cout << "password? " ;
    int pword ;
    std::cin >> pword ;

    // find the position corresponding to the name
    std::size_t pos ;
    for( pos = 0 ; pos < names.size() && names[pos] != user_name ; ++pos ) ;

    // if the  name was found, pos would be less than names.size()
    if( pos < names.size() && passwords[pos] == pword ) std::cout << "login successful\n" ;
    else std::cout << "invalid name/password\n" ;
}
JLBorges thank you it helps a lot , but when a run the code it dosn't work .
> when a run the code it dosn't work .

Are you able to compile the code without errors?
Which compiler and version are you using?
yeah i found errors and c corrected it , it worked fine but when i enter the name and the password every time it give me the else statement.
I'm using Microsoft Visual 2010 Express


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
#include <iostream>
#include <string>
#include <vector>
#include <cassert>

int main()
{
    std::vector<std::string> names ;
    names.push_back( "mohammad" ) ; names.push_back( "khaled" ) ; names.push_back( "ahmmad" ) ;

    std::vector<int> passwords ;
    passwords.push_back(3421) ; passwords.push_back(1416) ; passwords.push_back(2776) ;
    assert( passwords.size() == names.size() ) ;

    for( std::size_t i = 0 ; i < names.size() ; ++i )
        std::cout << i << ". " << names[i] << ' ' << passwords[i] << '\n' ;

    // std::cout << "name? " ;
    std::string user_name ;
    user_name = "khaled" ; // std::getline( std::cin, user_name ) ;

    // std::cout << "password? " ;
    int pword ;
    // std::cin >> pword ;
    pword = 1416 ;

    std::cout << "\nsearching for user name '" << user_name << "' with password " << pword << '\n' ;

    // find the position corresponding to the name
    std::size_t pos ;
    for( pos = 0 ; pos < names.size() && names[pos] != user_name ; ++pos ) ;
    if( pos < names.size() ) std::cout << "user name '" << user_name << "' was found at position " <<  pos
                                       << "\npassords[" << pos << "] is " << passwords[pos] << "\n\n" ;
    // if the  name was found, pos would be less than names.size()
    if( pos < names.size() && passwords[pos] == pword ) std::cout << "login successful\n" ;
    else std::cout << "invalid name/password\n" ;
}

0. mohammad 3421
1. khaled 1416
2. ahmmad 2776

searching for user name 'khaled' with password 1416
user name 'khaled' was found at position 1
passords[1] is 1416

login successful

http://coliru.stacked-crooked.com/a/c9327ab2d2c5df82
http://rextester.com/YRJXK84464
IT WORKED , THANK YOU
> I'm using Microsoft Visual 2010 Express

Switch to a current release of the compiler:
https://www.visualstudio.com/products/visual-studio-community-vs

Make sure that you install the C++ toolchain:
http://www.cplusplus.com/forum/beginner/175111/#msg866895
Topic archived. No new replies allowed.