Hi, I have tested your code and modified it as shown below, I think I am getting
the desired result with N = 2stars
The first change is that name is a char** and I have initialized it to 100 pointers.
xname, c, band are setup as char pointers (assuming they are work variables)
the other changes in the code are
memcpy(name[N],xname,strlen(xname)+1);
which copies xname into name[N] you cant use name[n] = xname,
because it only copies the pointer not the contents.
and same with strcmp(xname,name[J]) == 0
compares the contents and not the pointer
hope this has helped you
Shredded
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include <stdio.h>
#include <string>
#include <iostream>
#include <typeinfo>
using namespace std;
FILE *unit1;
int main()
{
unit1=fopen("calc_bands.tab", "r");
char** name; //CHANGED from here
name = new char*[100];
for (int i=0;i<100;i++)
{
name[i] = new char[32];
}
char* xname = new char[32];
char* c = new char[32];
char* band = new char[32]; // to here
double value, r;
int N, flag, I, J;
N=0;
I=0;
while(!feof(unit1))
{
fscanf(unit1, "%s %s %lg", xname,
band, &r);
if(N==0)
{
N=1;
memcpy(name[N],xname,strlen(xname)+1); // CHANGED
}
flag=0;
for(J=1; J<=N; J++)
{
//problem is this is always true!
if(strcmp(xname,name[J]) == 0) // CHANGED
flag=J;
}
if(flag==0)
{
N++;
name[N]=xname;
flag=N;
}
I++;
}
cout << "done with N = " << N << "stars \n";
fclose(unit1);
return 0;
}
|