Thread: I need help

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    18

    I need help

    I am trying to accomplish:
    This assignment is concerned with sorting of data. Your task is to
    read data from two files and create two sorted files. The primary
    information file is called "main.run". It contains an unknown
    number of records, with each record (line) consisting of a 25
    character student name (First Last), an eleven character time
    ("00:00:00:00"), and a numeric team code. The team code is
    separated from the time by one space and matches a team name of 35
    characters in a second data file called "teams.dat". The first
    team name in the "teams.dat" file has a team code of 1, the second
    team 2, etc.

    Your C++ program must first read the "main.run" file, sort the data
    by time, and write the sorted information to an output file called
    "sorted.out". The output file must contain the runner's name, school,
    and time. After printing the "sorted.out" file, print an alphabetized
    list of schools which had runners in the race and the number of
    runners that finished the race for each school. Call the second
    output file "school.out".

    Use the following shuttle-exchange algorithm to sort your data:

    SHUTTLE-EXCHANGE SORT

    The algorithm works in a looping structure as follows:

    1) Set a Boolean variable to true. This variable will be used
    to indicate if the list is ordered.

    2) Compare the first element in the array to the second element.
    If they are not in order, swap the contents of the elements and
    set the Boolean variable to false.

    3) Compare the second element to the third element. If they are
    not in order swap them and set the Boolean variable to false.
    Continue to make the above type of comparison thru N - 1 elements,
    where N is the maximum number of elements. ( example: 3 to 4,
    4 to 5, 5 to 6, ...,N - 1 to N )

    4) If the Boolean variable is true after a pass through the loop,
    the list is ordered and the looping can be terminated. If the
    Boolean variable is false, set it to true and begin the looping
    process all over again.


    my code thus far:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstring>
    #include <conio>
    
    using namespace std;
    ifstream input;
    ofstream out;
    struct main 
    {
      char name[26];
      int time[11];
    };
    main mouse;  
    
    
    void ReadMainData(ifstream& input);
    
    int main()
    {
    ifstream input;
    ofstream out;
    
    void ReadMainData(ifstream& input);
      for(int i=0; i<26; i++)
      cout << mouse.name[i];
    
    return 0; 
    }
    /*********************************************************/
    void ReadMainData(ifstream& input)
    {
      input.open("main.run");
      while(!input.eof())
      {
        for(int i=0; i<26; i++)
        {
          input.get(mouse.name[i]);
        }
        for(int i=0; i<11; i++)
        {
          input.get(mouse.time[i]);
        }
      }
      for(int i=0; i<11; i++)
      cout << mouse.time[i];
    }
    my question is what should I improve and add anything else you want.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    a few things: first, I'd put the struct definition in main and pass it in to the function. second, I'd look into using the stl string class instead of character arrays for this particular app. second, you have two input and two output objects. leave them out of the global declarations and you should be fine. finally, I dont' see a need for <cstring> and <conio> in your program, but I didn't take too long of a look.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    18
    How would I pass them also, I keep getting an error message saying undefinde symbol ifstream.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    hmm... on second look, that code's quite a mess... lemme take a longer look at it and I'll get back to you shortly
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    18
    I tried this out can you give it a look also. the program hangs and repeats the last line of the file though.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstring>
    #include <conio>
    
    using namespace std;
    ifstream input;
    ofstream out;
    
    
    int main()
    {
    struct main
    {
      char name[25];
      char time[11];
      char code[6];
    };
    struct teams
    {
      char team[30];
    };
    main mouse;
    teams cat;
    
      input.open("main.run");
      while(!input.eof())
      {
        for(int i=0; i<25; i++)
        {
          input.get(mouse.name[i]);
        }
        for(int i=0; i<11; i++)
        {
          input.get(mouse.time[i]);
        }
        for(int i=0; i<6; i++)
        {
          input.get(mouse.code[i]);
        }
      
      for(int i=0; i<25; i++)
      cout << mouse.name[i];
      for(int i=0; i<11; i++)
      cout << mouse.time[i];
      for(int i=0; i<6; i++)
      cout << mouse.code[i];
      }
      input.close();
      
    input.open("teams.run");
    while(!input.eof())
    {
      for(int i=0; i<31; i++)
      {
        input.get(cat.team[i]);
      }
    }
    
    return 0; 
    }

  6. #6
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    the program ... repeats the last line of the file though.
    Not sure if this is the problem, but I'd find using

    Code:
    While (infile>> name)
    more reliable than !EOF and any of its variants.
    The FAQ warns against FEOF, but I'm not sure if it extends to
    !EOF. Either way, I wouldn't use any.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    18
    I have this so far:
    But does not print out the right way.
    Could you help get me started if I link you to the files?

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstring>
    #include <conio>
    
    using namespace std;
    ifstream input;
    ofstream out;
    
    
    int main()
    {
    struct main
    {
      char name[26];
      char time[12];
      char code[2];
    };
    struct teams
    {
      char team[30];
    };
    main mouse;
    teams cat;
    
    input.open("main.run");
    
    while(input >> "main.run")
    {
    for(int i=0; i<26; i++)
    {
      input.get(mouse.name[i]);
      cout << mouse.name[i];
    }
    
    for(int i=0; i<12; i++)
    {
      input.get(mouse.time[i]);
      cout << mouse.time[i];
    }
    
    for(int i=0; i<2; i++)
    {
      input.get(mouse.code[i]);
      cout << mouse.code[i];
    }
    }
    
    input.close();
    
    
    return 0; 
    }
    I need to have it done by tommorow and I can't figure it out for the life of me.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1)Here is a piece of advice, use unique names for every variable in your program. BUT WHATEVER YOU DO, DO NOT NAME A VARIABLE, STRUCT, OR ANYTHING ELSE: "main". When you are thinking up a variable name for something, examine every other name on every line of your program, and do not use one of those names.

    2)Describe what you think this is doing:
    while(input >> "main.run")
    3)As you were already advised: do not define a struct inside main().

    4)Use proper indenting: after every opening brace the code should be indented.

    5) When you read input, you don't have to read one character at a time, like you are doing here:
    Code:
    for(int i=0; i<26; i++)
    {
      input.get(mouse.name[i]);
      cout << mouse.name[i];
    }
    You can read in a whole word at one shot. That is what the '>>' thing does.
    Last edited by 7stud; 11-28-2005 at 04:34 PM.

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    yeah, I took your code apart basically, here's a working version of what you had, although I'm pretty sure the logic isn't what you intended:
    Code:
    #include <iostream>	//for console IO
    #include <fstream>	//for file IO
    #include <string>	//for the string class
    
    struct mainStruct		//this holds our data
    {
    	std::string name;	//it contains a name
    	std::string time;	//and a time
    };
    
    void ReadMainData(mainStruct& mouse);	//read in the data
    
    int main()
    {	
    	//std::ofstream out;	//you never used this...
    	mainStruct mouse;	//instantiate a struct
    	//you do realize that after this runs, you'll be left with the last name
    	//in the data file, right?
    	ReadMainData(mouse);	//you don't specify datatype when calling
    	std::cout<<mouse.name;	//output a name
    	
    	return 0; 
    }
    
    void ReadMainData(mainStruct& mouse)
    {
    	std::fstream input("test.in",std::ios::in);	//open the file for reading
    	char*line=new char[25];		//create space for a char*
    	
    	while(input.get(line,25))		//take in the 25-char name
    	{
    		mouse.name.assign(line);	//assign the name to the string in the struct
        		getline(input,mouse.time,' ');	//take in and assign the time to the struct
    		input.ignore(32000,'\n');	//ignore up to the next newline
      	}
    
    	input.close();				//close the input file
    	delete[]line;				//free the memory from the char*
      	std::cout<<mouse.time<<std::endl;	//ouptut the time (consider relocating this)
    }
    basically, it only takes the last line of the code because of that loop in the function. You're going to have to rework the entire thing to get it to do what you want. you still have a good amount of work ahead of you on this one.

    **EDIT**
    This is the data file I came up with based on the instructions:
    Code:
    firstnameand lastname25a00:00:00:00 25
    firstnameand lastname25b00:00:00:00 25
    firstnameand lastname25c00:00:00:00 25
    firstnameand lastname25d00:00:00:00 25
    of course, that may be off, meaning you'd have to tweak the function a little bit just to get it working the way it does now.
    Last edited by major_small; 11-28-2005 at 04:32 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    18
    The time does not seem to read out and I tried getting the school names in but
    it wont read in.
    Here is my code thus far.
    Code:
    #include <iostream>	//for console IO
    #include <fstream>	//for file IO
    #include <string>	//for the string class
    using namespace std;
    
    struct mainStruct		//this holds our data
    {
    	string name;	//it contains a name
    	string time;	//and a time
    };
    struct teamStruct		//this holds our data
    {
      	string team;	//it contains a name
    };
    
    void ReadMainData(mainStruct& mouse);	//read in the data
    void ReadTeamData(teamStruct& cat);		//read in the data
    
    int main()
    {	
    	//std::ofstream out;	//you never used this...
    	mainStruct mouse;	//instantiate a struct
        teamStruct cat;		//instantiate a struct
    	//you do realize that after this runs, you'll be left with the last name
    	//in the data file, right?
    	ReadMainData(mouse);	//you don't specify datatype when calling
    	//cout<<mouse.name;	//output a name
        ReadTeamData(cat);
    	
    	return 0; 
    }
    
    void ReadMainData(mainStruct& mouse)
    {
    	fstream input("main.run");	//open the file for reading
    	char*line=new char[25];		//create space for a char*
    	
    	while(input.get(line,25))		//take in the 25-char name
    	{
    		mouse.name.assign(line);	//assign the name to the string in the struct
        		getline(input,mouse.time,' ');	//take in and assign the time to the struct
    		input.ignore(32000,'\n');	//ignore up to the next newline
            cout << mouse.name << mouse.time << endl;
      	}
    
    	input.close();				//close the input file
    	delete[]line;				//free the memory from the char*
      	cout<<mouse.time << endl;	//ouptut the time (consider relocating this)
    }
    
    void ReadTeamData(teamStruct& cat)
    {
      	fstream input("teams.dat");	//open the file for reading
        char*line=new char[31];		//create space for a char*
        
    	while(input.get(line,31))		//take in the 31-char name
        {
          cat.team.assign(line);	//assign the name to the string in the struct
          		getline(input,cat.team,' ');	//take in and assign the team name to the struct
          input.ignore(32000,'/n');	//ignore up to the next newline
          cout << cat.team <<endl;
        }
        
    	input.close();					//close the input file
        delete[]line;					//free the memory from the char*
        cout<<cat.team<<endl;			//output the team
    }

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    did you get what I gave you working first?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    Registered User
    Join Date
    Sep 2005
    Posts
    18
    everything but the time but after changed it a little it came up.
    also i don't think that each piece of data is being stored.

    can you help me with this part.
    The algorithm works in a looping structure as follows:

    1) Set a Boolean variable to true. This variable will be used
    to indicate if the list is ordered.

    2) Compare the first element in the array to the second element.
    If they are not in order, swap the contents of the elements and
    set the Boolean variable to false.

    3) Compare the second element to the third element. If they are
    not in order swap them and set the Boolean variable to false.
    Continue to make the above type of comparison thru N - 1 elements,
    where N is the maximum number of elements. ( example: 3 to 4,
    4 to 5, 5 to 6, ...,N - 1 to N )

    4) If the Boolean variable is true after a pass through the loop,
    the list is ordered and the looping can be terminated. If the
    Boolean variable is false, set it to true and begin the looping
    process all over again.


    And thank you very much for your help so far.

  13. #13
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    just take that one step at a time and code it as you read it, for example, it's in a loop. so you know you need some kind of looping structure. For now don't worry about what kind it is, just know that it loops.

    next, you need to read the first direction. set a boolean variable to true. so so far you know this:
    Code:
    bool bvar;	//This variable will be used to indicate if the list is ordered.
    /* some looping structure */
    {
    	bvar=true;	//here's you setting it to true, as per instruction #1
    }
    now you give it a shot.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  14. #14
    Registered User
    Join Date
    Sep 2005
    Posts
    18
    for the header to the function would it read:
    Code:
    void ShuffleExchangeSort();
    wow page 2.
    Last edited by hyrule; 11-28-2005 at 08:21 PM.

  15. #15
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I suppose... you're going to need to pass the data in too
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed