0

I am using an Ipad app called “Code App” with Clang compiler. I want to write an iris dataset program that finds k-means. However, when reading from a file in this app, it does not work.

After checking my code, I tested the same piece of code on VSCode (gcc) and it works. But, I still want to read files from this app.

Could anyone show me what I did wrong or give a solution to how to read files?

file names

#include <iostream>
#include <math.h>
#include <string>
#include <fstream>
#include <sstream>
#include <cstdlib>
using namespace std;
const int n=150;
const int trainn=120;
const int testn=30;
int k;

int testindex[16]{0};

struct irisdata{
    double sl[151]={0};
    double sw[151]={0};
    double pl[151]={0};
    double pw[151]={0};
    string variety[151]={"na"};
};

int rng(){ //randon number generator
    int r=1+(rand()%150);
    return r;
}     

double getdistance(double x1,double y1,double z1,double w1,double x2,double y2,double z2,double w2){
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2)+(w1-w2)*(w1-w2));
} //distance thing

int main(){
    srand((unsigned) time(NULL)); //seed for random number generator

    irisdata data;
    ifstream fin; //reading in data
    fin.open("iris.csv");
    string temp;
    getline(fin,temp,',');//clearing out first row
    getline(fin,temp,',');
    getline(fin,temp,',');
    getline(fin,temp,',');
    getline(fin,temp,',');
    int i=0;
    while(!fin.eof()){
        i++;
        getline(fin,temp,',');
        data.sl[i]=stod(temp);
        getline(fin,temp,',');
        data.sw[i]=stod(temp);
        getline(fin,temp,',');
        data.pl[i]=stod(temp);
        getline(fin,temp,',');
        data.pw[i]=stod(temp);
        getline(fin,temp,',');
        data.variety[i]=temp;
    }
    fin.close();
    //test samples 
    for(int i=1;i<=30;i++){
        while(testindex[i]==0||testindex[i]==testindex[i-1]||testindex[i]>150){
            testindex[i]=rng();
        }
    }



    return 0;
}
henry
  • 9
  • 5

0 Answers0