Reading values from a chart and creating a new one

This is a discussion on Reading values from a chart and creating a new one within the C++ Programming forums, part of the General Programming Boards category; Hello everybody. I need some help with this. Since I'm new to programming, I don't want anybody to actually do ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    4

    Reading values from a chart and creating a new one

    Hello everybody. I need some help with this. Since I'm new to programming, I don't want anybody to actually do the program for me, but if you could just name some terms or subjects I could research to figure it out I'd really appreciate it.

    This program I'm working on has basically has 2 roles. It reads a very confusing file spit out by a computer that's recording rat movement through a maze, and organizes it and removes unnecessary info and creates a new file with the organized values. That part was completed by much more knowledgable person than me. It's the 2nd part that I have to do and am stuck at.

    Now I need to enhance the program the other guy made so that itreads the new organized file (attachment) it just created. And I basically have to make a program that does what this researcher was doing by hand:

    "2) I create a new column titled “ARM” after the last column in the spreadsheet, which is usually Column: AJ, Row 7.

    3) In this column, I start with the first counter that gets to “3”, and whichever arm it corresponds to, I write down in the new column. For example, if “Reinforcement Counter for ArmFlag # 4” is the first counter to reach 3, then I would enter the number “4” in the new column, at whatever row you found the “3”.

    4) I then scroll down until either the first ArmFlag counter to reach 3 has reached 5, OR, until another ArmFlag counter has reached 3. Whichever Arm this is, is recorded into the “ARM” column, in the corresponding row.

    5) I then scroll down until either a) A different ArmFlag counter reaches 3, or, b) One of the same ArmFlag counters increases to the next odd number. Whichever of these happens first, is the arm that I write in the “ARM” column.

    a. For Example, if the “Reinforcement Counter for ArmFlag 3” is “5,” and the next thing that happens as I scroll down the spreadsheet is that the “Reinforcement Conter for ArmFlag 3” increases to “7” in Row 25, then in Row 25 I would write “3”. However, if the next thing that happens is that the Counter for ArmFlag 2 increases from nothing to 3, then I would write “2” in the corresponding row.

    6) Next, I repeat step 5, looking for the counters to increase to the next odd number (i.e 3,5,7,9,11…etc), and record which Arm it was in the “ARM” column. Thus, I follow the instructions in Step 5 precisely until I have examined the entire spreadsheet."


    Like I said, you don't need to get too specific if you don't want to. I have a few books on C++, so if you could just give me some pointers I could go research them.

    I'll attach the organized file.

    Thanks a lot.
    Attached Files Attached Files

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    my personal view, if the number of columns and rows
    are constant, id use a array of structs

    Code:
    struct mystruct
    {
         double data1;
         double data2;
         double data3;
         double data4;
         double data5;
    } data[20];


    where data1 - 5 or however amny columns there is
    would be for the value across in the columns.


    and the 20 out of data[20] would be the number of rows.

    that way you could simply read from a file with a loop to
    fill the structs.

    ex.
    Code:
    ifstream in("cptest.txt");
    for ( int i = 0; i != 20; i++ )
    {
         in >> data.data1[i] >> data.data2[i] >> data.data3[i] >> data.data4[i] >> data.data5[i];
    }
    when the loop finish the file should be stored in the array of structs, then you can make any nessassary changes to the data
    then use a similar loop to write out to a new file.


    there may be a better way to do it, but this doesnt seem very
    complicated to me then agian i may have missed soemthing
    you wanted. good luck.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    4
    EDIT: Seems like I was wrong and the rows ARE constant. I'll keep working on it. Thanks.
    Last edited by sackyhack; 08-23-2005 at 10:01 AM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    4
    Ok I'm still having trouble. It gives me this error for every column I tried to make into a struct:

    alt4Arm.cpp(179) : error C2228: left of '.data1' must have
    class/struct/union type
    type is 'Experiment::analyze::mystruct [50]'

    Here I'll post the code and you can see what the guy before me did, and what I tried to do. Keep in mind I'm VERY new to C++, so be prepared for some great atrocities and coding genocide in my portion. I never had a class or anything, I'm just trying to learn from a book. The bit I wrote is towards the botom of the code. I'll put in dashes to separate my code from his. I'll also attach the original unorganized file that his code organized into cptest1

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    
    class Experiment
    {
    public:
    	Experiment();	// constructor
    private:
    	void analyze(); // method for analyzing data
    }; 
    
    Experiment::Experiment()
    {
    	analyze();		// runs analzye to process data
    }
    
    void Experiment::analyze()
    {
    	ifstream abetData;		// declare input steam
    	ofstream testAbet, discardData;	// declare output streams
    	ifstream processedAbet;  // file of data extracted from ABET file
    	ofstream resultsAbet;	// Final output file
    	string input = "A";		// define buffer to hold data
    	int WIDTH = 9;
    	int armCountrs[8]     = {0, 0, 0, 0, 0, 0, 0, 0};
    	int entryCounters[8]  = {0, 0, 0, 0, 0, 0, 0, 0};
    
    	testAbet.open("cptest1.txt");
    	if (!testAbet){
            cout << "Cannot open Output File" << endl;
    		exit(1);
    	}
    
    	discardData.open("discard.txt");
    	if (!discardData){
            cout << "Cannot open Discard File" << endl;
    		exit(1);
    	}
    
        abetData.open("test1.txt");
    	if (!abetData){
            cout << "Cannot open Input File!" << endl;
    		exit (1);
    	}
    
    	// Read and ignore header preceding first timestamp at 0.000
    	for (int i=0;i<35;i++)
            getline(abetData,input,',');   // reads input using comma as delimeter
    	
    	// Write a row header to the output file
    	testAbet.width(WIDTH);
    	testAbet << right << "Time";
    	testAbet.width(WIDTH);
    	testAbet << right <<"Arm #1";
    	testAbet.width(WIDTH);
    	testAbet << right << "Arm #2";
    	testAbet.width(WIDTH);
    	testAbet << right << "Arm #3";
    	testAbet.width(WIDTH);
    	testAbet << right << "Arm #4";
    	testAbet.width(WIDTH);
    	testAbet << right << "Arm #5";
    	testAbet.width(WIDTH);
    	testAbet << right << "Arm #6";
    	testAbet.width(WIDTH);
    	testAbet << right << "Arm #7";
    	testAbet.width(WIDTH);
    	testAbet << right << "Arm #8" << endl;
    
    	
    
    	for (int n=0;n<50;n++) {		// check first 50 rows for debug purposes
    		for (int i=0;i<34;i++){
                // discard Input entries
    			getline(abetData,input,',');
    			if (!abetData || abetData.eof())	// check for end of file
    				exit(1);
    			discardData << right << input << '\t';
    		}
    		discardData << endl;
    		input.erase();
    
    		
    
    		// get and record timestamp in output file
    		getline(abetData,input,',');	// ignore first read
    		getline(abetData,input,',');	// this holds timestamp
    		testAbet << right << input;
    
    			
    		for (int j=0;j<25;j++){
                // discard leading entries in each Output row
    			getline(abetData,input,',');
    			//abetData >> input;
    			discardData << input;
    		}
    
    		// Check Reinforcement Arm Flags
    		for (int k=0;k<8;k++) {
    			getline(abetData,input,',');
    			if (input == " ") {
    				testAbet.width(WIDTH);
    				testAbet << right << "0";
    			}
    			else{
    				testAbet.width(WIDTH);
    				testAbet << right << input;
    			}
    		}
    		testAbet << endl;
    		// discard Timer entry
    		abetData >> input;
    		discardData << "X" << input << endl;
    	}
    
    
    
    	abetData.close();
    	testAbet.close();
    
    	processedAbet.open("cptest1.txt");
    	if (!processedAbet){
            cout << "Cannot open Processed Data File" << endl;
    		exit(1);
    	}
    
    	resultsAbet.open("results.txt");
    	if (!resultsAbet){
            cout << "Cannot open Results File" << endl;
    		exit(1);
    	}
    
    	//skip header row
    
    	// read in each Arm Counter and compare to appropriate armCounter
    	// when armCounter reaches 3, increment appropriate entryCounter
    	// and write that arm to results file
    ------------------------------------------------------------------------------
    struct mystruct
    {
         double data1;
         double data2;
         double data3;
         double data4;
         double data5;
    	 double data6;
    	 double data7;
    	 double data8;
    } data[50];             //Creates an array of structs
    
    ifstream in("cptest1.txt");
    for ( int i = 0; i != 20; i++ )           //Reads from the file and fills in the array of structs
    {
         in >> data.data1[i] >> data.data2[i] >> data.data3[i] >> data.data4[i] >> data.data5[i] >> data.data6[i] >> data.data7[i] >> data.data8[i];
    }
    
    
    	processedAbet.close();
    	resultsAbet.close();
    }
    ------------------------------------------------------------------------------
    
    
    int main()
    {
    	Experiment exp;
    	return 0;
    }

    Any help would be greatly appreciated.
    Attached Files Attached Files

  5. #5
    Banned
    Join Date
    Jun 2005
    Posts
    594
    holy wow, all i can say, first thing you need to look at, was double was just my choice at the time cause i saw big number, your obviously going to be needing to use string's as variabe in
    the struct for some of this stuff.


    so you may need multiple loops to read the seperate data in.
    where it differs.

    Code:
    ScheduleTime,   ,Entry Sensor #1,Cup Sensor #1,Entry Sensor 
    #2,Cup Sensor #2,Entry Sensor #3,Cup Sensor #3,Entry Sensor 
    #4,Cup Sensor #4,Entry Sensor #5,Cup Sensor #5,Entry Sensor 
    #6,Cup Sensor #6,Entry Sensor #7,Cup Sensor #7,Entry Sensor 
    #8,Cup Sensor #8,Automatic Door #1,Automatic Door 
    #2,Automatic Door #3,Automatic Door #4,Automatic Door 
    #5,Automatic Door #6,Automatic Door #7,Automatic Door 
    #8,Reinforcement Counter for ArmFlag #1,Reinforcement Counter 
    for ArmFlag #2,Reinforcement Counter for ArmFlag 
    #3,Reinforcement Counter for ArmFlag #4,Reinforcement Counter 
    for ArmFlag #5,Reinforcement Counter for ArmFlag 
    #6,Reinforcement Counter for ArmFlag #7,Reinforcement Counter 
    for ArmFlag #8,Interval Timer #1,
    i see that you have that many columns, so that means
    you will need that many variables inside your struct.
    each one corresponding to the correct data type
    or they wont be read in correctly.
    what i recommend you do is to read the data in gy using getline
    and using ',' as a delimiter.

    Code:
    getline(in , STRUCTMEMBER[i].data1, ',');
    a loops containing as many of these as need
    to read each line will be required, then you will
    have to use your brain to understand that
    STRUCTMEMBER[0 thru size of struct array].firstvariable will
    contain everything under the ScheduleTime column if you read
    in the data correctly.


    im out the door on the way to work right now so if
    no one give you a better answer by the time i return,
    i will prolly end up going through yoru code and making
    some changes to help you better understand.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    4
    Thanks so much for your help. But I think there's some misunderstanding. I don't need to deal with the second file I attached, just the first one (cptest1). I attached the 2nd file so you knew where all the values that went into cptest1 came from.

    The computer that records the rat in the maze produces test1.txt (the 2nd attachment). The guy who started the program before me, wrote the program so it reads that messy text file, eliminates the uneccessary stuff, and organizes what is needed and puts it into a new text file, cptest1.txt (the attachment in my first post). Now I have to add to the program so that after cptest1.txt is created, the program goes back to it and reads it and does what the researcher wants (the steps in my first post).

    I'm at home now and don't have access to the pc w/ the program on it, so I can't show you what I've done. But after reading the books more, I managed to get it to the point where it reads cptest1.txt and properly stores the values. (I tested it by printing the recorded data into 3rd output file, called results.txt).

    Now what I have to do is create some kinda counter that will go through those stored values and do what the researcher wants.

    Incase anyone was weary of helping me, don't worry you're not doing my job for me. My boss at my volunteer(as in unpaid) research job knows and has stated that he doesn't expect me to actually finish the program (If I could, I would be a full time employee at a biomedical engineering company). It's more of a learning experience to familiarize me w/ the language.

    So I really appreciate you walking me through this and putting up with my nonesense
    Last edited by sackyhack; 08-25-2005 at 02:17 PM.

  7. #7
    Banned
    Join Date
    Jun 2005
    Posts
    594
    there been alot worse peopel to ask things here.
    if i hadnt been really really busy at the time i prolly would
    of wrote then entire code weather you had wanted
    me to or not. i look at other people problem as if it
    where a challange for me lol.

    but since i didnt have much time, i went the explaination route
    to try and give you a good idea. i plan to look at this code here
    later tonight.

    PS
    if you havent figured it out yet,
    Code:
    for ( int i = 0; i != 20; i++ )           //Reads from the file and fills in the array of structs
    {
         in >> data.data1[i] >> data.data2[i] >> data.data3[i] >> data.data4[i] >> data.data5[i] >> data.data6[i] >> data.data7[i] >> data.data8[i];
    }
    it should be data[i].data1 and so on.

    i had just typed that up in the window real fast and didnt
    pay attention earler. so ill go ahead and fix my previous message.

    [edit]
    bah been to long i cant edit it, sorry about that misunderstanding.
    [/edit]
    Last edited by ILoveVectors; 08-25-2005 at 04:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to create chart
    By vipul_vgp in forum Linux Programming
    Replies: 1
    Last Post: 07-06-2009, 10:18 AM
  2. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM

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