Variable initialization in our application, could it cause problems?

This is a discussion on Variable initialization in our application, could it cause problems? within the C Programming forums, part of the General Programming Boards category; Hello all, I am supporting an application that I did not program. I also am only a beginner with C ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    5

    Variable initialization in our application, could it cause problems?

    Hello all,

    I am supporting an application that I did not program. I also am only a beginner with C programming, so my understanding is limited. We have had some unexpected results from the program that I need to fix. Here is the first problem.

    I am programming a PIC18 chip that runs a microcomputer. This microcomputer needs to respond to two distinct situations:

    1. If the computer is reset using the master clear switch, it needs to go through the startup questions with the default answer of 'n'

    2. If the computer is reset using the master clear switch, it needs to accept input from the keyboard to answer a series of questions, all the initial ones are y/n type questions.

    Basically if the first y/n question is not answered, then the computer should assume it is a 'n' and move on. Here is the relevant code:

    After the RS232 port has been opened
    Code:
    printf("Startup Event\r");
    
    printf("would you like to perform a manual startup event (Y/N)? \r");
    
    get_string(download_answer, 4);
    
    if (isamoung(download_answer[0],"Yy")) {
         //Ask the rest of the questions
         //More code that is not relevant....
      } else {
        //This is an auto-startup event, set default variables
        //More code that is not relevant....
    }
    The variable download_answer is not initialized when it is created. When the question "would you like to perform...."
    appears on the screen, sometimes the first time we answer the question a 'y' or 'Y' response will still lead to the program treating the answer as not a 'y' or 'Y' and going to the auto startup event programming. If you reset the computer again, it always recognizes the 'y' or 'Y' response

    My questions are:

    1. Is this because the the download_answer variable is not initialized?

    2. would initializing it to downoad_answer[0] = 'n'; be sufficient to avoid this problem, or do you think I have a bigger issue to deal with here in another portion of the code?

    Thanks!

    Brian

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It depends on how you're reading your input I suspect. Yes, you should initialize all of your variables to 'n', if you want that as your default to that. That works fine. However, it's possible that due to the way you're reading input, you read the first answer, and hit enter, and the second answer's input is taken as the enter key. Which won't evaluate to 'y' in a comparison test, and is thus false.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User fischerandom's Avatar
    Join Date
    Aug 2005
    Location
    Stockholm
    Posts
    71
    I have worked a lot with the PIC and since you said it works sometimes I suggest that you check that all global variables (if you have any) are initialized before you turn on the interrupt with GIE = true;
    P.S. Are you sure it shall be ". . .\r" and not ". . .\r\n"?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    Seems more likely to be that you don't initialise your serial port properly, so you get some 'random' character first before actual valid data.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    5
    Thanks for the comments, I'll be checking these things later today.

    Brian

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compile error about member variable initialization
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 12-04-2007, 10:55 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  4. Variable Declaration & Initialization :: C++
    By kuphryn in forum C++ Programming
    Replies: 6
    Last Post: 12-26-2001, 09:22 AM
  5. Having problems incrementing variable names
    By Legolas in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2001, 09:14 PM

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