Thread: This is going to be very easy, loop back to main?

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    21

    This is going to be very easy, now pointers?

    ok so I have a program its main function is

    int main(int argc, char *argv[])

    its the only function and when it gets to the bottom it closes. However I want it to go back to the top again. I just dont know how to call it.

    If you haven't guessed I'm a super C novice.

    Thanks for your help
    Last edited by mrbump2004; 12-05-2004 at 09:11 AM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    int main(int argc, char *argv[])
    {
      for(;;)
      {
        //all the stuff for your main() function goes here
      }
    
      return 0;
    }
    That will make your main() function infinitely loop.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    21
    There you go, knew it would be easy for someone, thanks a lot
    Last edited by mrbump2004; 12-05-2004 at 09:13 AM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >char str1[] = "";
    This allocates memory for an array of size 1.

    >scanf ("%s",str1);
    This assumes that there's enough memory for whatever the user types. If you type anything at all then you don't have enough memory. A better solution for the declaration is:
    Code:
    char str1[50] = "";
    Then you can do this for scanf:
    Code:
    scanf("%49s", str1);
    Now it's safer and won't seg fault.

    >char str3[] = "";
    This also allocates memory for an array of size 1.

    >fgets(str3, 1000, fp);
    This is evil incarnate. Since str3 only has room for the null terminator, asking fgets to write at most 999 characters plus a null character is begging for Bad Things to happen. If you want 999 characters, declare str3 to allow it:
    Code:
    char str3[1000] = "";
    Then you can call fgets using sizeof so that if the array size changes, you don't have to change the call to fgets:
    Code:
    fgets(str3, sizeof str3, fp);
    >strcat(str1, str2);
    >strcat(str1, str3);
    str2 doesn't change, so you need to add 1 to the size of str1 so that it can hold what you're trying to put into it. str3 is much bigger, so now you need to change all of your array sizes to account for long strings so that str3 doesn't kill the program when you try to strcat a 999 character string into a 50 character buffer.

    [edit]
    Wow, I answer a post only to find that the question is edited away.
    [/edit]
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    21
    thanks for that. i created another thread for it, sorry

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    21
    Thanks it gets rid of the segmentation fault, however if i type any number of words, like

    "Hello my name is Jon" it only take the word hello, am i using the wrong thing fgets?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    scanf will stop at the first whitespace, fgets will stop at the end of the line. Keep that in mind when you choose which to use.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    21
    So how do I take input from a keyboard for a whole line

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    fgets( buff, BUFSIZ, stdin );

  10. #10
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Why did you delete all your code in here?

  11. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    21
    I posted it as reply then decided it needed its own thread, someone had replied by then though, sorry

  12. #12
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    No problem, just lettin' you know.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help on main()
    By braddy in forum C Programming
    Replies: 9
    Last Post: 03-21-2008, 11:16 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. int main (), main (), main (void), int main (void) HELP!!!
    By SmokingMonkey in forum C++ Programming
    Replies: 7
    Last Post: 05-31-2003, 09:46 PM
  4. My Memory Game
    By jazy921 in forum C Programming
    Replies: 0
    Last Post: 05-05-2003, 05:13 PM
  5. how to loop back for this code ?
    By Jasonymk in forum Windows Programming
    Replies: 3
    Last Post: 02-28-2003, 12:40 PM