1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

why it doesn't works right?

Discussion in 'C' started by aVague, Nov 5, 2007.

  1. aVague

    aVague New 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();
     }
    
     
  2. Lief Webster

    Lief Webster New 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).
     
  3. aVague

    aVague New 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
     
  4. dharmaraj.guru

    dharmaraj.guru New 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 |||
     
  5. listendinesh

    listendinesh New Member

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

Share This Page