Help with my search function

I am trying to have a user enter his/her name. The program should then search the .txt file to see if that name exists. It is then supposed to input the name to the file. My program does not do anything after the user enters their name. Where did I go wrong???

Also, I would like it to only input the name if it doesn't exist already, but I'm not worried as much about that part.

I'm only including the first part of my code as everything else works correctly. But, it is followed up with the game of War!

Thanks for any help!

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
  //System Libraries
#include <iostream>//I/O Library
#include <iomanip>//IO Manipulators
#include <cstdlib>//C standard library
#include <ctime>//C Time library
#include <string>//string library
#include <cctype>
#include <fstream>
using namespace std;//Namespace for iostream

//User Libraries

//Global constants

//Function Prototypes
void header();
void fnlSCR(int, int, string);
int srchUSR(string, char);

//Execution Begins Here!
int main(int argc, char** argv){
    srand((unsigned)time(0));
    
    //Declare Variables Here
    int comp=0;//computer card
    int plyr=0;//player card
    int compTTL=26;//computer score
    int plyrTTL=26;//player score
    int war=1;//add to scores during war
    char ans;//deal next hand?
    string name;//Player enters name
    int offset;
    string line;
    
    //Prompt user to enter name in order to begin game
    header();
    cout<<"Please enter your unique user name (no spaces): \n";
    cin>>name;
    
    ifstream inputFile;
    inputFile.open("users.txt");
    cin>>name;
    
    if (inputFile.is_open())
    {
        while (!inputFile.eof())
        {
            getline(inputFile,line);
            if ((offset=line.find(name, 0))!=string::npos)
            {
                cout<<"Welcome Back!\n";
            }else
            {
                cout<<"First time player! Welcome!\n";
            }
        }
        inputFile.close();
    }
    
    ofstream outputFile;
    outputFile.open("users.txt",ios::out|ios::app);
    outputFile<<name<<"\n";
    outputFile.close();
    
Lines 38 and 42. Why?
OMG! I should have been able to catch that.. THANK YOU!

Any chance anyone can help me make it so "Welcome Back" or "First time player! Welcome!" only displays once? Right now it displays "welcome back" as many times as the name appears in the file.. and it shows "First time player! Welcome!" for every line it searches.. so if there are four names already in the file, it displays "first time player" 4 times..

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
#include <iostream>//I/O Library
#include <iomanip>//IO Manipulators
#include <cstdlib>//C standard library
#include <ctime>//C Time library
#include <string>//string library
#include <cctype>
#include <fstream>
using namespace std;//Namespace for iostream

//User Libraries

//Global constants

//Function Prototypes
void header();
void fnlSCR(int, int, string);
int srchUSR(string, char);

//Execution Begins Here!
int main(int argc, char** argv){
    srand((unsigned)time(0));
    
    //Declare Variables Here
    int comp=0;//computer card
    int plyr=0;//player card
    int compTTL=26;//computer score
    int plyrTTL=26;//player score
    int war=1;//add to scores during war
    char ans;//deal next hand?
    string name;//Player enters name
    int offset;
    string line;
    
    //Prompt user to enter name in order to begin game
    header();
    cout<<"Please enter your unique user name (no spaces): \n";
    cin>>name;
    
    ifstream inputFile;
    inputFile.open("users.txt");
    
    if (inputFile.is_open())
    {
        while (!inputFile.eof())
        {
            getline(inputFile,line);
            if ((offset=line.find(name, 0))!=string::npos)
            {
                cout<<"Welcome Back!\n";
            }else
            {
                cout<<"First time player! Welcome!\n";
            }
        }
        inputFile.close();
    }
    
    ofstream outputFile;
    outputFile.open("users.txt",ios::out|ios::app);
    outputFile<<name<<"\n";
    outputFile.close();
Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

if (inputFile.is_open())
    {
        while (!inputFile.eof())
        {
            int flag =1;
            getline(inputFile,line);
            if ((offset=line.find(name, 0))!=string::npos) flag=0;
        }
            if(!flag) cout<<"Welcome Back!\n";
            else      cout<<"First time player! Welcome!\n";
        inputFile.close();
    }
THANK YOU! THANK YOU! THANK YOU! I wish they had a tipping option!
Just a question can two users have the same username? if not then this will save you a lot of time:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

if (inputFile.is_open())
    {
        while (!inputFile.eof())
        {
            int flag =1;
            getline(inputFile,line);
            if ((offset=line.find(name, 0))!=string::npos)  { flag=0; break; } //changed here
        }
            if(!flag) cout<<"Welcome Back!\n";
            else      cout<<"First time player! Welcome!\n";
        inputFile.close();
    }
Topic archived. No new replies allowed.