Thread: Message printing problem

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    6

    Unhappy Message printing problem

    I am having trouble to print a message before user's input from command window.
    I wrote a read() method in the "Instruction.c" file as follow:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "memory.h"
    #include "processor.h"
    
    void read(int address)
    {
      int data = 0;
      printf("Please input the data: \n");
      perror("Please input the data: ");
      scanf("%d", &data);
      writeMemory(address,data);
    }
    It prints a message to ask user to input some figures. However the problem is when I call this method in another file called "processor.c", it doesn't call the printf to print out the message first, then take in the input from the user. Instead, it waited for the user input, after the user input, at the end of the execution of the program, it printed out "Please input the data: \n". The "processor.c" program is like this:
    Code:
    #include <stdio.h>
    #include "memory.h"
    #include "processor.h"
    #include "Instructions.h"
    
    int main()
    {
      char * fileName = "data.txt";
      int flag = loadFile( fileName );
      if ( flag > 0 )
        execute();
      return 0;
    }
    
    int loadFile( char * fileName )
    {
      FILE * fPointer;
      int line = 0;
      int data = 0;
      fPointer = fopen( fileName, "r" );
      if ( fPointer == NULL )
      {
        perror( "File could not be opened\n" );
        return -1;
      }
      else
      {
        while ( fscanf( fPointer, "%d", & data ) == 1 )
        {
          writeMemory( line, data );
          ++line;
        }
        fclose(fPointer);
        fflush(stdout);
        return 1;
      }
    }
    
    void execute( void )
    {
      int instructionRegister = 0;
      int instruction = 0;
      int operand = 0;
      terminator = 0;
      accumulator = 0;
      instructionCounter = 0;
      while ( instructionCounter < SIZE && terminator==0)
      {
        instructionRegister = readMemory( instructionCounter );
        ++instructionCounter;
        instruction = instructionRegister / 100;
        operand = instructionRegister % 100;
        switch ( instruction )
        {
          case READ:
            read( operand );
          break;      default:
            terminator = 1;
            break;
    
        }
      }
    }
    So the read() method is called in the execute() this method, funny thing about this is if I put perror() this function inside the read() method, read() method will print out the error message with Error 0 before the user input, then after the user input, it would then print out the "Please input the data: \n" this message. here is the result after execution:
    Code:
    Please input the data: : Error 0
    23
    Please input the data: : Error 0
    54
    Please input the data: 
    Please input the data: 
    The output is:77
    ***** Simpletron execution terminated *****
    "Please input the data: : Error 0" is the message from the perror() functin, 23 and 54 are the inputs from me, and "Please input the data:" is from the printf() function. I am really puzzled by this as both perror and printf functions are before scanf() function in the read() method, so both of them should be printed before the scanf function, however, in fact, it is one before, and one after. Initially, I was thinking it may be coursed by the input file, so I used fclose() and fflush() as well. But none of them worked. Why could this happen??? I would very much appriciated if somebody could help me for this. Thank you.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop spamming the board with the same post. This has been answered already.
    Quote Originally Posted by Dave_Sinkula
    But since you didn't apparently get the hint, I'll spell it out again:
    Code:
    void read(int address)
    {
      int data = 0;
      printf("Please input the data: \n");
      fflush( stdout );
      perror("Please input the data: ");
      scanf("%d", &data);
      writeMemory(address,data);
    }
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  2. MFC Message Map Problem
    By durban in forum C++ Programming
    Replies: 10
    Last Post: 10-31-2005, 01:55 PM
  3. problem printing with floating number
    By ssharish in forum C++ Programming
    Replies: 4
    Last Post: 01-25-2005, 07:31 PM
  4. problem with UDP WSAAsyncSelect socket, no callback message received???
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-29-2004, 11:59 AM
  5. Printing problem using CDC::StartDoc()
    By LuckY in forum Windows Programming
    Replies: 0
    Last Post: 01-20-2003, 04:48 PM