-
File is not read!
Hi,
I am totally new to this programming stuff, and requesting u all ,to help me out in this .........
My Q is I have to take valid input FIle from the user ,and open it,until user presses -1 to exit........ or until he/she doesnt type the valid name (which in my case is "records.txt")......
My code is working ........but the problem is if I type valid name ,it get stucked.........
Code:
int main()
{
int number = 0;
string file = " ";
ifstream inputFile("records.txt", ios::in );
cout<<"Enter the file (-1 to end)"<<endl;
file=readLine();
while(file !="-1")
{
if(file != "records.txt" )
{
cout<<"File doesnt exist"<<endl;
inputFile.clear();
cout<<"Please re-enter"<<endl;
file=readLine();
}
}
if (file == "-1")
{
exit(0);
}
if (file== "records.txt")
{
inputFile.open("records.txt".c_str(), ios::in);
adatabase(database,number);// populating the database//
displayMainMenu();
getMenuOption(database,number);
}
return 0;
}
string readLine()
{
char buffer[SIZE];
cin.getline(buffer, SIZE, '\n');
return buffer;
}
-
I can't be volumes of help to you, as I could not get this to compile (I'm very new too). But it may get more of a response if you list your compiler and your include files.
Also, even though you have code in your program to make sure the user enters the correct file name, you do not appear to be making sure that the file can be opened (I don't know, the function you use may do that automatically).
Also, since you have a number of statements under your test to see that the user enters the correct file name, perhaps you could put a print statement of some kind after each one to see exactly where your program is getting stuck (if you haven't already tried that). If you have tried that you could reply with exactly what line of code was giving you issues
-
Okay... how 'bout tryin' this Bird:
Replace:
file != "-1" or file == "records.txt"
with:
strcmp(file, "-1") != 0 or strcmp(file, "records.txt") == 0
respectively.
strcmp(char *s1, char *s2) returns:
< 0 if s1 < s2
> 0 if s1 > s2
== 0 if s1 == s2