Getting frustrated

This is a discussion on Getting frustrated within the C++ Programming forums, part of the General Programming Boards category; Hey, I am trying to read in data into 3 parallel arrays to manipulate later. When I test to see ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    81

    Getting frustrated

    Hey, I am trying to read in data into 3 parallel arrays to manipulate later. When I test to see what is in each on after reading them in it is really messed up.

    Here is my code:
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <iomanip.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <string.h>
    
    //Global Variables
    ifstream infile;
    ofstream outfile;
    
    typedef char string[30];
    
    //Prototypes
    
    int main()
    {
    string social[30],phone[30],name[30];
    int i;
    	infile.open("In599A.Dat");
    		while(!infile.eof())
    		{
    			for(i=0;i<23;i++)
    			{
    			infile>>social[i]>>phone[i];
    			infile.getline(name[i],30,'\0');
    			
    
    			}
    		}
    		for(int j=0;j<22;j++)
    			cout << social[j] << endl;
    
    
    	return 0;
    }
    Here is the contents of the file I am reading from:
    Code:
    526334512 2665312 Jones, Julie Claire
    264910093 2689022 Morgan, James Walter
    587194309 5441234 Allen, Mary Ann
    264881234 2669128 Thompson, William C
    267456543 2683139 Williamson, Terry H
    587521937 2680145 Robertson, Susan Leigh
    588552584 2661357 Yelverton, Ted W
    267552894 2664422 Bolton, Rebecca Lynn
    576885197 5451992 Edwards, Tracy Ann
    234675891 2610094 Walker, LaTonya B
    641225537 2664949 Young, Henry Clarke
    247569311 2665278 Thompkins, Marcia R
    455124690 2681189 Price, Adam Paul
    426133299 2684518 Vickery, Kathy Ann
    587023057 2685572 McPhail, Ann K
    258199876 5446993 Clark, Rodney P
    256451389 5441898 Robbins, Terry Lynn
    456113278 5453358 Hickman, Susan Leigh
    257126677 2668818 Upton, Richard P
    487931470 2665001 Shaw, Anna Jane
    289889637 2665017 Zachary, Phillip F
    587366338 2610358 Calhoun, Roderick T
    thanks.

  2. #2
    0x01
    Join Date
    Sep 2001
    Posts
    88
    I thought you were supposed to keep peoples social security numbers private?

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    they're fake numbers

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> I thought you were supposed to keep peoples social security numbers private?

    Doubt those are real SSNs.

    Anyways, I think your problem is in getline. Lines in a file do not end in \0. In fact, I believe you can leave the delimiter out of the function call.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    Bam! that was it. I have a few more questions to ask. I will continue to ask them in this forum when they arise. Thanks again.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    81

    Sorting parallel arrays

    Okay, I need to sort the name array in alphabetical order, but I need to keep the other arrays to correspond with the moves that I do. I tried to start by using the Bubble Sort and my program keeps crashing.

    Please take a look at my code and tell me what might be the problem:

    Code:
    I did a typedef char string[30] as a global variable..
    
    
    void bubblesort(string name[], int size)
    {
    	int i, j;
    	string temp;
    	for(i=0;i<size-1;i++)
    		for(j=1;j<size;j++)
    			if(strcmp(name[j],name[j-1])<0)
    			{strcpy(temp,name[j]);
    			strcpy(name[j],name[j-1]);
    			strcpy(name[j-1],temp);
    			}
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    typedef char string[30];
    I'll never understand why people find the need to do this. That being said, you really shouldn't call you "string" string. After all, there is already a class called string (#include <string>), and anything starting with 'str' is a reserved key word.

    Furthermore, why don't you just use a structure to store all of your items, and have an array of those?

    [edit] Actually, since you're including <string.h>, I'm surprised this even compiles. [/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    We haven't studied structs yet. I hear they are a lot easier to use. For now, I will stick with what I know.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    <string.h> is still a valid header in C++, equivalent to <cstring> essentially.

    Anyways, Quzah's structure idea is the way to go. Store your data in the structure, provide a suitable operator< for it, keep a vector, and use the standard sort algorithm.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Why don't you use the string class instead of the char array to store the strings? I just checked it out today, and it's loads easier. Here's a link with a good section on the STL string class and a little on a couple other STL classes.

    http://www.msoe.edu/eecs/cese/resources/stl/
    Away.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Zach L.
    <string.h> is still a valid header in C++, equivalent to <cstring> essentially.
    Yeah, I was thinking that the 'string' class was defined in said header.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    thanks for your help, but my teacher is really picky about us moving ahead. She wants us to understand how to operate with parallel arrays.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should write a function to swap strings. Something like:
    Code:
    void swapstuff( string array[], size_t fromhere, size_t tohere )
    {
        buffer
    
        copy tohere to buffer
        copy fromhere to tohere
        copy buffer to fromhere
    }
    The hint here is that you use the 'cell' number to swap that location in your array. You'd call it with something like:

    swapstuff( array1, 5, 2 );
    swapstuff( array2, 5, 2 );
    swapstuff( array3, 5, 2 );

    This way, you end up swapping all three arrays, keeping their values matching.

    I'll leave the actual copy process to you. It should be trivial.

    Note: I used "string" as your typedef, though I'd really suggest changing it.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    I think you've lost me...

  15. #15
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    He's saying this:

    Write a function to swap two values in an array. This function should take the array as an argument, as well as the indices to swap. Then, simply call that swap function on all your arrays each time you swap something in the sort. That way, all arrays stay synchronized.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. frustrated bout char array
    By royuco77 in forum C++ Programming
    Replies: 2
    Last Post: 07-11-2005, 10:10 AM
  2. A Bit Frustrated....
    By DirX in forum C++ Programming
    Replies: 3
    Last Post: 04-14-2003, 11:29 PM
  3. so confused and frustrated
    By ct26torr in forum C Programming
    Replies: 2
    Last Post: 02-13-2003, 10:40 PM
  4. frustrated job!
    By Cemilia in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 11-27-2002, 09:00 AM
  5. so frustrated!!!!
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-28-2001, 06:44 PM

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