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!
//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>
usingnamespace 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();
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..
#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>
usingnamespace 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();
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();
}
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();
}