error =(

This is a discussion on error =( within the C Programming forums, part of the General Programming Boards category; i keep on getting an error and i dont know what it meanz.... =( can anyone please help Code: #include ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    227

    error =(

    i keep on getting an error and i dont know what it meanz.... =( can anyone please help

    Code:
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct data{
          char fname[25];
          char lname[25];
          char fnumb[10];
       }ph;
    
     char q;
    
    int main()
     {
    
    
     do
      {
    
      FILE *info;
    
          info = fopen("ph","w");
    
        if( info == NULL )
    {
        printf("Fatal Error. Could not open file for writing.\n");
        exit( EXIT_FAILURE );
    }
    
        printf("\nType in the first and last one of the person\n");
        printf("please add a space when typing the last name\n");
        scanf("%s %s",&ph.fname, &ph.lname);
        puts("\n\n");
    
        printf("Please type in the persons phone number\n\n");
        scanf("%s",&ph.fnumb);
        puts("\n");
    
        fprintf(info,"Name: %s  %s\n",ph.fname, ph.lname);
        fprintf(info,"Phone#:  %s\n\n",ph.fnumb);
    
     fclose(info);
       }
        while(ph != 'q');
        q = tolower(q);
        if (q)
          {
          printf("\nexiting");
          return 0;
    
      }
     }
    thanx

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    - Your FILE* variable is declared inside the do/while loop. You'll get multiple declarations if you go through this loop more than once.

    - while(ph != 'q');
    ph is a structure. You can't simply compare it to a character.

    >> q = tolower(q);
    You haven't initialized q yet.

    >> fclose(info);
    You should probably close the file at the end of your code not inside of your loop. Otherwise you'll just be overwriting your file and only get values from your last loop.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    ok thanx but im not that advance in c yet.. but im tring hard.. i have tried diffrent ways and im still getting an error .. see what im tring to do is make it look untill you press q to quit... but it seems that my knowledge is not enough yet =(

  4. #4
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Modify this section
    Code:
     fclose(info);
       }
        while(ph != 'q');
        q = tolower(q);
        if (q)
          {
          printf("\nexiting");
          return 0;
    
      }
     }
    to:

    Code:
          printf("Press q to quit or any other key to continue\n");
          scanf("%c", &q);
          q = tolower(q);
       }while(q != 'q');
    
       printf("\nexiting");
       return 0;
    }
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    damn i did what you told me to do but., damn i have no luck.. please take a look....


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct data{
          char fname[25];
          char lname[25];
          char fnumb[10];
       }ph;
    
     char q;
    
    int main()
     {
    
    
     do
      {
    
      FILE *info;
    
          info = fopen("ph","w");
    
        if( info == NULL )
    {
        printf("Fatal Error. Could not open file for writing.\n");
        exit( EXIT_FAILURE );
    }
    
        printf("\nType in the first and last one of the person\n");
        printf("please add a space when typing the last name\n");
        scanf("%s %s",&ph.fname, &ph.lname);
        puts("\n\n");
    
        printf("Please type in the persons phone number\n\n");
        scanf("%s",&ph.fnumb);
        puts("\n");
    
        fprintf(info,"Name: %s  %s\n",ph.fname, ph.lname);
        fprintf(info,"Phone#:  %s\n\n",ph.fnumb);
    
        printf("Press q to quit or any other key to continue");
        scanf("%c",&q);
        q = tolower(q);
        }
           while(q != 'q');
          printf("\nexiting...");
          sleep(1);
    
        fclose(ph);
          printf("Good bye");
          return 0;
    
     }

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    19
    I dont know if this is what u wanted but i have it so that it takes in the input but it deosnt seem to quit when u press q for some reason. But here it is. U werent closing the file properly, it should have been fclose (info) not fclose (ph).

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct data{
          char fname[25];
          char lname[25];
          char fnumb[10];
       }ph;
    
     char q;
    
    int main()
     {
    
      FILE *info;
     do
      {
    
    
    
          info = fopen("ph","w");
    
        if( info == NULL )
    {
        printf("Fatal Error. Could not open file for writing.\n");
        exit( EXIT_FAILURE );
    }
    
        printf("\nType in the first and last one of the person\n");
        printf("please add a space when typing the last name\n");
        scanf("%s %s",&ph.fname, &ph.lname);
        puts("\n\n");
    
        printf("Please type in the persons phone number\n\n");
        scanf("%s",&ph.fnumb);
        puts("\n");
    
        fprintf(info,"Name: %s  %s\n",ph.fname, ph.lname);
        fprintf(info,"Phone#:  %s\n\n",ph.fnumb);
    
        printf("Press q to quit or any other key to continue");
        scanf("%c",&q);
        q = tolower(q);
    
        fclose(info);
        }
           while(q != 'q');
          printf("\nexiting...");
          sleep(1);
    
    
          printf("Good bye");
          return 0;
    
     }

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    ya that is my problem.. it is not quiting.. im still tring to fix it my self... but like i said im still a newbie... and im tring to learn little by little... but anyways.. thanx.. about the ph part ya i notice that but thanx again.. i hope i can get to fix this.. =)

  8. #8
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Here's the fix. You also had some logic problems besides the quit condition. Take a look at this code and see if you can figure out what you did wrong before.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>
    
    struct data
    {
       char fname[25];
       char lname[25];
       char fnumb[10];
    }ph;
    
    char q;
    
    int main()
    {
       FILE *info;
       info = fopen("ph.txt","w");   
       
       do
       {
          if( info == NULL )
          {
              printf("Fatal Error. Could not open file for writing.\n");
              exit( EXIT_FAILURE );
          }
    
          printf("Type in the first and last one of the person\n");
          printf("please add a space when typing the last name\n");
          scanf("%s %s", &ph.fname, &ph.lname);
    
          printf("\nPlease type in the persons phone number\n");
          scanf("%s", &ph.fnumb);
    
          fprintf(info,"Name: %s %s\n",ph.fname, ph.lname);
          fprintf(info,"Phone#: %s\n\n",ph.fnumb);
    
          printf("\nPress q to quit or any other key to continue\n");
          while (getchar() != '\n');
          scanf("%c",&q);
          q = tolower(q);
       }while(q != 'q');
    
       printf("\nexiting...");
       fclose(info);
       printf("Good bye\n");
       return 0;
    }
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    Great it worked! i see that you have added a new line to the code.. can you tell me what the '\n' means in .. while(getchar() != '\n'); i want to know .. everything... so i can learn better.. thanx

    now i have to make it so that it dont print over the info that i just input hhhmm... thanx again
    Last edited by xlordt; 10-06-2002 at 02:10 AM.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    \n represents a newline character.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    Originally posted by Hammer
    \n represents a newline character.

    hehe damn i have all night tring to ... i got it once but then again... other part of the prog didnt work.. so can you tell me what do you mean by what you said about the \n

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    heh damn all night for this heheh but i figured it out.. all i did was just changed

    Code:
    info = fopen("ph.txt","w");
    
    to
    
    info = fopen("ph.txt","a");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Not important, only a curiosity...
    By BrownB in forum C Programming
    Replies: 35
    Last Post: 12-27-2004, 02:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21