Solved

fstream variable  and skip string

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

830 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