Solved

fstream variable  and skip string

Posted on 2011-03-14
1
363 Views
Last Modified: 2012-05-11
Hi,
in a very SIMPLE way,
how can I skip the name and only enter the age into my array?
I would like to skip the names without creating an array for them.
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main(){
	fstream f;
	 string age[2];

	f.open("data.txt");
	for(int i=0; i<2; ++i)
		f>>age[i];
	f.close();
return 0;
}

Open in new window

data.txt
0
Comment
Question by:pgmerLA
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 35131068
By reading the name into a dummy variable that you only use for skipping them, e.g.
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main(){
	fstream f;
	 string age[2];
         string dummy;

	f.open("data.txt");
	for(int i=0; i<2; ++i)
                f >> dummy; // skip the 1st field
		f>>age[i]; // store the 2nd one
	f.close();
return 0;
}

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now