i dont knw y it being overwritten again again

#include<iostream.h>


void return_path(char *a);
char* return_file(char*a,char* ret);


void main(){
char arr[30];
cout<<"enter the path: ";
cin>>arr;
return_path(arr);
char ans[30];
char *res;
res=return_file(arr,ans);
cout<<res<<endl;
}

void return_path(char* a){
int i=0;
int j=0;
char *d[30];
int cuts=0;
char temp[20];
while(a[i]!='\0')
{
if(a[i]==92)
{
temp[j]='\0';
j=0;
i++;
d[cuts]=temp;// add it to the index and move ahead

cout<<"DIR: "<<d[cuts]<<endl;
for(int l=0;l<=cuts;l++)
cout<<d[l]<<endl;
cuts++;
}
else
{
temp[j]=a[i];
j++;
i++;
}
}
d[cuts]='\0';
for( i=0;i<cuts;i++)
cout<<"dir:"<<d[i]<<endl;
}



char* return_file(char* a,char* ret){
int i=0;
int j=0;

char temp[20];
while(a[i]!='\0')
{
if(a[i]==92)
{
temp[j]='\0';
j=0;
i++;

}
else
{
temp[j]=a[i];
j++;
i++;
}
}
temp[j]='\0';
cout<<"NAME: "<<temp;
ret=temp;
return ret;
}


this is my code....second funcion works perfectly but the first function is giving me a hard time....it shows all the dir in the path....... in the func return_path() i have declared an arr of pointers and when i get \ it cuts the string,save it in temp and place it into the pointer array...but when i cout the name it is just over writing temp string on all the indexes???? i dnt knw y it is being happening...
This might help. You should also know that std::cin terminates the string after it reads the first space - i'll let you look into that.

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
void return_path(char* a){
    int i=0;
    char *d[30];
    int cuts=0;
    char temp[30];
    while(a[i]!='\0')
    {
        if(i == 0 && a[i] != 92){
            temp[i]=a[i];
            d[cuts] = temp;
            cuts++;
        }
        else if(a[i]==92)
        {
            temp[i]='\0';
            d[cuts]=temp+i+1;// add it to the index and move ahead
            cuts++;
        }
        else
        {
            temp[i]=a[i];
        }

        i++;
    }
    d[cuts]='\0';
    temp[i] = '\0';
    for( i=0;i<cuts;i++)
        cout<<"dir:"<<d[i]<<endl;
}


Good luck!
Last edited on
@nickPaul
thanx mate!!!!!! GOD bless YA :D
Topic archived. No new replies allowed.