how to use an array with an infile

im trying to use an array with data from my infile and the data is not

Page 1 of 1

10 Replies - 2933 Views - Last Post: 17 November 2009 - 06:46 PM Rate Topic: -----

#1 liteskin23   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-October 09

how to use an array with an infile

Posted 13 November 2009 - 11:40 PM

HERE IS MY CODE SO FAR CAN ANYONE HELP ME

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int SPECIES = 200;

int main()
{
    double species [200] ;   
    string filename;  // name of the input file                     
    ifstream inFile; //input file stream variable
    string cityName;
    string birdName;
    int birdCount;
    
    inFile.open("Project_4_Three.txt"); 
  
    if (!inFile) 
    {    
         cout << "bummer file name \n\n";
         system ("pause");
         return 1;
    }
           
cout << filename << "Project_4_Three " "file was opened." << endl<<endl; 
  
   cout << "City Name: ";
   getline(cin,cityName);
   cout <<endl;
     
   while (inFile)
   {
         inFile >> birdName;
         cout << " Name of Bird: " << birdName <<endl;
         inFile >> birdCount;
         cout << " # of Birds: " << birdCount << endl;
  

         
         
     }  
   
}



**********************************
Melfa, VA
Wood_Duck 8
American_Black_Duck 75
Bufflehead 135
Common_Goldeneye 2
Hooded_Merganser 10
Ruddy_Duck 1
Turkey_Vulture 2
Bald_Eagle 2
Red-tailed_Hawk 3
Ring-billed_Gull 10
Herring_Gull 10
Mourning_Dove 10
Belted_Kingfisher 1
American_Crow 15
Carolina_Chickadee 10
Tufted_Titmouse 6
Red-breasted_Nuthatch 3
Carolina_Wren 6
Gray_Catbird 3
Cedar_Waxwing 30
Dark-eyed_Junco 6
Pine_Siskin 2
American_Goldfinch 40



ITS SUPPOSE TO BE AN ARRAY OF STRUCTS BUT ITS NOT WORKING. THE PROGRAM COMPLIES BUT ITS NOT SENDING MY DATA OUT

*** MOD EDIT: Added code tags. Please :code: ***

Attached File(s)


This post has been edited by JackOfAllTrades: 14 November 2009 - 06:24 AM


Is This A Good Question/Topic? 0
  • +

Replies To: how to use an array with an infile

#2 seeP+   User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: how to use an array with an infile

Posted 13 November 2009 - 11:59 PM

Getting information from a file: getline(inFile, variablename);
Was This Post Helpful? 0
  • +
  • -

#3 Guest_c.user*


Reputation:

Re: how to use an array with an infile

Posted 14 November 2009 - 03:05 AM

	struct bird {
		string name;
		int number;
	} birds[100], *pb;

	pb = birds;
	pb->name = "duck";
	pb->number = 10;
	pb++;

	pb->name = "black duck";
	pb->number = 20;
	pb++;

	...


in the loop

This post has been edited by c.user: 14 November 2009 - 03:07 AM

Was This Post Helpful? 0

#4 liteskin23   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-October 09

Re: how to use an array with an infile

Posted 14 November 2009 - 09:49 AM

what is pb im just begining programming


here is what i have changed but it is still not reading my file

#include <iostream>
#include <fstream>
#include <iomanip>


using namespace std;

struct species_Type
{
       string cityName;
       int birdCount;
       string speciesName;
};

void printit (const int [],int );
void fill_it (int anArray [],int size );
const int SPECIES = 200;

int main()
{
       
    string inFile;                
    ifstream inData; //input file stream variable
    string cityName;
    string birdName;
    int birdCount;
    
    inData.open("Bird Data.txt"); 
  
    if (!inData) 
    {    
         cout << "file could not be opened... Try Again! \n\n";
         system ("pause");
         return 1;
    }

 cout << inFile << "Bird Data " "file was opened." << endl<<endl; 
   inData.open ("BirdData.txt");
   
       cout <<"City Name:" <<endl;
       cin>> cityName;
   
   while (!inData)
   {
         
         inData >> birdName; 
         cout <<"Name of Species: " <<endl;
         getline(cin, birdName);
         cin >> birdName;
         inData >> birdCount;
         cout <<"Bird Count: " <<endl;
         cin >> birdCount;
         break;
   }
       
   
}


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 14 November 2009 - 10:00 AM

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: how to use an array with an infile

Posted 14 November 2009 - 10:01 AM

Anywhere you see cin? You're not reading from the file.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_c.user*


Reputation:

Re: how to use an array with an infile

Posted 14 November 2009 - 09:28 PM

liteskin23 said:

im just begining programming

take a book for the programming, read it and then write the code
pb is a pointer to the bird in the array

This post has been edited by c.user: 14 November 2009 - 09:28 PM

Was This Post Helpful? 0

#7 liteskin23   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-October 09

Re: how to use an array with an infile

Posted 16 November 2009 - 02:43 PM

View Postc.user, on 14 Nov, 2009 - 08:28 PM, said:

liteskin23 said:

im just begining programming

take a book for the programming, read it and then write the code
pb is a pointer to the bird in the array



anyway i have the book and that is not in our book. like I said before I'm not using pointers. I know what pointers are and they are not required in this assignment. we are just suppose to put this information into an array of stucts
Was This Post Helpful? 0
  • +
  • -

#8 Guest_c.user*


Reputation:

Re: how to use an array with an infile

Posted 16 November 2009 - 08:24 PM

liteskin23 said:

I know what pointers are

ok, I removed them

Attached File(s)

  • Attached File  ducks.zip (856bytes)
    Number of downloads: 153

Was This Post Helpful? 0

#9 liteskin23   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-October 09

Re: how to use an array with an infile

Posted 17 November 2009 - 07:58 AM

View Postc.user, on 16 Nov, 2009 - 07:24 PM, said:

liteskin23 said:

I know what pointers are

ok, I removed them



the zip file wouldn't open
Was This Post Helpful? 0
  • +
  • -

#10 barnwillyb   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 108
  • Joined: 22-May 07

Re: how to use an array with an infile

Posted 17 November 2009 - 08:35 AM

View Postliteskin23, on 13 Nov, 2009 - 10:40 PM, said:

HERE IS MY CODE SO FAR CAN ANYONE HELP ME

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int SPECIES = 200;

int main()
{
    double species [200] ;   
    string filename;  // name of the input file                     
    ifstream inFile; //input file stream variable
    string cityName;
    string birdName;
    int birdCount;
    
    inFile.open("Project_4_Three.txt"); 
  
    if (!inFile) 
    {    
         cout << "bummer file name \n\n";
         system ("pause");
         return 1;
    }
           
cout << filename << "Project_4_Three " "file was opened." << endl<<endl; 
  
   cout << "City Name: ";
   getline(cin,cityName);
   cout <<endl;
     
   while (inFile)
   {
         inFile >> birdName;
         cout << " Name of Bird: " << birdName <<endl;
         inFile >> birdCount;
         cout << " # of Birds: " << birdCount << endl;
  

         
         
     }  
   
}



**********************************
Melfa, VA
Wood_Duck 8
American_Black_Duck 75
Bufflehead 135
Common_Goldeneye 2
Hooded_Merganser 10
Ruddy_Duck 1
Turkey_Vulture 2
Bald_Eagle 2
Red-tailed_Hawk 3
Ring-billed_Gull 10
Herring_Gull 10
Mourning_Dove 10
Belted_Kingfisher 1
American_Crow 15
Carolina_Chickadee 10
Tufted_Titmouse 6
Red-breasted_Nuthatch 3
Carolina_Wren 6
Gray_Catbird 3
Cedar_Waxwing 30
Dark-eyed_Junco 6
Pine_Siskin 2
American_Goldfinch 40



ITS SUPPOSE TO BE AN ARRAY OF STRUCTS BUT ITS NOT WORKING. THE PROGRAM COMPLIES BUT ITS NOT SENDING MY DATA OUT

*** MOD EDIT: Added code tags. Please :code: ***


Make sure that you tell your program where the file is: Folder Name separated by slash( / ).

/*__________________________________________________________*/
/*																													*/
/*												 includes													*/
/*__________________________________________________________*/

#include <iostream>
#include <iomanip>
#include <fstream>

// introduces namespace std
using namespace std;

/*__________________________________________________________*/
/*																													*/
/*						structures												*/
/*__________________________________________________________*/

struct Bird {
	string name;
	int number;
};

/*__________________________________________________________*/
/*																													*/
/*											 main listing												*/
/*__________________________________________________________*/

int main (int argc, char * const argv[]) {
	ifstream in;
	Bird ducks[100];
	long n;
	const char *filename = "/Developer/Applications/Mac OS X Books/C++ Programming/Ducks/ducks.txt";

	cout << endl;
	in.open(filename);
	if (!in.is_open()) {
		cerr << "error: file" << endl;
		return 1;
	}	
	
	n = 0;
	while (in >> ducks[n].name && in >> ducks[n].number)
		n++;
	
	for (int i = 0; i < n; i++)
		cout << ducks[i].name
		<< " "
		<< ducks[i].number
		<< endl;

	return 0;
}

This post has been edited by barnwillyb: 17 November 2009 - 08:37 AM

Was This Post Helpful? 0
  • +
  • -

#11 Guest_c.user*


Reputation:

Re: how to use an array with an infile

Posted 17 November 2009 - 06:46 PM

liteskin23 said:

the zip file wouldn't open

install zip, it's compatible with pkzip

add
this file can be unpacked by the built-in zip in WinXP

This post has been edited by c.user: 18 November 2009 - 04:04 PM

Was This Post Helpful? 0

Page 1 of 1