Thread: Im confused why this isnt working

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    19

    Im confused why this isnt working

    Code:
    main()
    {
        UserOutput(-1);
        GetALine();
        UserOutput(7);
        UserOutput(-2);
    }
    int UserOutput(int action)
    {
      if(action==-1)
      {
          printf("Hello How are you doing today");
          return 0;
    
    
      }
      else if(action==-2)
      {
          printf("Good bye see you soon");
          return 0;
      }
      else if (action>0)
      {
        GetALine(action);
      }
    }
    
    
      int GetALine(char str[])
    {
      int i=0;
    
    
        while(str[i]!='\n')
       {
            i++;
       }
    
    
        return i;
    }
    It works for the first part of my program than after it finishes the first useroutput nothing else works please explain it too me im confused I thought it should work my computer says error program stopped working

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You are experiencing something called undefined behavior. You're calling GetALine with no parameters in main(), but it expects one. It's probably crashing.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    also you pass a integer to the function GetAline when it expects a char array.

    Did you place the function prototypes above or is this the exact same code you compile?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    In addition, you're also calling "GetALine()" in your "UserOutput()" function and passing an integer instead of a char[].

    And why does a function called "GetALine()" seem to be counting the length of a string (with the assumption that it terminates with a newline instead of '\0')? Based on the name, I would assume that function is supposed to get a line of text.

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    19
    This is the code I compiled The first function is main the second is useroutput which displays welcome and good bye messages as well as getting the length of the user output.

  6. #6
    Registered User
    Join Date
    Mar 2014
    Posts
    19
    no it doesnt get a line of text it gets the user input and counts the length of that

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    A development process

    Start small. Get one function working correctly (compiled and tested). Then add the next one. Build up gradually, testing along the way.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by lavaboy View Post
    no it doesnt get a line of text it gets the user input and counts the length of that
    It doesn't get any user input in that function.

  9. #9
    Registered User
    Join Date
    Apr 2014
    Posts
    6
    Does main() require a return(0);

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    Quote Originally Posted by lavaboy View Post
    This is the code I compiled The first function is main the second is useroutput which displays welcome and good bye messages as well as getting the length of the user output.
    try reading this link

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. not sure why my loop isnt working....
    By cprogrammer1980 in forum C Programming
    Replies: 3
    Last Post: 03-11-2011, 12:41 PM
  2. Ummm, why isnt this working?
    By gp364481 in forum C Programming
    Replies: 5
    Last Post: 04-13-2010, 08:39 AM
  3. EOF isnt working....
    By i_can_do_this in forum C Programming
    Replies: 2
    Last Post: 07-10-2006, 09:58 AM
  4. If statement isnt working
    By Extropian in forum C Programming
    Replies: 4
    Last Post: 08-08-2005, 09:21 AM
  5. why isnt my switch working
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-21-2002, 12:08 PM