Like Tree3Likes
  • 1 Post By rags_to_riches
  • 1 Post By AndrewHunter
  • 1 Post By anduril462

This is my attempt!

This is a discussion on This is my attempt! within the C Programming forums, part of the General Programming Boards category; <<< Split from dead thread Please read the rules! >>> Hello. I am having the hardest time getting my code ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    6

    This is my attempt!

    <<< Split from dead thread Please read the rules! >>>
    Hello. I am having the hardest time getting my code to compile. My compiler tells me that there are conflicting types for convert and that there is a previous declaration to convert. I have not added any notation to the code as yet. Thanks for any help.

    Code:
    #include<stdio.h>
    #include <stdlib.h>
    #include<string.h>
    
    FILE *getOpen();
    void inOut(FILE *);
    void toUpper(char);
    void convert(char);
    
    int main()
    {
    
      FILE *outFile;
      
      outFile = getOpen();
      inOut(outFile);
      fclose(outFile);
      printf("The File was successuflly written.\n");
      
      void convert(char); 
    
      getch();
      return 0;
    }
    
    FILE *getOpen()
    {
         FILE *fname;
         char name[13];
         
         printf("Enter a file name:  ");
         gets(name);
         fname=fopen(name, "w");
    
      if (fname == NULL)
      {
                printf("\nFailed to open the file %s.\n");
                exit(1);
      }
      return (fname);
    }
    
    void inOut(FILE *fname)
    {
         int count;
         char line[81];
         printf("Please enter five lines of text:\n");
         for (count = 0; count < 5; count++)
         {
             gets(line);
             fprintf(fname, "%s\n", line);
             
         }
    }
    
    void convert(char line[81])
    {
         int i=0;
         
         while (line[i] != '\0');
         {
               line[i] = toUpper(line[i]);
               i++;
         }
               return 0;
    }
    void toUpper(char letter)
    {
         if( letter >= 'a' && letter <= 'z')
         return (letter - 'a' + 'A');
         else
         return (letter);
    }

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Code:
    void convert(char);
    Prototype for a function accepting a single character as an argument.

    Code:
    void convert(char line[81])
    {
         int i=0;
         
         while (line[i] != '\0');
         {
               line[i] = toUpper(line[i]);
               i++;
         }
               return 0;
    }
    Implementation of a function which accepts an array of characters as an argument.
    stahta01 likes this.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Not so good way...
    Code:
    void convert(char line[81])
    {
         int i=0;
         
         while (line[i] != '\0');
         {
               line[i] = toUpper(line[i]);
               i++;
         }
               return 0;
    }
    Better way...
    Code:
    #include <string.h>
    #incldue <ctype.h>
    
    // prototype
    void convert(char *line);
    
    // implementation
    void convert(char *line)
    {
         int i=0;
         while (line[i] != '\0');
           {
              line[i] = toupper(line[i]);
               i++;
           }
         line[i] = 0;
        }
    Arrays decay to pointers when passed into functions.
    toupper() is a standard C-99 library function, no need to reinvent the wheel.
    Last edited by CommonTater; 06-24-2011 at 11:20 AM.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    6
    Thanks. This was very helpful! That pointer stuff gets me every time! I figured out my void prototype was using char instead of char[] as well. There are some really handy tools in the C library function!

    Of course, I have run into a wall with printing the newly uppercased contents of the line[81]. Do I have to use a prototype function or can I just insert a fprints() under main() somehow? When I use the fprints(outFile, "%s", line[81]); my compiler says that it is the first use of line[81] even though the last two functions defined it!

    Code:
    #include<stdio.h>
    #include <stdlib.h>
    #include<string.h>
    
    FILE *getOpen();
    void inOut(FILE *);
    char convert(char *);
    
    int main()
    {
    
      FILE *outFile;
      
      outFile = getOpen();
      inOut(outFile);
      fclose(outFile);
    
      fprtints(outFile, "%s", line[81]);
    
      printf("The File was successuflly written.\n");
      char convert(char []); 
      getch();
      return 0;
    }
    
    FILE *getOpen()
    {
         FILE *fname;
         char name[13];
         printf("Enter a file name:  ");
         gets(name);
         fname=fopen(name, "w");
    
    
      if (fname == NULL)
      {
                printf("\nFailed to open the file %s.\n");
                exit(1);
      }
      return (fname);
    }
    
    void inOut(FILE *fname)
    {
         int count;
         char line[81];
         printf("Please enter five lines of text:\n");
         for (count = 0; count < 5; count++)
         {
             gets(line);
             fprintf(fname, "%s\n", line);
             
         }
    }
    
    char convert(char *line)
    {
         int i=0;
         
         while (line[i] != '\0');
         {
               line[i] = toupper(line[i]);
               i++;
         }
               return 0;
    }
    Last edited by geustuser; 06-24-2011 at 11:36 AM.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Ok, so let's see....
    Code:
    fclose(outFile);  //<----------This closes the file
    
    fprtints(outFile, "%s", line[81]); //<-----------Now you try to write to a closed file with a made up function

    File Ops FAQ
    CommonTater likes this.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by geustuser View Post
    Thanks. This was very helpful! That pointer stuff gets me every time! I figured out my void prototype was using char instead of char[] as well. There are some really handy tools in the C library function!

    Of course, I have run into a wall with printing the newly uppercased contents of the line[81]. Do I have to use a prototype function or can I just insert a fprints() under main() somehow? When I use the fprints(outFile, "%s", line[81]); my compiler says that it is the first use of line[81] even though the last two functions defined it!
    Way wrong...
    Code:
      fprtints(outFile, "%s", line[81]);
    This should work...
    Code:
      fprintf(outFile, "%s\n", line);   // print to file
      printf("%s\n"",line);     // print to screen
    When you keep including the size like that, the compiler thinks you are making a new variable...

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    6

    More tries at printing...

    I had to include char line[81] as a variable in main() to get it to accept the print feature but instead of the five strings of entered text back, I'm getting three nonsense characters. These do change with different inputs as file name and lines of text so it looks like data is being passed to the line[]. I'm thinking maybe a prototype function is the way to go. I tried that out using the line notation but it will not print any of the file. Both methods are in this code... I seriously appreciate your time and help with this!

    Code:
    #include<stdio.h>
    #include <stdlib.h>
    #include<string.h>
    
    FILE *getOpen();
    void inOut(FILE *);
    char convert(char *);
    char textprint(char *);
    
    int main()
    {
    
      FILE *outFile;
      char line[81];
      outFile = getOpen();
      inOut(outFile);
      
      fclose(outFile);
      
      printf("The File was successuflly written.\n");
      char convert(char []); 
      char textprint(char *line);
         
      fprintf(outFile, "%s\n", line);   // print to file
      printf("%s\n",line);
      
      getch();
      return 0;
    }
    
    FILE *getOpen()
    {
         FILE *fname;
         char name[13];
         printf("Enter a file name:  ");
         gets(name);
         fname=fopen(name, "w");
    
    
      if (fname == NULL)
      {
                printf("\nFailed to open the file %s.\n");
                exit(1);
      }
      return (fname);
    }
    
    void inOut(FILE *fname)
    {
         int count;
         char line[81];
         printf("Please enter five lines of text:\n");
         for (count = 0; count < 5; count++)
         {
             gets(line);
             fprintf(fname, "%s\n", line);
         }
    }
    
    char convert(char *line)
    {
         int i=0;
         
         while (line[i] != '\0');
         {
               line[i] = toupper(line[i]);
               i++;
         }
               return 0;
    }
    
    char textprint(char *line)
    {
         int i=0;
         
         while (line[i] != '\0');
         {
               printf("%s", line[i]);
               i++;
         }
               return (*line);
    }
    
    FILE *getOpen()
    {
         FILE *fname;
         char name[13];
         printf("Enter a file name:  ");
         gets(name);
         fname=fopen(name, "w");
    
    
      if (fname == NULL)
      {
                printf("\nFailed to open the file %s.\n");
                exit(1);
      }
      return (fname);
    }
    
    void inOut(FILE *fname)
    {
         int count;
         char line[81];
         printf("Please enter five lines of text:\n");
         for (count = 0; count < 5; count++)
         {
             gets(line);
             fprintf(fname, "%s\n", line);
         }
    }
    
    char convert(char *line)
    {
         int i=0;
         
         while (line[i] != '\0');
         {
               line[i] = toupper(line[i]);
               i++;
         }
               return 0;
    }
    
    char textprint(char *line)
    {
         int i=0;
         
         while (line[i] != '\0');
         {
               printf("%s", line[i]);
               i++;
         }
               return (*line);
    }

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Honestly?

    Code:
      
      [B]fclose(outFile);[B]     YOU ARE CLOSING THE FILE HERE
      
      printf("The File was successuflly written.\n");
      
      char convert(char []); //<---------- WHAT ARE YOU TRYING TO DO HERE
      char textprint(char *line);<---------  AND HERE.....Function Tutorial
         
      fprintf(outFile, "%s\n", line);   // print to file   YOU ARE TRYING TO WRITE TO THE CLOSED FILE STILL
    Look, we all want to help but you are just throwing code around and it doesn't seem like you are trying to learn anything. I suggest starting with the tutorials they have made on this site!!!
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    6
    Sorry, bad post. I changed the order to be before the file is closed as shown below. Still getting three random characters for the printf(). Does the printf() work for what I'm trying to do?

    Code:
    int main()
    {
    
      FILE *outFile;
      char line[81];
    
      outFile = getOpen();
      inOut(outFile);
      char convert(char []);  
    
      fprintf(outFile, "%s\n", line);   // print to file
      printf("%s\n",line);                // print to screen
      
      fclose(outFile);
    
      printf("The File was successuflly written.\n");
     
      getch();
      return 0;
    }

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Quote Originally Posted by geustuser View Post
    I had to include char line[81] as a variable in main() to get it to accept the print feature but instead of the five strings of entered text back, I'm getting three nonsense characters.
    That line variable is different than the one you use in inOut. Since it's a local variable, and you don't initialize it or put any useful data in it, it contains garbage, hence your nonsense characters.

    I'm thinking maybe a prototype function is the way to go.
    I don't know what you mean by this, and I'm not sure you do either. A prototype just tells the compiler about a function (parameters and return type), so it can make sure you use it right. This is only needed if you call the function before you define it (like how you call getOpen in main, but define getOpen after main). The compiler reads from top to bottom and only knows about stuff it's already seen (things above it in the file).

    Code:
    FILE *getOpen();
    void inOut(FILE *);
    char convert(char *);
    char textprint(char *);
    
    
    int main()
    {
    
      FILE *outFile;
      char line[81];
      outFile = getOpen();
      inOut(outFile);
      
      fclose(outFile);
      
      printf("The File was successuflly written.\n");
      char convert(char []); 
      char textprint(char *line);
         
      fprintf(outFile, "%s\n", line);   // print to file
      printf("%s\n",line);
      
      getch();
      return 0;
    }
    Those two red lines are prototypes, not function calls. Take them out. Never put a prototype inside a function. The belong where you have the others, above your functions (green text). Also, as I mentioned, the line variable in main is different from the one in inOut. You need to declare one in main, and pass it in to inOut. Note that line is declared as a single array of chars, thus it can only hold one line at a time. If you need to hold several lines, you need an array of array of chars:
    Code:
    int main()
    {
        char line[5][81];  // declare line to be an array of 5 "lines", each "line" having room for 80 chars plus the terminating null.
        ...
        inOut(outFile, line);  // pass line into inOut
        convert(line[0]);  // converts the first line to upper case, do this for all lines, using a loop
    }
    ...
    void inOut(FILE *fname, char line[][81])
    {
         int count;
         char line[81];
         printf("Please enter five lines of text:\n");
         for (count = 0; count < 5; count++)
         {
             fgets(line[count], 81, stdin);  // gets is bad, see the FAQ
             fprintf(fname, "%s\n", line);
         }
    }
    That should get you going for a bit.
    rags_to_riches likes this.

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    6
    The compiler is getting hung up on the InOut(outFile, line); because it has too many arguments. I made changes to update the array. Not sure if I did that right. Wow, there are so few things in life that make me feel so retarded as programming in C. I really appreciate all the direction I'm getting from your community!

    Code:
    #include<stdio.h>
    #include <stdlib.h>
    #include<string.h>
    
    FILE *getOpen();
    void inOut(FILE *);
    char convert(char *);
    
    int main()
    {
    
      FILE *outFile;
      char line[5][81];
    
      outFile = getOpen();
      inOut(outFile, line);
      convert(line[0]);  
    
      fprintf(outFile, "%s\n", line);   // print to file
      printf("%s\n",line);   // print to screen
      
      fclose(outFile);
    
      printf("The File was successuflly written.\n");
     
      getch();
      return 0;
    }
    
    FILE *getOpen()
    {
         FILE *fname;
         char name[13];
         printf("Enter a file name:  ");
         gets(name);
         fname=fopen(name, "w");
    
    
      if (fname == NULL)
      {
                printf("\nFailed to open the file %s.\n");
                exit(1);
      }
      return (fname);
    }
    
    void inOut(FILE *fname, char line[][81])
    {
         int count;
         char line[][81];
         printf("Please enter five lines of text:\n");
         for (count = 0; count < 5; count++)
         {
             fgets(line[count], 81, stdin);
             fprintf(fname, "%s\n", line);
             
         }
    }
    
    char convert(char *line)
    {
         int i=0;
         
         while (line[i][] != '\0');
         {
               line[i][] = toupper(line[i][]);
               i++;
         }
               return 0;
    }

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Guestuser... stop guessing. Spend some time with the tutorials and manuals, you need to actually learn C...

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Quote Originally Posted by geustuser View Post
    The compiler is getting hung up on the InOut(outFile, line); because it has too many arguments. I made changes to update the array. Not sure if I did that right. Wow, there are so few things in life that make me feel so retarded as programming in C. I really appreciate all the direction I'm getting from your community!
    You clearly didn't learn anything from post #2, and you don't get what a prototype is actually for. You're cutting and pasting our suggestions literally, without thought to how it affects anything, and taking shots in the dark trying to fix it. We don't usually hand out big chunks of working code here, rather we point you in the right direction, and sometimes give you a swift kick in the butt to get you moving. You need to actually think about what you're doing. Until I see evidence of that, I refuse to help.

    As everybody here has been suggesting, work your way through the tutorials we have on this site (if you can't find them, they're here: Cprogramming.com - Programming Tutorials: C++ Made Easy and C Made Easy), and Google around for some more. Also, I'm guessing you're in school, so read your class notes and textbooks. Go through all the sections in order, working through each example and exercise until you totally get it. You need to totally understand functions, arrays and file handling for this assignment, so make sure you really get that.

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    6
    I seriously appreciate the help. Spot on, I'm trolling through tutorials right now. I definitely let this language go over my head. If I have any seriously valid problems I'll let you all know. Great community of people, thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No attempt yet, please do this for me (non-urgent)
    By ryleigh in forum C Programming
    Replies: 2
    Last Post: 02-24-2011, 02:26 PM
  2. My first attempt at a text rpg
    By Bargomm in forum Game Programming
    Replies: 8
    Last Post: 12-26-2007, 07:54 AM
  3. attempt 2
    By cppdungeon in forum C++ Programming
    Replies: 3
    Last Post: 05-27-2005, 07:57 PM
  4. My First Attempt at a Game...
    By Izzy545 in forum Game Programming
    Replies: 20
    Last Post: 12-21-2004, 01:38 PM
  5. Sigmaze! -- Second Attempt.
    By quzah in forum Contests Board
    Replies: 42
    Last Post: 11-09-2004, 05:45 PM

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