C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-18-2004, 03:42 AM   #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.
robert_sun is offline   Reply With Quote
Old 05-18-2004, 05:05 AM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah 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:49 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