Getting input from txt file with tabs in it

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:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()

ifstream input("Babynameranking2001.txt");

//Read data
int ranking;
string malename;
int amountofmales;
string femalename;
int amountoffemales;

input >> ranking >> malename >> amountofmales >> femalename >> amountoffemales;
cout << malename << " " << amountofmales << " " << femalename << " " << amountoffemales << endl;


cout << "Done" << endl;

return 0;


Sorry for how sloppy it is.

Anyways when I run the program I wind up getting an output of

-858993460 -858993460

I'm thinking that because the text file is formatted like this(just an example):

1 Jacob 32509 Emily 25048
2 Michael 29666 Madison 22149

(the spaces between everything are actually tabbed over)

that it's the large amount of whitespace messing it up. Any help would be appreciated. And again I'm new to the forum so plz excuse my bad formatting.
Last edited on
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".

Yes, as jlb says try to check if you really open your file with input.is_open()
Your code works with your sample input:
Jacob 32509 Emily 25048

Process returned 0 (0x0)   execution time : 0.005 s
Press any key to continue.
Thanks guys apparently I had it saved in a location that it didn't like
I have another quick question. I'm trying to get it to where the user inputs the year of a file they want to open and it opens that file

Ex: Enter a year: 2002
and it opens Babynameranking2002.txt

I'm planning on using the following

int year;
cout << "Enter the year: ";
cin >> year;

ifstream input;

if(year == 2001)
ifstream input("Babynameranking2001.txt");
if(year == 2002)
ifstream input("Babynameranking2002.txt");
if(year == 2003)
ifstream input("Babynameranking2003.txt");
if(year == 2004)
ifstream input("Babynameranking2004.txt");
if(year == 2005)
ifstream input("Babynameranking2005.txt");
if(year == 2006)
ifstream input("Babynameranking2006.txt");
if(year == 2007)
ifstream input("Babynameranking2007.txt");
if(year == 2008)
ifstream input("Babynameranking2008.txt");
if(year == 2009)
ifstream input("Babynameranking2009.txt");
if(year == 2010)
ifstream input("Babynameranking2010.txt");

it seems like it isn't opening the files correctly. I was thinking it was the original ifstream input; messing it up
std::string input_filename("Babynameranking");
input_filename += std::to_string(year) + ".txt";

Your code shouldn't even compile.
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.

   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 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.

once again thank you for all your help. I managed to get it working :)
Topic archived. No new replies allowed.