I want get data from the car1.txt file and do the probability calculation but all result is 0.0% ?? just want to ask how to get large number of data from txt file by using array method.
This is my txt file and cpp file

1	1	2	2	3	4	unacc
1	1	2	2	3	3	unacc
1	1	2	2	3	2	unacc
1	1	2	2	3	4	unacc
1	1	2	2	3	3	unacc
1	1	2	2	3	2	unacc
#include <iostream.h>
#include <fstream.h>
#include <math.h>
#include <iomanip.h>
#include <conio.h>

#define LAJUR 6
#define BARIS 1036

void data();
int count=0, number,i,j;
char k;
double newcase[LAJUR];
double localsim[BARIS][LAJUR];

int oldCases[BARIS][LAJUR+1];

fstream infile;
fstream outfile;

void main(){
    data();
        cout << "\n\n*************************************";
        cout << "\n*                                   *";
        cout << "\n*        Car Classify System        *";
        cout << "\n*                                   *";
        cout << "\n*************************************";
        cout << "\n\n";
        //INPUT NEW CASE
        
        cout << "\n\n1st data : Buying --> v-high[1], high[2], med[3], low[4]\n"
                << "2nd data : Maint --> v-high[1], high[2], med[3], low[4]\n"
                << "3rd data : Doors --> 2,3,4,5-more [5]\n"
                << "4th data : Person --> 2,4,more [5]\n"
                << "5th data : Lug_boot --> big[1], med[2], small[3]\n"
                << "6th data : Safety --> high[1], med[2], low[3]\n\n";
        for(i=0;i<LAJUR;i++){
                cout << "Insert data " <<i+1 <<":\n";
                cin >> newcase[i];
        }
        cout << "New Case: {";
        for(i=0;i<LAJUR;i++){
                cout << newcase[i] <<",";
        }
        cout << "}\n\n";
        
        //CALCULATE WEIGHT
        
        int weight[LAJUR]={1,1,1,1,1,1};
        int sumweight=0;
        double normWeight[LAJUR]={0,0,0,0,0,0};
        
        for(i=0;i<LAJUR;i++){
                sumweight= sumweight + weight[i];
        }
        cout << "sumweight: " << sumweight << "\n";
        for(i=0;i<LAJUR;i++){
                normWeight[i] = double(weight[i])/double(sumweight);
        }
        
        //CALCULATE LOCAL SIMILARITY
        
        for(i=0;i<BARIS;i++){
                for(j=1;j<LAJUR+1;j++)
                {
                                if(newcase[j-1]==oldCases[i][j]){
									localsim[i][j]=1;
								}
                                else{
                                 localsim[i][j]=0;
                                }
                }
        }
        //CALCULATE GLOBAL SIMILARITY
       double globalsim[BARIS]={0,0,0,0,0,0};
        cout << "\n\n*************************************";
        cout << "\n*                                   *";
        cout << "\n*        Global Similarity          *";
        cout << "\n***************************************";
        cout << "\n\nGlobal Similarity    " << "Class\n";
        
        for (i=0;i<BARIS;i++){
                for(j=0;j<LAJUR;j++){
                 globalsim[i] = (globalsim[i] + localsim[i][j+1]*normWeight[j]);
        }
        globalsim[i]= globalsim[i]*100;
         //cout << "\n";
         //cout << globalsim[i] << "% "<< oldCases[i][0];
        }
        for(i=0;i<10;i++){
                cout << "\n";
                cout << globalsim[i] << "% " << oldCases[i][0];
        }
        
        //SORT SIMILARITY RESULT
        double temp;
        double tempClass[BARIS];
        for(i=0;i<BARIS;i++){
                tempClass[i]=oldCases[i][0];
        }
        for(i=0;i<BARIS;i++){
                for(j=0;j<BARIS;j++){
                 if(globalsim[j]<globalsim[j+1]){
                  temp=globalsim[j];
                  globalsim[j]=globalsim[j+1];
                  globalsim[j+1]=temp;
                  
                  temp=tempClass[j];
                  tempClass[j]=tempClass[j+1];
                  tempClass[j+1]=temp;
                 }
                 else{
                 }
                }
        }
        cout << "\n\n*************************************";
        cout << "\n*                                   *";
        cout << "\n*        Sorted Similarity Result   *";
        cout << "\n*                                   *";
		cout << "\n*************************************";
        cout << "\n\nGlobal Similarity (Sort) "<<"Class\n\n";
		
        for(i=0;i<10;i++){
                cout << globalsim[i] << "% " << tempClass[i] <<"\n";
        }
        //MAJORITY VOTING
        int class1 =0,class2=0,class3=0,class4=0;
        cout << "\n\n*************************************";
        cout << "\n*                                   *";
        cout << "\n*        Majority Voting            *";
        cout << "\n*                                   *";
        cout << "\n*************************************";
        cout << "\n\nGlobal Similarity "<<" Class\n\n";
        for(i=0;i<10;i++){
                if(globalsim[i] > 80.00)
                { 
					cout << globalsim[i] << "% " << tempClass[i] << "\n";
                 {                 if(tempClass[i]==1)
                                      class1=class1+1;
                                      else if (tempClass[i]==2)
                                      class2=class2+1;
                                      else if (tempClass[i]==3)
                                      class3=class3+1;
                                      else if (tempClass[i]==4)
                                      class4=class4+1;
                 }
                }
               }
               int tempValue;
               int array[4];
               array[0]=class1;
               array[1]=class2;
               array[2]=class3;
               array[3]=class4;
               
               for(i=0;i<4;i++){
                for(j=0;j<4;j++){
                 if(array[i] <= array[j])
                 {
                  tempValue=array[i];
                   array[i]=array[j];
                   array[j]=tempValue;
                  }
                 }
                }
                //PRINT RESULT
                cout << "\n\n";
                cout<<"Class 1 ="<< class1<<endl;
                cout<<"Class 2 ="<< class2<<endl;
                cout<<"Class 3 ="<< class3<<endl;
                cout<<"Class 4 ="<< class4<<endl;
                
                cout << "\nThe class for the new case is ";
                 if(array[3]==class1)
                                  cout << "v-good"<<endl;
                 else if(array[3]==class2)
                                  cout << "good"<<endl;
                 else if(array[3]==class3)
                                  cout << "acc"<<endl;
                 else if(array[3]==class4)
                                  cout << "unacc"<<endl;
                 else {
                                  cout << " Sorry, system cannot define the car's class!";
                                  
                 }
                                  cout << "\n\n";
                 }
        void data()
        {
                infile.open("car1.txt",ios::in);
              //cout<<"No Class Buying Maint Doors Persons "
                 //<<"Lug_boot Safety\n\n";
                for(i=0;i<BARIS;i++)
				{
                                infile>>oldCases[i][0];
								infile>>oldCases[i][1];
								infile>>oldCases[i][2];
								infile>>oldCases[i][3];
								infile>>oldCases[i][4];
								infile>>oldCases[i][5];
                                infile>>oldCases[i][6];
				}
                //Knowledge base output
                /*for(i=0;i<10;i++)
                { number=i+1;
                                cout<<setw(4)<<setiosflags(ios::left)<<number
                                      <<oldCases[i][0]<<"     "
                                      <<oldCases[i][1]<<"     "
                                      <<oldCases[i][2]<<"     "
                                      <<oldCases[i][3]<<"     "
                                      <<oldCases[i][4]<<"     "
                                      <<oldCases[i][5]<<"     "
                                      <<oldCases[i][6]<<endl;
                }
                cout<<"Count = "<<count;*/
        }

Recommended Answers

All 4 Replies

The last column in that file is a string which also must be read

int i = 0;
string stuff;
while( infile >> oldcases[i][0])
{
    for(int j = 1; j < 6; j++)
          infile >> oldCases[i][j];
    infile >> stuff; // read the strings at the end of each line
    ++i;
}

hi , can you tell me that code is put in which part of my code? cause when i put there have the error, then i add #include <string> . and modify some code like
char string stuff;
still got two error occur .

C:\Program Files\Microsoft Visual Studio\MyProjects\CBR1\heart.cpp(19) : error C2146: syntax error : missing ';' before identifier 'stuff'
C:\Program Files\Microsoft Visual Studio\MyProjects\CBR1\heart.cpp(19) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

http://www.mediafire.com/imageview.php?quickkey=z1injwwzmjm
http://www.mediafire.com/?iwwzemmzvor

Its better to just attach the source code to your post here than to post links to some jpeg file. To do that, just click the "Go Advanced" button then on the next screen "Manage Attachments".

If you want to use the code I posted then delete aht loop just above it. Either that, or ignore the code I posted and just add infile >> stuff; after reading the last integer so that your program also reads the string at the end of each line.

#include "heart.h"
#include <iostream.h>
#include <fstream.h>
#include <math.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>


#define LAJUR 6
#define BARIS 1036

void data();
int count=0, number,i,j;
char string stuff;
char k;
double newcase[LAJUR];
double localsim[BARIS][LAJUR];

int oldCases[BARIS][LAJUR+1];

fstream infile;
fstream outfile;

void main(){
    data();
        cout << "\n\n*************************************";
        cout << "\n*                                   *";
        cout << "\n*        Car Classify System        *";
        cout << "\n*                                   *";
        cout << "\n*************************************";
        cout << "\n\n";
        //INPUT NEW CASE
        
        cout << "\n\n1st data : Buying --> v-high[1], high[2], med[3], low[4]\n"
                << "2nd data : Maint --> v-high[1], high[2], med[3], low[4]\n"
                << "3rd data : Doors --> 2,3,4,5-more [5]\n"
                << "4th data : Person --> 2,4,more [5]\n"
                << "5th data : Lug_boot --> big[1], med[2], small[3]\n"
                << "6th data : Safety --> high[1], med[2], low[3]\n\n";
        for(i=0;i<LAJUR;i++){
                cout << "Insert data " <<i+1 <<":\n";
                cin >> newcase[i];
        }
        cout << "New Case: {";
        for(i=0;i<LAJUR;i++){
                cout << newcase[i] <<",";
        }
        cout << "}\n\n";
        
        //CALCULATE WEIGHT
        
        int weight[LAJUR]={1,1,1,1,1,1};
        int sumweight=0;
        double normWeight[LAJUR]={0,0,0,0,0,0};
        
        for(i=0;i<LAJUR;i++){
                sumweight= sumweight + weight[i];
        }
        cout << "sumweight: " << sumweight << "\n";
        for(i=0;i<LAJUR;i++){
                normWeight[i] = double(weight[i])/double(sumweight);
        }
        
        //CALCULATE LOCAL SIMILARITY
        
        for(i=0;i<BARIS;i++){
                for(j=1;j<LAJUR+1;j++)
                {
                                if(newcase[j-1]==oldCases[i][j]){
									localsim[i][j]=1;
								}
                                else{
                                 localsim[i][j]=0;
                                }
                }
        }
        //CALCULATE GLOBAL SIMILARITY
       double globalsim[BARIS]={0,0,0,0,0,0};
        cout << "\n\n*************************************";
        cout << "\n*                                   *";
        cout << "\n*        Global Similarity          *";
        cout << "\n***************************************";
        cout << "\n\nGlobal Similarity    " << "Class\n";
        
        for (i=0;i<BARIS;i++){
                for(j=0;j<LAJUR;j++){
                 globalsim[i] = (globalsim[i] + localsim[i][j+1]*normWeight[j]);
        }
        globalsim[i]= globalsim[i]*100;
         //cout << "\n";
         //cout << globalsim[i] << "% "<< oldCases[i][0];
        }
        for(i=0;i<10;i++){
                cout << "\n";
                cout << globalsim[i] << "% " << oldCases[i][0];
        }
        
        //SORT SIMILARITY RESULT
        double temp;
        double tempClass[BARIS];
        for(i=0;i<BARIS;i++){
                tempClass[i]=oldCases[i][0];
        }
        for(i=0;i<BARIS;i++){
                for(j=0;j<BARIS;j++){
                 if(globalsim[j]<globalsim[j+1]){
                  temp=globalsim[j];
                  globalsim[j]=globalsim[j+1];
                  globalsim[j+1]=temp;
                  
                  temp=tempClass[j];
                  tempClass[j]=tempClass[j+1];
                  tempClass[j+1]=temp;
                 }
                 else{
                 }
                }
        }
        cout << "\n\n*************************************";
        cout << "\n*                                   *";
        cout << "\n*        Sorted Similarity Result   *";
        cout << "\n*                                   *";
		cout << "\n*************************************";
        cout << "\n\nGlobal Similarity (Sort) "<<"Class\n\n";
		
        for(i=0;i<10;i++){
                cout << globalsim[i] << "% " << tempClass[i] <<"\n";
        }
        //MAJORITY VOTING
        int class1 =0,class2=0,class3=0,class4=0;
        cout << "\n\n*************************************";
        cout << "\n*                                   *";
        cout << "\n*        Majority Voting            *";
        cout << "\n*                                   *";
        cout << "\n*************************************";
        cout << "\n\nGlobal Similarity "<<" Class\n\n";
        for(i=0;i<10;i++){
                if(globalsim[i] > 80.00)
                { 
					cout << globalsim[i] << "% " << tempClass[i] << "\n";
                 {                 if(tempClass[i]==1)
                                      class1=class1+1;
                                      else if (tempClass[i]==2)
                                      class2=class2+1;
                                      else if (tempClass[i]==3)
                                      class3=class3+1;
                                      else if (tempClass[i]==4)
                                      class4=class4+1;
                 }
                }
               }
               int tempValue;
               int array[4];
               array[0]=class1;
               array[1]=class2;
               array[2]=class3;
               array[3]=class4;
               
               for(i=0;i<4;i++){
                for(j=0;j<4;j++){
                 if(array[i] <= array[j])
                 {
                  tempValue=array[i];
                   array[i]=array[j];
                   array[j]=tempValue;
                  }
                 }
                }
                //PRINT RESULT
                cout << "\n\n";
                cout<<"Class 1 ="<< class1<<endl;
                cout<<"Class 2 ="<< class2<<endl;
                cout<<"Class 3 ="<< class3<<endl;
                cout<<"Class 4 ="<< class4<<endl;
                
                cout << "\nThe class for the new case is ";
                 if(array[3]==class1)
                                  cout << "v-good"<<endl;
                 else if(array[3]==class2)
                                  cout << "good"<<endl;
                 else if(array[3]==class3)
                                  cout << "acc"<<endl;
                 else if(array[3]==class4)
                                  cout << "unacc"<<endl;
                 else {
                                  cout << " Sorry, system cannot define the car's class!";
                                  
                 }
                                  cout << "\n\n";
                 }
        void data()
        {
                infile.open("car1.txt",ios::in);
              //cout<<"No Class Buying Maint Doors Persons "
                 //<<"Lug_boot Safety\n\n";
                for(i=0;i<BARIS;i++)
				{
                                infile>>oldCases[i][0];
								infile>>oldCases[i][1];
								infile>>oldCases[i][2];
								infile>>oldCases[i][3];
								infile>>oldCases[i][4];
								infile>>oldCases[i][5];
                                infile>>oldCases[i][6];
				
				while( infile >> oldcases[i][0])
				{
					 
				for(int j = 1; j < 6; j++)
				infile >> oldCases[i][j];
				infile >> stuff; // read the strings at the end of each line
				++i;
				}
				}
				
                //Knowledge base output
                /*for(i=0;i<10;i++)
                { number=i+1;
                                cout<<setw(4)<<setiosflags(ios::left)<<number
                                      <<oldCases[i][0]<<"     "
                                      <<oldCases[i][1]<<"     "
                                      <<oldCases[i][2]<<"     "
                                      <<oldCases[i][3]<<"     "
                                      <<oldCases[i][4]<<"     "
                                      <<oldCases[i][5]<<"     "
                                      <<oldCases[i][6]<<endl;
                }
                cout<<"Count = "<<count;*/
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.