Arrays Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fr0zen
    New Member
    • Dec 2007
    • 6

    Arrays Question

    alright, well ive been having a problem trying to get this code to work,
    i have my three arrays

    char first[80];
    char second[80];
    char third[160];

    First i have to use the fgets() function to get a string from the user and store the data in the array first.. then do the same for the second array.

    next, i have to use the strcmp() function to determine which string is last alphabetically.

    then the strlen() function to determine which string is longest.

    and last use the strcpy() and strcat() functions to take first join it to second and store the result in the array named third.

    I have attempted this, it sounds easy to me, but i cant figure it out, i just need help on this so hopfully it will help me to understand how arrays work alittle better so that i can complete my test and pass.... thanks
  • fr0zen
    New Member
    • Dec 2007
    • 6

    #2
    the output should look similar to

    Enter a city name: Halifax
    Enter another city name: Hamilton
    last alphabetically: Hamilton
    Longest string: Hamilton
    Joined together: HalifaxHamilton

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Have you used these functions before? Do you know how they work?

      All you need to do, you have already described to us. It's very easy to take your description and write a short program.

      Comment

      • fr0zen
        New Member
        • Dec 2007
        • 6

        #4
        not really, i don,t really understand C that well.... i can understand code if i read it, i can say this does that and that does this... but its hard for me to make new code

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          If you don't know how to use the functions, try www.cplusplus.com for looking up what they do. The site also provides several short, practical examples in the function's use. Other than knowing what these functions do, your program will basically write itself, according to that description. It's very clear on exactly what you have to do.

          Comment

          • fr0zen
            New Member
            • Dec 2007
            • 6

            #6
            if someone could help me with the fgets, i can get it to accept anything

            Comment

            • oler1s
              Recognized Expert Contributor
              • Aug 2007
              • 671

              #7
              Originally posted by fr0zen
              if someone could help me with the fgets, i can get it to accept anything
              "can't get it to accept anything" is a vague statement that has no place in programming. I'm guessing you're using the fgets function and either getting compile errors or runtime errors.

              First, check the documentation. Understand precisely how to use fgets in your code. Using Google, you should be able to find plenty of documentation on how to use fgets. Typing in "man fgets" (hint: man <something> is a useful google term) will get you documentation.

              If you think you have written correct code, and still face problems, you need to show us that code, describe to us your observations that make you think your code doesn't work (errors need to copy pasted here, runtime errors mean you can describe what happens when you run the program).

              You need to give us this much information for us to help you properly.

              Comment

              • fr0zen
                New Member
                • Dec 2007
                • 6

                #8
                int main(){
                char first[80];
                FILE *file;
                {
                printf("Enter a city name:");
                fgets(first, 80, file);
                }
                {
                printf("%s", first[80]);
                }
                system("pause") ;
                return 0;}


                hopeless?

                Comment

                • fr0zen
                  New Member
                  • Dec 2007
                  • 6

                  #9
                  i dont understand char *fgets (char *s, int size, FILE *stream);

                  Comment

                  • primeSo
                    New Member
                    • Aug 2007
                    • 35

                    #10
                    Originally posted by fr0zen
                    int main(){
                    char first[80];
                    FILE *file;
                    {
                    printf("Enter a city name:");
                    fgets(first, 80, file);
                    }
                    {
                    printf("%s", first[80]);
                    }
                    system("pause") ;
                    return 0;}


                    hopeless?
                    fgets() used wrongly, in your case, the last argument is wrong. Try to change it to STDIN. Check it out.

                    what do you want to do with the file pointer i.e file? are you sure what you are doing will achieve what you expected? do some reading dude. ^.^

                    Comment

                    Working...