13 Replies - 1392 Views - Last Post: 03 February 2010 - 02:14 PM Rate Topic: -----

#1 th3kingdom1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-February 10

Fstream get data from notepad. why no calculation occur?

Posted 03 February 2010 - 03:09 AM

Need get data from car1.txt file and calculate the percentage of car. but no calculations occur. Can any body help me understand the code like write comment beside it. Cause i not so know the code also. TQ for view.
#include <iostream.h>
#include <fstream.h>
#include <math.h>
#include <iomanip.h>
#include <conio.h>


#define column 6
#define line 1036

void data();
int count=0, number,i,j;
double temp,sum=0,count1=0;
double newcase[line];
double localsim[line][column];
int oldCases[line][column+1];

ifstream infile;
ifstream 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<column;i++){
                cout << "Insert data " <<i+1 <<":\n";
                cin >> newcase[i];
        }
        cout << "New Case: {";
        for(i=0;i<column;i++){
                cout << newcase[i] <<",";
        }
        cout << "}\n\n";
        
        //CALCULATE WEIGHT
        
        int weight[column]={1,1,1,1,1,1};
        int sumweight=0;
        double normWeight[column]={0,0,0,0,0,0};
        
        for(i=0;i<column;i++){
                sumweight= sumweight + weight[i];
        }
        cout << "sumweight: " << sumweight << "\n";
        for(i=0;i<column;i++){
                normWeight[i] = double(weight[i])/double(sumweight);
        }
        
        //CALCULATE LOCAL SIMILARITY
        
        for(i=0;i<line;i++){
                for(j=1;j<column+1;j++)
                {
                                if(newcase[j-1]==oldCases[i][j]){
									localsim[i][j]=1;
								}
                                else{
                                 localsim[i][j]=0;
                                }
                }
        }
        //CALCULATE GLOBAL SIMILARITY
       double globalsim[line]={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<line;i++){
                for(j=0;j<column;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[line];
        for(i=0;i<line;i++){
                tempClass[i]=oldCases[i][0];
        }
        for(i=0;i<line;i++){
                for(j=0;j<line;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("c:\\car1.txt",ios::in);
			
              //cout<<"No Class Buying Maint Doors Persons "
                // <<"Lug_boot Safety\n\n";
                for(i=0;i<line;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;*/
        }                                           


Attached File(s)

  • Attached File  car1.txt (51.47K)
    Number of downloads: 55

This post has been edited by th3kingdom1: 03 February 2010 - 12:58 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Fstream get data from notepad. why no calculation occur?

#2 carltech  Icon User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • Posts: 997
  • Joined: 19-October 07

Re: Fstream get data from notepad. why no calculation occur?

Posted 03 February 2010 - 06:12 AM

you wrote the code but you can't under stand it?

you data() function takes in numbers from the file but does no math or anything.
all the data function seems to do is take numbers from the file and display them in the screen.
Was This Post Helpful? 0
  • +
  • -

#3 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Fstream get data from notepad. why no calculation occur?

Posted 03 February 2010 - 06:24 AM

View Postcarltech, on 03 February 2010 - 05:12 AM, said:

you wrote the code but you can't under stand it?

you data() function takes in numbers from the file but does no math or anything.
all the data function seems to do is take numbers from the file and display them in the screen.


I read this post earlier and I thought the same thing.lol
Was This Post Helpful? 0
  • +
  • -

#4 th3kingdom1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-February 10

Re: Fstream get data from notepad. why no calculation occur?

Posted 03 February 2010 - 11:01 AM

I am doing the research about the case based reasoning method, this code is our university lecture give one and tell us understand and modify the code. Maybe got some error in get data, so not calculate number show out. Any one know modify and go out the result ? please...
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Fstream get data from notepad. why no calculation occur?

Posted 03 February 2010 - 11:26 AM

View Postth3kingdom1, on 03 February 2010 - 01:01 PM, said:

I am doing the research about the case based reasoning method, this code is our university lecture give one and tell us understand and modify the code. Maybe got some error in get data, so not calculate number show out. Any one know modify and go out the result ? please...



Wait... your professor gave you this? That is outrageous! This code is a HORRIBLE example! Its terrible code. If this is what your school is teaching you to program like you need to find a better school. This will not help you become a successful programmer. You will need to forget most of the things you learn in this class.


However I will look into it for you...

@carltech -- data() does not need to return anything because this poor excuse for an example program uses global variables. --- I wonder if there is a goto in there as well, maybe a gets()... I wonder if we can count how many things are wrong with this code the the OP can turn that into the professor. Maybe the professor will retire...
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Fstream get data from notepad. why no calculation occur?

Posted 03 February 2010 - 11:32 AM

as I look at it I realize... I have no idea what it is SUPPOSED to do -- so I have no way to help... what does this program do?
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6035
  • View blog
  • Posts: 23,418
  • Joined: 23-August 08

Re: Fstream get data from notepad. why no calculation occur?

Posted 03 February 2010 - 11:42 AM

Nick, this is obviously just thrown-together found code. Compare comments, variable names, etc.
Was This Post Helpful? 0
  • +
  • -

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Fstream get data from notepad. why no calculation occur?

Posted 03 February 2010 - 11:55 AM

IT really is totally illogical -- the comments don't seem to match what whatever is going on... which I can't even figure out. What do cars and hearts have to do with one another. If this is a "implementation of the heart class." where is the heart class? why is the last comment: "Construction/Destruction" -- I don't see a class let alone a constructor or deconstructor.

With the exception of SpectateSwamp's Desktop Search this is the worst program I have seen in a long time. Strangely enough the programmer here did not resort to any goto's and did not use gets()... So it surly can't be the worst C++ program I have seen...
Was This Post Helpful? 0
  • +
  • -

#9 th3kingdom1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-February 10

Re: Fstream get data from notepad. why no calculation occur?

Posted 03 February 2010 - 12:40 PM

That heart file name is me give the name one..so sorry, i'm just the beginner of this c++ programming. But i major use the java programming. And i borrow many c++ fundamental textbook to read. But the lecturer want us use the c++, really no idea what the code talking about. From the code lajur is means column and the baris is means line. One more, i not belong one part of copy and paste of generation. I try understanding what the code doing and why no percent go out only?

This post has been edited by th3kingdom1: 03 February 2010 - 01:02 PM

Was This Post Helpful? 0
  • +
  • -

#10 th3kingdom1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-February 10

Re: Fstream get data from notepad. why no calculation occur?

Posted 03 February 2010 - 12:57 PM

Hi NickDMax, this program the car classify system is an intelligence system which applying case based reasoning approach. Use to classify the car class whether they are unacc, acc , good or very good. acc= accurate~
By calculate the similarities with the new data with the history data in the data base. New data is key in by the user . The history data is the old data get from the car1.txt file.

This post has been edited by th3kingdom1: 03 February 2010 - 12:58 PM

Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Fstream get data from notepad. why no calculation occur?

Posted 03 February 2010 - 01:09 PM

Tell you what. If you know java then lets just forget about this code and write our own because that code is garbage.


ok... so the cars.txt contains 7 columns of numbers. What do those numbers represent?

Column 1 is: ?
Column 2 is: ?
Column 3 is: ?
Column 4 is: ?
Column 5 is: ?
Column 6 is: ?
Column 7 is: ?
Was This Post Helpful? 0
  • +
  • -

#12 th3kingdom1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-February 10

Re: Fstream get data from notepad. why no calculation occur?

Posted 03 February 2010 - 01:21 PM

the column 1 untill 7 is represent by the word and i replace the word by other number.
My link
And another link as below is the further explain.
My link
Was This Post Helpful? 0
  • +
  • -

#13 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Fstream get data from notepad. why no calculation occur?

Posted 03 February 2010 - 01:40 PM

I have managed to compile and run it - including reading from the cars1.txt file. I agree with you NickDMax, the code is appauling and if this has been written by a professor, we should perhaps get him to join us here. He could maybe learn a thing or two.

@th3kingdom1 honestly, I do not comprehend what you can benefit from with this exercise except how not to write programs!

This post has been edited by Martyn.Rae: 03 February 2010 - 01:43 PM

Was This Post Helpful? 0
  • +
  • -

#14 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Fstream get data from notepad. why no calculation occur?

Posted 03 February 2010 - 02:14 PM

Here is something to get you started.

This creates a class called "CarEvaluation" which encapsulates a single evaluation. I have written all the logic needed to load the data into memory and have shown how to make a function to determine similarity between two reports (based upon the code you posted above).

If you are a Java programmer this program should look much more familiar to you than the code example you gave. You should be able to complete the program but as you run into troubles feel free to ask questions. Note that the logic is up to you... we are not here to do your homework for you... I have really already done too much of the program for you.


#include <iostream>
#include <fstream>
#include <vector>

/**
 *  CarEvaluation -- a class that encapsulates a record of a car evaluation.
 *  Basic class coded by NickDMax (at) DreamInCode
 */
class CarEvaluation {
    int popularity;
    int maintenance;
    int doors;
    int seats;
    int bootcap;
    int safety;
    int classrate;
    
    public: 
    //default constructor.
    CarEvaluation() : 
        popularity(0),
        maintenance(0),
        doors(0),
        seats(0),
        bootcap(0),
        safety(0),
        classrate(0) { }

    //member constructor
    CarEvaluation(int pop, int maint, int door, int seat, int boot, int safe, int rate) : 
        popularity(pop),
        maintenance(maint),
        doors(door),
        seats(seat),
        bootcap(boot),
        safety(safe),
        classrate(rate) { }
 
    //calculate the number or report items that are the same...
    getSimilarity(const CarEvaluation otherCar) {
        return (popularity == otherCar.popularity) +
               (maintenance == otherCar.maintenance) +
               (doors == otherCar.doors) +
               (seats == otherCar.seats) +
               (bootcap == otherCar.bootcap) +
               (safety == otherCar.safety) +
               (classrate == otherCar.classrate);
    }
    
    friend std::ostream &operator<<(std::ostream& out, const CarEvaluation &car);
};

std::ostream &operator<<(std::ostream& out, const CarEvaluation &car) {
    out << "Popularity:     " << car.popularity << "\n"
        << "Maintenance:    " << car.maintenance << "\n"
        << "No. Doors:      " << car.doors << "\n"
        << "No. Seats:      " << car.seats << "\n"
        << "Boot capacity:  " << car.bootcap << "\n"
        << "Safety rating:  " << car.safety << "\n"
        << "Classification: " << car.classrate << "\n\n";
    return out;
}

std::istream& operator>>(std::istream &in, CarEvaluation &carEval) {
    int popularity;
    int maintenance;
    int doors;
    int seats;
    int bootcap;
    int safety;
    int classrate;
    in >> popularity
        >> maintenance
        >> doors
        >> seats
        >> bootcap
        >> safety
        >> classrate;
    carEval = CarEvaluation(popularity, maintenance, doors, seats, bootcap, safety, classrate);
    return in;
} 

std::vector<CarEvaluation> &loadCarDatabase(const char* filename, std::vector<CarEvaluation> &db) {
    ifstream infile;
    infile.open(filename);
    if(infile) {
        while(infile.good()) {
            CarEvaluation newRecord;
            if(infile >> newRecord) {
                db.push_back(newRecord);
            }
        }
        infile.close();
    } else {
        std::cerr << "\nCould not open file: " << filename << std::endl;
    }
    return db;
}




using namespace std;

int main() {
    vector<CarEvaluation> database;
    loadCarDatabase("cars.txt", database);
    for(size_t i = 0; i < database.size(); ++i) {
        cout << "Report #" << i+1 << "\n-------------------------" << endl;
        cout << database[i] << endl;
    }
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1