array issue from openfile

Jun 17, 2016 at 10:33pm
Hi everyone. I writing a program that opens the file "plants.txt" ( see below) and then reads "plants.txt" into two arrays(name and price). The file is then sorted from lowest to highest in price.

I have 3 errors on program in the second while loop( see below) with comments for more detail.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
  #include <iostream>
#include <string>
#include <fstream>
#include <iomanip>;
using namespace std;


int main ()

{
	string name; //declaring variables
	int price;
	ifstream theFile("plants.txt"); // filename
	
	while(theFile >> name >> price) // opens the file
	{ cout << name<< price << endl;	
	}
	
	int ArrayOne[58]; // defining array size
	int ArrayTwo[58];
	int i, size = 0;
	 
	while (!feof("plants.txt")) /* First error. "plants.txt" Error: argument of type "const Char" is incompatiable with parameter of type "FILe" so i am not sure what to replace "plants.txt" with*/
	{
		fscanf("plants.txt", " %d %d", &name, &price); // same issue as the one above with "plants.txt"
		ArrayOne[size] = name; // FINAL error: no suitable conversion function from "std::string" to int exists
		ArrayTwo[size] = price;
		size++;
	}

	for (i = 0; i<size; i++) {  // sorts the data
		printf("ArrayOne[%d] = %d,  ArrayTwo[%d] = %d\n",
			i, ArrayOne[i], i, ArrayTwo[i]);
	}
	
	
	system ("pause");
	return (0);

}


Here is what the plants.txt file contains for a better understanding of what i am trying to accomplish.
Fist column is name, second is price.
bigleaf_maple 10
boxelder 5
red_maple 3
silver_maple 100
sugar_maple 54
common_yarrow 87
western_yarrow 2
Indian_ricegrass 6
Lemmon's_needlegrass 99
Columbia_needlegrass 110
western_needlegrass 22
desert_needlegrass 33
Thurber's_needlegrass 11
Maui_chaff_flower 55
purple_giant_hyssop 88
bigflower_agoseris 99
annual_agoseris 88
crested_wheatgrass 77
desert_wheatgrass 66
Siberian_wheatgrass 15
spike_bentgrass 15
redtop 51
American_water_plantain 3
twincrest_onion 2
glassy_onion 16
oneleaf_onion 17
European_alder 71
gray_alder 22
speckled_alder 101
groundnut 111
Indianhemp 102
red_columbine 3
Pacific_madrone 5
common_bearpoppy 6
hairy_manzanita 2
pinemat_manzanita 1
greenleaf_manzanita 66
kinnikinnick 88
whiteleaf_manzanita 55
bluestem_pricklypoppy 12
Canadian_wildginger 21
heartleaf_milkweed 4
whorled_milkweed 2
swamp_milkweed 3
Mead's_milkweed 85
showy_milkweed 58
common_milkweed 36
butterfly_milkweed 22
woolly_milkweed 55
green_antelopehorn 101
Welsh's_milkweed 110
Paradox_milkvetch 112
Mancos_milkvetch 102
Monti's_milkvetch 32
Kremmling_milkvetch 21
tubercled_saltbush 12
fourwing_saltbush 16
shadscale_saltbush 55

I am a complete noob and cannot figure this out so Thanks for taking the time to reading this post. If anyone needs clarification let me know!

Last edited on Jun 17, 2016 at 10:34pm
Jun 18, 2016 at 2:47am
Hi,

while(theFile >> name >> price) // opens the file

This code doesn't open your file

but

theFile.open("plants.txt");

will open

HOPE IT HELPS
Topic archived. No new replies allowed.