C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-15-2004, 04:34 PM   #1
Registered User
 
Join Date: May 2004
Posts: 6
Question Problem with Printing message

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);
    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. Why could this happen??? I would very much appriciated if somebody could help me for this. Thank you.
robert_sun is offline   Reply With Quote
Old 05-15-2004, 04:38 PM   #2
Just Lurking
 
Dave_Sinkula's Avatar
 
Join Date: Oct 2002
Posts: 4,990
fflush(stdout)?
__________________
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Dave_Sinkula is offline   Reply With Quote
Old 05-16-2004, 02:09 PM   #3
Registered User
 
Join Date: May 2004
Posts: 6
nop, still doesn't work. Only the the perror() was called and error message printed out before the user input, the printf() still doesn't work properly
robert_sun is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 08:47 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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