ifstream problem

i have this problem- since i need to get my input from a "*.in" file using ifsream.,
is there's an easy way to get input for example-

this is input.in
2
3
wallmart city, cotton city, violete city
3 2 4
2
big mack, mcdonalds
3

i need to get
the first line as # of cases
second line as # of towns
and i need the town to be placed in a variable such that town[0]= wallmart city
town[1]=cotton city town[2] = violete city
and continue to fetch the last line
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

int main() {
	
	int n, c;
	std::vector<string> city;
	
	ifstream file("data.in");
	string cityname;
	
	for (file >> n; n--; )
	{
		for (file >> c; c-- > 1; )
		{
			getline(file, cityname, ',');
			city.push_back(cityname);
		}
		
		getline(file, cityname);
		city.push_back(cityname);
		
		/* etc etc... */
		
	}
	return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using namespace;
int main()
{
	ifstream input;
	input.open("d.in");
	int num, way, towns; string town,t[10];
	input>>num;//as # of cases;
	for(int x=0;x<num;x++)
	{
		input>>towns;//# of towns
		getline(cin,town);getline(cin,town); //need to double for pointer to be on the right line
		for(int y=0;y<towns;y++)
		{
			t[y]=town.substr(0,town.find(';'));
			town=town.substr(t[y].length()-1,town.length()-1);
			cout<<t[y]<<endl;
		}
		for(int z=0;z<towns-1;z++)//this loop to get the roads
		{
			input>>way;
		}
		cout<<endl;
	}
	input.close();
	return 0;	
}




actually i already started like this one and still have errors, might as well suggest an easier way
Last edited on
Topic archived. No new replies allowed.