Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

fstream variable  and skip string

Posted on 2011-03-14
1
Medium Priority
?
378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.

705 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