reading through an array

This is a discussion on reading through an array within the C++ Programming forums, part of the General Programming Boards category; Hey again- This time I am using filestream to read in from a file the user inputs. I want to ...

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

    reading through an array

    Hey again-

    This time I am using filestream to read in from a file the user inputs. I want to read the name, see if its already in the array and if it is- disregard it and read in the next name- and if it isnt then I want to send it to fixname so it makes the first letter capitalized and the rest lower case. I think i have the right idea but its just written wrong. I want to read each name in individually from the file and put it into temp. Then compare the name in temp to each name in the array to see if its already there. I have it reading in and then bubblesorting ok- but its not doing the comparing and then fixname. Its bubblesorting the messed up names- but i need to see if the name is not in the array already- fix it and then store it. I have tried different ways but there is something i am not seeing. here is my code:

    Code:
    void countnames(string list [], int& count, ifstream& inputfile, ofstream& outputfile)
    {
      string temp;
      count = 0;
      inputfile >> list[count];
       while(!inputfile.eof())
         {
           for(int i = 0; i < count; i++)
           {
             if (list[count] != temp)
              {
               temp = fixname(temp);
               count++;
              }
             else if (list[count] == temp)
              {
               inputfile >> temp;
              }
             }
        inputfile >> list[count];
        }
    }
    i know in this code i have it reading the word into the array- but when i change it temp- it prints blank. this way at least the array prints out. thanks for your help!

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    You are using temp not initialized
    You should not enter values directly to the array
    You are checking the temp with list[count] not list[i]
    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
    Nov 2006
    Posts
    85

    more help!!!

    I know this function looks messy- but i have tried sooo many ways to fix this and I cant. I am pretty sure my for loop is the one thats messed up but i cant figure out how to fix it. Its the same thing as the earlier post but i have it working better now. I have 8 names in datafile, but some are repeats. Example:
    Code:
    jEnna
    PArrish
    KURt
    JoYce
    whitNEY
    Jenna
    whitney
    KURT
    I want it to read the name into temp- compare it with the names in the array and if it is not in the array- send it to fix name (which it is doiing with all of them) and then put it in the next space in my array(list). If it is already in the array then i just discard it and move on to the next one. I think i have the for loop and maybe the if stmt messed up. I am going crazy.

    Code:
    void countnames(string list [], int& count, ifstream& inputfile, ofstream& outputfile)
    {
      string temp;
      count = 0;
      inputfile >> temp;
       while(!inputfile.eof())
         {
          for(int i = 0; i < MAX; i++)
           {
            if (temp != list[count])
             {
              temp = fixname(temp);
              count++;
              list[count] = temp;
             }
            else if (temp == list[count])
             {
              inputfile >> temp;
             }
            inputfile >> temp;
           }
          inputfile >> temp;
        }
       outputfile << "# of unique names: " << count << endl;
    Here is what it is outputting:
    Code:
    Jenna
    Joyce
    Kurt
    Parrish
    Whitney
    Jenna
    Whitney
    Help!

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    look at this loop
    for(int i = 0; i < MAX; i++)
    what do you want to do? check if the string exists... but inside the loop the i is not used...
    maybe you should write a function
    isNameFound()
    that will get an array and a new name as a parameter and return true if the name is already present in the array

    when you write this function your reading loop will be easier to implement
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Vart you did help- but I am having trouble still with it not comparing the strings. I have been working on this forever- and i just dont understand where i am going wrong. Here it is- please someone help!

    Code:
    void countnames(string list [], int& count, ifstream& inputfile, ofstream& outputfile)
    {
      int flag;
      string temp;
      count = 0;
      inputfile >> temp;
       while(!inputfile.eof())
         {
           if(count <= MAX)
            {
             temp = fixname(temp);
             isnamefound(list, temp);
             if (isnamefound(list,temp))
               {
                list[count] = temp;
                count++;
               }
              else
               {
                inputfile >> temp;
               }
            }
         inputfile >> temp;
         }
       outputfile << "# of unique names: " << count << endl;
    }
     
    bool isnamefound(string list[], string temp)
    {
      bool flag;
      for (int i =0; i < MAX; i++)
       if (temp != list[i])
         {
           flag = 0;
         }
       else
         {
           flag = 1;
         }
      if (flag ==0)
        return true;
      else
        return false;
    }
    one of my TA's told me to do the flag thing- but its giving me the same output as i had before.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    In your search function you're making to many unnesasery operations - kip it simple
    Code:
    bool isnamefound(string list[], int current_length, string newString)
    {
    	for (int i =0; i < current_length; i++) //no need to look the items that are not there yet
    	{
    		if (newString == list[i]) //OK the match is found
    		{
    			return true; //no need to continue search - we already know the answer
    		}
    	}
    	return false; //got here? - it means nothing appropriate was found
    }
    Try to look on your reading loop and simplify it.
    On every line - add a comment explaining why are you doing this operation
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    all of a sudden my bubble sort isn't working....see anything wrong?

    Code:
    void sortnames(ifstream& in, ofstream& out, string list[], int count, int& count2)                                                                                                                  
    {
      string temp;
      for (int i=0; i < count-1; i++)
        {
          for (int j=0; j < count-(i+1); j++)
            {
              if(list[j] > list[j+1])
                {
                  temp = list[j];
                  list[j] = list[j+1];
                  list[j+1] = temp;
                }
            }
        }

  8. #8
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    CMIIW, can a string be compared with less than (<) or more than (>)? I think what you want is

    Code:
    ...
          if (atoi(list[j].c_str()) > atoi(list[j+1].c_str()))
    ...
    or

    Code:
    ...
          if (list[j].size()) > list[j+1].size())
    ...
    Sorry can only understand your code vaguely. My head is full right now.
    Last edited by g4j31a5; 11-29-2006 at 02:16 AM.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    atoi("Whitney") == 0 so this comparison (atoi(list[j].c_str()) > atoi(list[j+1].c_str())) makes no sence
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    all of a sudden my bubble sort isn't working....see anything wrong?
    Nope, except for a missing closing brace.

    If it previously worked, may-be the problem lies with how you call it - passing incorrect parameters. The function also seems to take too many irrelevant parameters. What are the filestreams and count2 passed for? Each function should do some well-defined action. A sort function shouldn't do any file input/output, just the sorting.

    By the way, the countnames function looks particularly messy. Why so many inputfile >> temp; statements? It looks to me that it should be skipping some names under some conditions altogether (many file inputs without checking temp inbetween). Also, as it is not good to use eof() to control the loop, you might do it so:
    Code:
    //initialise relevant variables
    while (inputfile >> temp) { //read file only here
        //decide if the name already exists
        //if not, add to the array, else just ignore temp
    }

  11. #11
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by vart
    atoi("Whitney") == 0 so this comparison (atoi(list[j].c_str()) > atoi(list[j+1].c_str())) makes no sence
    That's why I said my head is full. Confused with alottabugsandtweaks right now.

    BTW, can a string be compared with "<" or ">"? Never use them before.

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can see for yourself in a reference: string operators

    And forget about the atoi thing, you are not converting anything to int. Character arrays/strings can be compared, because each character is really a numeric value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data into a 2D array
    By swgh in forum C Programming
    Replies: 2
    Last Post: 08-17-2007, 03:07 AM
  2. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Reading a mixed Text and decimal file into an array
    By djamie in forum C Programming
    Replies: 3
    Last Post: 08-05-2003, 06:25 AM
  5. problems reading data into an array and printing output
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 08:39 AM

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