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.