Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   why it doesn't works right? (http://www.go4expert.com/forums/doesnt-t7189/)

aVague 5Nov2007 23:47

why it doesn't works right?
 
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 6Nov2007 08:23

Re: why it doesn't works right?
 
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 6Nov2007 10:13

Re: why it doesn't works right?
 
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 6Nov2007 12:20

Re: why it doesn't works right?
 
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 13Nov2007 11:05

Re: why it doesn't works right?
 
Code:

scanf("%s",&z);
This statement will lead illlegle memory access also.


All times are GMT +5.5. The time now is 13:41.