Thread: Why does it only loop three times??

  1. #1
    iguana999
    Guest

    Angry Why does it only loop three times??

    Code:
    /////////////////////////////////////////////////////////
    // INCLUDES
    #include <iostream.h>   // delcare the header files
    #include <fstream.h>    // to be included
    /////////////////////////////////////////////////////////
    
    
    
    /////////////////////////////////////////////////////////
    // DEFINES
    #define DEAD_BAND_LIMIT 8
    /////////////////////////////////////////////////////////
    
    
    
    /////////////////////////////////////////////////////////
    // FILE STREAMS
    ifstream fin ;     // declare the input file stream
    ofstream fecgA ;   // delcare the output file stream for ECGA
    ofstream fecgB;    // declare the output file stream for ECGB
    /////////////////////////////////////////////////////////
    
    /////////////////////////////////////////////////////////
    //  PROTOTYPES
    void decodeData (int counter, int &relativeA, int &relativeB,
    							int datastoreA[], int datastoreB[]) ;
    /////////////////////////////////////////////////////////
    
    
    
    /////////////////////////////////////////////////////////
    //  used to limit the stop values from being wrapped
    
    void limitValues(int &relativeA, int &relativeB)
    {
    	// Limit values for next iteration...
    	if(relativeA > (4095 - 503))
    	{
    		relativeA = 4095 - 503 ;
    	}
    	else if(relativeA < 503)
    	{
    		relativeA = 503 ;
    	}
    	if(relativeB > (4095 - 503))
    	{
    		relativeB = 4095 - 503 ;
    	}
    	else if(relativeB < 503)
    	{
    		relativeB = 503 ;
    	}
    }
    /////////////////////////////////////////////////////////
    
    
    
    /////////////////////////////////////////////////////////
    // getData is called from the main, it is used to read in 
    // data from the ECG dat file and un-encodes the relative 
    // data into absolute data .. 
    
    void getData (int &counter, char block[], int &relativeA, int &relativeB, 
    										int datastoreA[], int datastoreB[])
    {
    	char holdComma ;  // used to handle the commas in the file
    	
    	// the tempX variables are used to hold the value of
    	// the encoded data while the absolute value is calculated
    	 
    
    	fin >> block ;
    	//cout << block << endl;
    
    	fin >> relativeA ;
    	//cout << relativeA << endl ;
    
    	fin >> holdComma ;
    	//cout << holdComma ;
    
    	fin >> relativeB ;
    	//cout << relativeB << endl ;
    	
    	datastoreA[0] = relativeA;
    	datastoreB[0] = relativeB;
    
    	while (counter<=191)
    	{
    		decodeData (counter, relativeA, relativeB, datastoreA, datastoreB) ;
    		counter ++ ;
    	}
    }
    /////////////////////////////////////////////////////////
    
    
    /////////////////////////////////////////////////////////
    // decode data
    
    void decodeData (int counter, int &relativeA, int &relativeB,
    							int datastoreA[], int datastoreB[])
    {
    	int tempA = 0, tempB = 0 ;
    	char holdComma ;
    	
    	fin >> tempA ; // hold temp value of channel A
    	
    	if ((0<=tempA)&&(tempA<=503))
    	{
    	// computes and stores the absolute value (channel A)
    	datastoreA [counter] = datastoreA[counter - 1] + tempA; 
    	}
    	else if ((505<=tempA)&&(tempA<=519))
    	{
    		counter-- ;
    		cout << "ERROR - Offset is out of Range: 505 - 519 !!!" <<endl;
    		fecgA.close() ;
    		fecgB.close() ;
    		//return ; can I call this as it is now???
    	}
    	else if (tempA == 504)
    	{
    		datastoreA [counter] = 4095 - DEAD_BAND_LIMIT ;
    		cout << "POSITIVE SATURATION: @ Element " << counter << endl ;
    	}
    	else if (tempA == 520)
    	{
    		datastoreA [counter] = DEAD_BAND_LIMIT ;
    		cout << "NEGATIVE SATURATION: @ Element " << counter << endl ;
    	}
    	else 
    	{
    		datastoreA [counter] = datastoreA[counter-1] - (1024 - tempA);
    	}
    
    	fin >> holdComma ; // holds the comma
    	
    	fin >> tempB ; // holds temp value of channel B
    
    	if ((0<=tempB)&&(tempB<=503))
    	{
    		// computes and stores the absolute value (channel A)
    		datastoreB [counter] = datastoreB[counter - 1] + tempB; 
    	}
    	else if ((505<=tempB)&&(tempB<=519))
    	{
    		counter--;
    		cout << "ERROR - Offset is out of Range: 505 - 519 !!!" <<endl;
    		fecgA.close() ;
    		fecgB.close() ;
    		//return ; can I call this as it is now???
    	}
    	else if (tempB == 504)
    	{
    		datastoreB [counter] = 4095 - DEAD_BAND_LIMIT ;
    		cout << "POSITIVE SATURATION: @ Element " << counter << endl ;
    	}
    	else if (tempB == 520)
    	{
    		datastoreB [counter] = DEAD_BAND_LIMIT ;
    		cout << "NEGATIVE SATURATION: @ Element " << counter << endl ;
    	}
    	else 
    	{
    		datastoreB [counter] = datastoreB[counter-1] - (1024 - tempB);
    	}
    
    	limitValues(relativeA, relativeB) ;
    
    }
    /////////////////////////////////////////////////////////
    
    
    
    /////////////////////////////////////////////////////////
    // fileOutput is used to write all the computed data from 
    // the arrays to a file
    
    void fileOutput (int datastoreA[], int datastoreB[])
    {
    	// file output for channel A!
    	int outcount = 0 ;
    
    	for (outcount = 0; outcount<=191; outcount++)
    	{
    		fecgA<<datastoreA[outcount] << endl ;
    	}
    	
    	// open channel B output file
    	for (outcount =0; outcount<=191; outcount++)
    	{
    		fecgB<<datastoreB[outcount]<< endl ;
    	}
    }
    /////////////////////////////////////////////////////////
    
    
    
    /////////////////////////////////////////////////////////
    // VOID MAIN
    
    void main()
    {
    	int relativeA = 0, relativeB = 0 ; 
    	int counter = 1 ;
    	
    	int datastoreA[191] ;
    	int datastoreB[191] ;
    	
    	char block[4] ;
    	
    	fin.open("smallECG.dat") ;
    	
    	if (!fin)
    	{
    		cout << "Error Opening File ... " << endl ;
    	}
    	else
    	{
    		fecgA.open("ecgA.dat") ;
    		fecgB.open("ecgB.dat") ;
    	
    	int testcount = 1 ;
    		
    	while(fin)
    	{
    		cout << "Loop run number: " << testcount << endl ;
    		getData (counter, block, relativeA, relativeB, 
    								datastoreA, datastoreB) ;
    		
    		fileOutput (datastoreA, datastoreB) ;
    		testcount++ ;
    		// problems with the eof test.... what tested on, first  3 runs work now???
    	}
    	
    	fecgA.close();
    	fecgB.close();		
    	
    	}
    	
    	fin.close() ;
    }
    /////////////////////////////////////////////////////////


    Some Sample Data....

    Block
    2048, 2048
    1023, 0
    1, 0
    0, 1021
    1022, 5
    2, 1022
    1, 1017
    1016, 20
    1019, 32
    6, 1
    8, 1012
    1022, 8
    1020, 5
    7, 1012
    1010, 11
    956, 41
    987, 1012
    91, 944
    294, 2
    383, 96
    65, 38
    673, 943
    701, 877
    957, 927
    13, 40
    12, 105
    23, 54
    1022, 15
    1021, 14
    6, 1
    4, 2
    4, 7
    3, 3
    6, 1023
    7, 1
    8, 3
    6, 0
    6, 1
    7, 1
    10, 1
    10, 3
    11, 1
    12, 1021
    17, 1022
    16, 1
    12, 1022
    16, 1023
    11, 1
    2, 1023
    4, 2
    1023, 3
    1011, 0
    1007, 1022
    1012, 3
    1006, 4
    986, 1021
    978, 1021
    1004, 1
    1019, 2
    1010, 1021
    1018, 1023
    1020, 1021
    1008, 0
    1012, 5
    2, 1
    5, 1020
    7, 4
    10, 5
    7, 1
    8, 5
    7, 2
    1023, 0
    0, 1022
    13, 1023
    3, 2
    1011, 4
    3, 0
    10, 1023
    1019, 3
    0, 0
    1, 1017
    1013, 1021
    1023, 4
    8, 0
    1, 1023
    1023, 5
    1019, 0
    1018, 1019
    1022, 1
    9, 4
    12, 2
    1021, 0
    1008, 1022
    12, 5
    27, 3
    1011, 1021
    998, 1021
    5, 0
    9, 1023
    1022, 0
    4, 3
    12, 10
    11, 11
    3, 5
    5, 9
    30, 16
    22, 1023
    1014, 1009
    1018, 1017
    5, 1019
    1015, 1013
    1000, 1018
    999, 1021
    2, 1019
    15, 0
    1014, 1
    1001, 1020
    3, 1022
    12, 2
    1010, 5
    1022, 1018
    13, 1021
    978, 37
    955, 17
    28, 943
    203, 968
    379, 80
    241, 91
    803, 1008
    625, 912
    865, 885
    5, 988
    5, 90
    23, 84
    14, 21
    1017, 16
    9, 11
    8, 1
    1021, 4
    5, 5
    17, 3
    1, 4
    1020, 2
    10, 1023
    12, 4
    6, 4
    8, 0
    9, 1
    13, 0
    15, 1022
    11, 2
    12, 2
    23, 0
    16, 2
    1, 2
    2, 3
    6, 3
    1022, 2
    1018, 1
    1015, 0
    1007, 0
    1000, 0
    997, 1
    997, 1
    1000, 0
    1007, 1021
    1011, 1021
    1011, 1
    1022, 1
    5, 1023
    1019, 0
    1016, 1
    5, 0
    3, 5
    1023, 2
    1, 1023
    1023, 0
    0, 1022
    9, 1023
    6, 7
    0, 1
    1023, 1019
    1023, 1
    1, 1
    5, 1022
    1, 1021
    1017, 0
    1022, 0
    4, 0
    1022, 1022
    1021, 0
    0, 2

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    what is the program supposed to do?

  3. #3
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65
    I would have to ask the same thing! Its quite confusing.
    I have a rabbit in my pants! Please be happy for me.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    oaoaweeewaaaaaaaaa!

  5. #5
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Tell us what the program does and then isolate the problem to a specific area and post that here.
    Im not going to read through all of that code.

  6. #6
    iguana999
    Guest

    What the program does!!

    The program is designed to read data from an encoded input file, fom a heart monitor..

    then decode the data and place it into two output files for channel A and B data....

    The input file has two colomns, they are A and B respectively, and the data is apparently encoded in dual slope 10-bit delta encoding...

    Does anything make sense here/?


    I need the program to read in the Data from input file, decode it and store it into arrays, then copy it to two output files, then do it again and again until the eof is encountered...

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > char block[4] ;

    This should be at least 6 characters, as the string you are reading is 5, then you need room for the string terminator.

    char block[6] ;

    Your counter variable gets corrupted because of this.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I debated whether or not to reply to this thread. In the end my disgust for void main decided for me, but I'll only focus on that and leave the rest of my nagging comments to myself...

    void main is wrong, do not use it!

    -Prelude
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >/////////////////////////////////////////////////////////
    >// VOID MAIN

    >void main()
    >{

    And it's even commented so as to not forget to use it in future programs.

  10. #10
    iguana999
    Guest

    here.. I asked for help not harrassment

    Im only programming in the way I have been taught..

    The only reason you should have to reply on this thread is to HELP

    ....... Immature ... hhhhmmmnn

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Im only programming in the way I have been taught..
    You were taught wrong.

    >The only reason you should have to reply on this thread is to HELP
    I told you that void main is wrong, that's help. If you don't want my help, just say the word and I won't reply to your posts.

    This is a rough place, we don't care about making you feel warm and fuzzy, we care about passing information back and forth as efficiently as possible. If you can't handle that then go elsewhere.

    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Is void main() used in C or earlier versions of C++? I know why not to use it, but even some MSDN examples use it.

  13. #13
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    what is MSDN?

  14. #14
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    A big windows programming help thing made by MicroSoft

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutiliplcation Program that uses a (do, while, and for loop)?
    By Debbie Bremer in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 06:04 PM
  2. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  3. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM