Hi guys this is my first time posting something so if you need anything else let me know. Currently I'm trying to write a program that searches a file named "Babynameranking.txt" for a persons name, sex, and ranking. I'm currently having trouble with just being able to search the first line of the text file, which is somewhat of a precursor to being able to finish the program. My current code is:
Are you sure your file is opening correctly. You should always check for a successful opening. As long as there are no embedded spaces in your names your input line should work The tab character is considered a white space character that cin normally will "ignore".
When you declare a variable in a control structure block it is only visible inside that block:, remember if you don't use braces with your control structures this block is one line.
1 2 3 4 5 6
if(condition)
{
int a = 1000; // this variable is only defined within this block.
}
cout << a << endl; // Error a is not defined in this context.
The same holds true with your ifstream instances, they only exist inside the if statements. The instance that you created outside the block is not affected. Meaning it is never used. Instead of creating new instances of the ifstream class in each if statement you could use the ifstream.open() function to open the file with the existing ifstream instance.
However I would recommend you try to implement MiiNiPaa's method above. Also unless you are using a C++11 compliant compiler you will need to convert the std::string to a C-string when you create the instance of the ifstream class.