Solved

fstream variable  and skip string

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ to C# code conversion issue 4 132
Path to  STL Map header file 1 91
Error C2678: binary '!=': no operator found... 4 73
Error creating a new C++ project in ,net 20 59
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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

738 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