why it doesn't works right?

Go4Expert Member
5Nov2007,23:47   #1
aVague's Avatar
Code:
#include<stdlib.h>
 #include<stdio.h>
FILE *fp;


 int func_1(int n,double w[],double v[])
  {   int i;
    fp=fopen("in.txt","w+");

    if(fp==NULL) write("can't open");
    return 13;
    
    for(i=0;i<n-1;i++)
    { putchar(w[i]); putchar(v[i]);}

return 7;
    }


void main()
{

  int N,i;
  char z;
  double *oX,*oY;


 printf("what would you like to do now?\n");
 scanf("%s",&z);

switch (z)
{case 'add': printf("\n enter: 1)number of points you wish to add 2)coordinates of points ");
scanf("%d",&N);
oX=(double*)malloc(N*sizeof(double));
oY= (double*)malloc(N*sizeof(double));
for(i=0;i<N-1;i++)
{scanf("%d%d",&oX[i],&oY[i]);printf("\n");i++;}
 func_1(N,oX,oY);
   break;

 case 'q' :break;}
// getchar();
// getchar();
 }
Go4Expert Member
6Nov2007,08:23   #2
Lief Webster's Avatar
Ahh! Where are the spaces in the code? I can't understand it at all. You should make it easier to read and actually tell what the problem with it is (i.e. what it won't do).
Go4Expert Member
6Nov2007,10:13   #3
aVague's Avatar
Code:
#include<stdlib.h>
 #include<stdio.h>

FILE *fp;

 int func_1(int n,double w[],double v[])
  {   
int i;


    fp=fopen("in.txt","w+");
    if(fp==NULL) write("can't open");
    return 13;
    
    for(i=0;i<n-1;i++)
    { 
putchar(w[i]); putchar(v[i]);}
return 7;
    }


void main()
{
  int N,i;
  char z;
  double *oX,*oY;


 printf("what would you like to do now?\n");
 scanf("%s",&z);

switch (z)
{
case 'add': 

printf("\n enter: 1)number of points you wish to add 2)coordinates of points ");
scanf("%d",&N);

oX=(double*)malloc(N*sizeof(double));
oY= (double*)malloc(N*sizeof(double));

for(i=0;i<N-1;i++)
{
scanf("%d%d",&oX[i],&oY[i]);printf("\n");i++;
}

 func_1(N,oX,oY);
   break;

 case 'q' :break;} //not valueable
// getchar();
// getchar();
 }
this should read number of points N, then it reads coordinats of points (oX and oY) , then it opens file
"in.txt" , then it should write this coordinats in this file
Go4Expert Member
6Nov2007,12:20   #4
dharmaraj.guru's Avatar
1. You are receiving string and storing in char variable. Try giving integer number to indicate add operation and quiting from switch.
2.Switch cases cannot be triggered by string. they can be only by integer, char or cost expressions.
3. Close the file in your func_1

Make your program in effect with the above changes.

||| Dharma |||
Go4Expert Member
13Nov2007,11:05   #5
listendinesh's Avatar
Code:
scanf("%s",&z);
This statement will lead illlegle memory access also.