why it doesn't works right?

aVague's Avatar, Join Date: May 2007
Go4Expert Member
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();
 }
Lief Webster's Avatar, Join Date: Oct 2007
Go4Expert Member
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).
aVague's Avatar, Join Date: May 2007
Go4Expert Member
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
dharmaraj.guru's Avatar
Go4Expert Member
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 |||
listendinesh's Avatar, Join Date: Aug 2007
Go4Expert Member
Code:
scanf("%s",&z);
This statement will lead illlegle memory access also.