Solved

fstream variable  and skip string

Posted on 2011-03-14
1
367 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

777 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