1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

why it doesn't works right?

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

  1. aVague

    aVague New Member

    Joined:
    May 2, 2007
    Messages:
    34
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    forex
    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

    Joined:
    Oct 16, 2007
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    May 2, 2007
    Messages:
    34
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    forex
    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

    Joined:
    Oct 23, 2007
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Aug 3, 2007
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    scanf("%s",&z);
    This statement will lead illlegle memory access also.
     

Share This Page