Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,158 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,115 people online right now. Registration is fast and FREE... Join Now!




Difficulty with array

 
Reply to this topicStart new topic

Difficulty with array, Due today!! Need the following code to also display the alphab

chasityc6
15 Dec, 2007 - 10:26 AM
Post #1

New D.I.C Head
*

Joined: 15 Dec, 2007
Posts: 2


My Contributions
CODE
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

// readData returns theStream, aNAme, aBDay, and anAge
void readData(ifstream& theStream, string& aName, string& aBDay, int& anAge);

// findAges pass in an int array of ages and int numInArray -
// returns leastAgeIndex and greatestAgeIndex
void findAges(int agesArray[], int numInArray, int& leastAgeIndex, int& greatestAgeIndex);

//create method to output the persons with the least and greatest age

void outputData(string namesArray[], string bdayArray[], int ageArray[], int leastAgeindex, int greatesAgeIndex);

int main(){

    // open the input file
    ifstream inStream;
    inStream.open("c:\\inputfile.txt");

    //declare variables to read from input file and declare arrays
    string name;
    string namesArray[100];
    string bday;
    string bdayArray[100];
    int age;
    int ageArray[100];

    // declare a counter to help loop through the array and keep track of number of elements
    // initialize to 0 to start at first element of the array
    int counter = 0;

    // deaclare variable to keep track of the index in arrays for least and greatest age
    int leastAgeIndex;
    int greatestAgeIndex;


    while (! inStream.eof())
    {

       // call the readData Function - it returns name, age, bday
       readData(inStream,name,bday,age);

       // store name, age, bday in the correct element of the array
       namesArray[counter] = name;
       bdayArray[counter] = bday;
       ageArray[counter] = age;
      
        // increment counter
        counter = counter +1;
    }
    
    // close input stream - we are finish reading
    inStream.close();

    // call findAges - returns leastAgeIndex and greatestAgeIndex
    findAges(ageArray,counter, leastAgeIndex,greatestAgeIndex);


    //output - to check that it is correct
    //cout << leastAgeIndex <<" " << greatestAgeIndex<< endl;

    outputData(namesArray,bdayArray,ageArray,leastAgeIndex,greatestAgeIndex);
}

void readData(ifstream& theStream, string& aName, string& aBDay, int& anAge)
{

    theStream >>aName >>aBDay >>anAge;

    

}

void findAges(int agesArray[], int numInArray, int& leastAgeIndex, int& greatestAgeIndex)
{
    int lowestAge = 1000;
    int greatestAge = 0;

    for (int i = 0; i < numInArray; i=i+1)
    {

        if (agesArray[i] < lowestAge)
        {
            leastAgeIndex = i;
            lowestAge = agesArray[i];
        }
        if (agesArray[i] > greatestAge)
        {
            greatestAgeIndex = i;
            greatestAge = agesArray[i];
        }

    }

    

}

void outputData(string namesArray[], string bdayArray[], int ageArray[], int leastAgeIndex, int greatestAgeIndex)
{
  cout<<"The person with the least age is " << namesArray[leastAgeIndex]<<endl;
  cout<<"The person with the greatest age is " << namesArray[greatestAgeIndex]<<endl;
}


This post has been edited by jayman9: 15 Dec, 2007 - 02:30 PM


Attached File(s)
Attached File  inputfile.txt ( 55bytes ) Number of downloads: 28
User is offlineProfile CardPM
+Quote Post

curiose
RE: Difficulty With Array
15 Dec, 2007 - 01:14 PM
Post #2

D.I.C Head
**

Joined: 28 Oct, 2007
Posts: 94


My Contributions
I would like to help you but i havent understand where is ur week point in this program??? it would be better if u identify it in much clear way
User is offlineProfile CardPM
+Quote Post

chasityc6
RE: Difficulty With Array
15 Dec, 2007 - 03:51 PM
Post #3

New D.I.C Head
*

Joined: 15 Dec, 2007
Posts: 2


My Contributions
This program is due by midnight and I am completely stuck. The program is to contain a minimum of 5 functions and a main function.

1. The Function ReadData should read a file containing the following information; it should read until there is no more data in the file

your name (must be declared as a variable)
your age (must be declared as a variable)
your birthday (must be declared as a variable)
2. Function Store Places data into an Array
3. Function FindAges should find both the youngest and oldest in the array
4. Function FindNames should find alphabetically first and alphabetically last names in the array.
5. Function OutputData should write the youngest and oldest in the array and all of their information; alphabetically first and alphabetically last names in the array and all of their information.

I have attached a text file that this information pulls from.


Attached File(s)
Attached File  inputfile.txt ( 37bytes ) Number of downloads: 19
User is offlineProfile CardPM
+Quote Post

curiose
RE: Difficulty With Array
16 Dec, 2007 - 01:54 AM
Post #4

D.I.C Head
**

Joined: 28 Oct, 2007
Posts: 94


My Contributions
Sometimes with the arrays u r not identifiying them correctly....i mean when u want to have a charecter array u have to intialize its type,and num. of arrays fields or u have to write them after but here u r taking them from the file so u can also use functions inside the main to call as u did
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 11:31PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month