Array help

This is a discussion on Array help within the C++ Programming forums, part of the General Programming Boards category; Basically, I guess I dont understand why you would need an array when you can just do everything individually which ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Array help

    Basically, I guess I dont understand why you would need an array when you can just do everything individually which makes it less complicated. I understand the concept of the array and how it holds variables or names of the same datatype but I dont know understand how to implement it into my program. We have to read from a file that the user inputs using filestreams (got that)...then we have to read each name in the file and create an array to store each name only once. We then have to fix the names in the string to have the first letter capitalized an the rest lower case (got that)...then we have to count the number of names (not repeated names) etc. Here is my code. Ok basically all it is doing right now is taking the names one by one and going into fixname and fixing them and putting them in my output file. I am guessing I have to put all of the names in an array- and then fix them all at once or something?? See I just dont understand. i have a code for an array I guess...but its not working (or i really dont know what it is supposed to do).

    Here is my code:

    Code:
     inputfile >> name;
       while(!inputfile.eof())
        {
          // countnames(names,size,inputfile);
          fixname(name);
          outputfile << name << endl;
        inputfile >> name;
        }
    
       return 0;
    }
    
    string fixname(string& name)
    {
      if (name[0] >= 'a' && name[0] <= 'z')
        {
          name[0] = toupper(name[0]);
        }
      for (int i = 1; i < name.length(); i++)
        {
          name[i] = tolower(name [i]);
        }
      return name;
    }
    
    void countnames(string names[MAX], int& size,ifstream& inputfile)
    {
      for (size = 0; size < MAX; size++)
        inputfile >> names[size];
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    to store each name only once
    You miss this. Your solution does not provide uniqueness of the name read...

    So the first loop should be something like:
    Code:
    while(there is names in the file)
    {
       read name
       if(name is not present in the array)
       {
          add name to array
          count++
       }
    }
    for each name in array
    {
       fix name
       output name
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Instead of outputting the strings to the file, just put them in the array. Keep a variable that remembers the current index in the array, and increment it each time you add a new one. Your countnames function pretty much does this, but you should probably add logic to check the filestream to make sure the inputfile is still valid after each read. If it is not valid (because of an error or end-of-file), then you would break the for loop and your size would hold how many strings were actually stored.

    Once you have the strings in your array, you can loop through the array and run fixname on each one and do any other processing you need to do.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Basically, I guess I dont understand why you would need an array when you can just do everything individually which makes it less complicated.
    Arrays can be processed in loops. Individual variables can't. And you may not be able to know, how much data you'll need to process, and how many individual variables you'd need for all that data.

    Which way would you prefer to program it?
    Code:
    //individual variables
    string a0, a1, a2, a3, a4, a5, ... , a999;
    process(a0);
    process(a1);
    process(a2);
    process(a3);
    process(a4);
    process(a5);
    ...
    process(a999);
    Code:
    //array
    const int size = 1000; 
    string array[size];
    for (int i = 0; i < size; i++) {
        process(array[i]);
    }

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    OK so then i would do:
    Code:
    outputfile << name[size] in my void countnames function?
    I see what you mean about running through the array to do all of the other functions I need but its the passing of the array I dont get and I am pretty sure my above statement is wrong.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> outputfile << name[size] in my void countnames function?
    Why? The countnames function is for counting names. The current code in there is actually doing the input into the array work, so that isn't exactly in the right place.

    You will have to run through the array several times in the program. A simple for loop that runs from = 0 to < size will do that. You can make any function take an array exactly as your countnames function does (except the inputfile parameter wouldn't be needed in most cases).

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    so my countnames function is taking each name from the inputfile and putting them in an array called names?

    do i need a seperate function for that? I guess I am confused as to how to do the looping through the array? Once everything is in the array- cant i just pass the array to each function? Ok here is a sample code from my class:

    Code:
    void getdate(string list [], int& count, ifstream& in)
    {
      count = 0;
      in >> list[count];
      while(!in.eof())
      {
       count++;
       in >> list[count];
       }
    }
    is this taking each individual name and then placing it in an array called list and counting it as its doing it?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> is this taking each individual name and then placing it in an array called list and counting it as its doing it?
    Yes. That looks fine for those two parts of the assignment (except for the function name not making any sense). Do you know how to call that function? Perhaps you should write the main() function if you haven't already and test just that code.

    >> I guess I am confused as to how to do the looping through the array?
    You've shown two different examples (the for loop and this while loop) that loop through the array. When looping through the array that already has the names input, just make sure you use the count to know when to stop looping.

    >> Once everything is in the array- cant i just pass the array to each function?
    Yes, exactly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21