kokopo2 0 Newbie Poster

Hi guys, i like to thank all who has helped me so far. I managed to come up with some sort of code, to find the invalid string. e.g -8ha .
However im having problems getting it to print whether is it a invalid string once only. Also if there is a number in the invalid string it will print the number out too.

Also, if it is a negative number, the program will print it as invalid string too.

/*the output is .
Contents of file : 12 3 + * -79 -8ha

12 number

3 number

+ not number

* not number

-79 invalid
invalid
invalid
number

-8ha invalid
invalid
number
*/

i managed to print the positive number and operator correctly,
however i just cant seem to print the negative number and invalid string correctly.
can some1 help pls?
thanks in advance...

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>



int main()
{
  FILE * mFile;
  char string[100]; /* Input line from file           */
  char st[100];     /* Temporary storage for the line */

  char nt[4];
  char *tokenPtr;

  mFile = fopen ("myfile.txt" , "r");
  if (mFile == NULL) { /* If there was a problem, show the error */
    perror ("Error opening file");
  }
  else {
    fgets (string , 100 , mFile);
    printf("The original line: >%s<\n", string);

    strcpy(st, string);

    tokenPtr = strtok(st, " \n");

    while( tokenPtr != …
kokopo2 0 Newbie Poster

Thanks for the help NPH, it has helped me a lot.
i am now having problems trying to get the a variable from my 'public void balloon' method. The variable is array x. I like to call the variable in my 'mouseclick' method but i am unable to do so. An error " x is not public in java.awt.Component; cannot be accessed from outside the package".

I would also like to ask for help or examples in objects and classes. Im thinking of setting the balloon method into another class. But i do not know how to go about in doing so. If possible, how do i access the methods in the newly created balloon class too?

I like to seek some help in this . Thank you.


import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.Timer;





public class Game extends Applet implements MouseListener, ActionListener
{



 // The X-coordinate and Y-coordinate of the last move.
 int xpos, ypos;
 
 int ysrc;
 
 Color fill, outline;


 
 
 Button butt1 = new Button("Start");
 
 private Timer timer;
 
 long time = 60000; //60 seconds
 
 int displayTime, i;
 
 boolean clicked, OnImage;
 
 int HitScore, MissScore;
 
 
 
 int delay = 1000; //milliseconds
  ActionListener timerAction = new ActionListener() {
      
      long x = time - 1000;
      public void actionPerformed(ActionEvent evt) {
         ysrc -= 1;
         
          x -= 1000;       
          
         i += 1;
         displayTime = 60 - i;   
          
          if (x < 0) {
              timer.stop();
            }
        
         repaint();
      }
  };


 public void init()
 {
     
     fill = Color.red;
     outline = Color.blue;  
    
    
    butt1.setBackground(Color.yellow);
    add(butt1); …
kokopo2 0 Newbie Poster

Hi, pple im interested to know how to put a drawn 'image', a combination of a circle and a line into another class?

If i like to draw that 'image' in the void paint() method multiple times, e.g 10. How should i go about doing it?

Below is a sample of my code...

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.Timer;


public class Balloons extends Applet implements MouseListener, ActionListener
{
 // The X-coordinate and Y-coordinate of the last move.
 int xpos, ypos;
 int xsrc, ysrc;

 Color fill, outline;
 
 Button butt1 = new Button("Start");
 
 private Timer timer;
 
 long time = 60000; //60 seconds
 
 int displayTime, i;

  
 int delay = 1000; //milliseconds
  ActionListener timerAction = new ActionListener() {
      
      long x = time - 1000;
      public void actionPerformed(ActionEvent evt) {
         ysrc -= 1;
         
          x -= 1000;
          
         i += 1;
              displayTime = 60 - i;
     
          
          if (x < 0) {
              timer.stop();
            }
        
         repaint();
      }
  };


 public void init()
 {

    fill = Color.red;
    outline = Color.blue;  
    
    butt1.setBackground(Color.yellow);
    add(butt1);
    butt1.addActionListener(this);
    
    /*timer start*/
    timer = new Timer( delay, timerAction);
    
    
   
 
  // Add the MouseListener to your applet
  addMouseListener(this);

 

  this.setBackground(Color.white);
 }

 public void paint(Graphics g)
 {

            // A circle using the oval at 150,310 and 80 pixels diameter
    g.setColor(fill);
    g.fillOval(10, 450+ysrc, 30, 30);
    g.setColor(outline);
    g.drawOval(10, 450+ysrc, 30, 30);
    
   
    g.setColor(fill);
    g.fillRect(25, 480+ysrc, 1, 20);
    g.setColor(outline);
    g.drawRect(25, 480+ysrc, 1, 20);
    
     g.drawString("Timer: ("+displayTime+") seconds", 10, 10);
  
    

}

     /*mousemoved method*/
     public void mouseMoved(MouseEvent me) {}

     //method called when mouse is being dragged
     public void mouseDragged(MouseEvent me) {}

     // This method …
kokopo2 0 Newbie Poster

hi people,

i am having some problems, perhaps understanding about how dynamic memory allocation works with pointers and arrays.

Im trying to get the user to input the size of the string in my program and allocates a certain amount of memory to hold the string of the size.

Next, i am going to ask the user to input a string which must consists of only M, U, I. Im going to put that into my input array.

The problem now is how am i going to reference the memory pointer to the input array ?

Also im having a problem with functions too. My isaMUstring function has to return a 1, if the user input correctly ( only M, U, I) and a 0 if otherwise. I can't seem to get the concept of getting the return value of the function to work in my main function.

If i can get the the return value of my function, i am able to perform other task like if the function return a 1, i would print "Correct", if otherwise, i would print "Incorrect".

I have tried

if( isaMUstring( 1 ) ){

do someting}

in the main.

but it doesn't work.

Could someone offer some help on the above 2 problems?

Below is a sample of my code.

Thanks in advance.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int isaMUstring( const char *string );




int main(){

	int …
kokopo2 0 Newbie Poster

This prints the address of the variable data.

printf("The address: %08lx\n", &data);

Have you posted a sample of the data file?

[edit]And now I'm thoroughly confused by trying to follow all of the cross posting (cprogramming, devshed, gidforums, cppworld, and here). Rather than continue on with your presumed solutions and various grabs from different sources, how about summarizing for us what you have for input and what you expect for output?

i have my b3-long-sample.bin file for input
it is a file containing machine excutable MIPS programs.
i need to determine whether it is in lengths of 4 bytes and read the contents into an unsigned long and print out in hexa.

Also, i like to print the address before each instruction is loaded into.

I didn't know that my posting in various forums confuses you... But they are actually the same posts in every forums....so if you just look at one forum, it will suffice.

I still like to thank you, Dave for your help so far.

kokopo2 0 Newbie Poster
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

struct VarData{
	unsigned long var;
};


int main()
{
  FILE * mFile;

  struct VarData data;


  mFile = fopen ("b3-long-sample.bin" , "rb");
  if (mFile == NULL) { /* If there was a problem, show the error */
    perror ("Error opening file");
  }
  else {

	  while( !feof( mFile ) ) {

		  fread( &data, sizeof( struct VarData ), 1, mFile);

		  	printf("The address: %08lx\n", &data);
    		printf("The word: %08lx\n", data.var);
		}

   }
return 0;
}

this is the attempt i made so far.
im wondering if this is right.
also the address is the same for each word ...
but i have to print the address before each instruction is loaded into.
ive noted tt the MIPS text segment begins at 0x00400000.
but i have can't seems to acheive that.
can some1 tell me what the problem is ?
thanks

kokopo2 0 Newbie Poster

I have another problem.
supposedly i have to read a .bin file.
i have to make sure tt the length is in a multiple of 4 bytes. and store each four byte into a unsigned long variable and print the variable in hexa.

i would use the fopen to read the file in binary .
however im having problems trying to read the file in four 'bytes'...
how am i suppose to break the file into bytes?


some help pls?
thanks.

kokopo2 0 Newbie Poster
#include <stdio.h>
#include <stdlib.h>

    union uVariant {

        // Long
        unsigned long num;

        // Bits
        struct {
            unsigned char bit31 : 6;

            unsigned char bit25 : 5;

            unsigned char bit20 : 1;
            unsigned char bit19 : 1;
            unsigned char bit18 : 1;
            unsigned char bit17 : 1;
            unsigned char bit16 : 1;
            unsigned char bit15 : 1;
            unsigned char bit14 : 1;
            unsigned char bit13 : 1;
            unsigned char bit12 : 1;
            unsigned char bit11 : 1;
            unsigned char bit10 : 1;
            unsigned char bit09 : 1;
            unsigned char bit08 : 1;
            unsigned char bit07 : 1;
            unsigned char bit06 : 1;
            unsigned char bit05 : 1;
            unsigned char bit04 : 1;
            unsigned char bit03 : 1;
            unsigned char bit02 : 1;
            unsigned char bit01 : 1;
            unsigned char bit00 : 1;

        };
           };



int main()
{
    long    nTestLong = 0x3e5AF3;

	union uVariant flags;
   	flags.num = nTestLong;

    printf( "%d\n", (int)flags.bit25);
}

i managed to figure out how to extract different sections of the bits in the instruction, but supposedly if i like to convert the above code into a function . how can i do so ?
in the main will be where the input instruction will be. but how do i put the struct into a function and print from there?
some help pls?

kokopo2 0 Newbie Poster

Hi guys,
do any of u know how to extract a certain amount of bits from a parameter(instruction) and return the bits as a integer in a range .

for example i would like to extract the opcode field i would extract from bits 31 to 26.
but if i wan to extract from bits 25 to 21 for R type registers, how can i go about it? Some help pls?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void displayBits( unsigned value );

int main()
{
	unsigned long x; /*user input variable*/
	printf("Enter an long integer: ");
	scanf ("%ld", &x );

	displayBits( x );

	return 0; /*indicates sucessful termination*/

} /*end main*/

/* display bits of an unsigned integer value*/
void displayBits( unsigned  value )
{
	unsigned c; /* counter */

	/* define displayMask and left shift 31 bits */
	unsigned displayMask = 1 << 31;

	printf("%10ld = ", value);

	/*loop through bits */
	for ( c=1; c<=8; c++){
		putchar( value & displayMask ? '1' :'0');
		value <<= 1; /*shift value left by 1*/

		if( c% 8 ==0){ /*output space after 8 bits*/
			putchar( ' ' );
		}/*end if*/
	}/*end for*/

	putchar(' \n ');
}/* end function displayBits */
kokopo2 0 Newbie Poster

thanks for the help. I managed to solve the problem! :)

kokopo2 0 Newbie Poster

Hi guys, i like to thank all who has helped me so far. I managed to come up with some sort of code, to find the invalid string. e.g -8ha .
However im having problems getting it to print whether is it a invalid string once only. Also if there is a number in the invalid string it will print the number out too.

Also, if it is a negative number, the program will print it as invalid string too.

/*the output is .
Contents of file : 12 3 + * -79 -8ha

12 number

3 number

+ not number

* not number

-79 invalid
invalid
invalid
number

-8ha invalid
invalid
number
*/

i managed to print the positive number and operator correctly,
however i just cant seem to print the negative number and invalid string correctly.
can some1 help pls?
thanks in advance...

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>



int main()
{
  FILE * mFile;
  char string[100]; /* Input line from file           */
  char st[100];     /* Temporary storage for the line */

  char nt[4];
  char *tokenPtr;

  mFile = fopen ("myfile.txt" , "r");
  if (mFile == NULL) { /* If there was a problem, show the error */
    perror ("Error opening file");
  }
  else {
    fgets (string , 100 , mFile);
    printf("The original line: >%s<\n", string);

    strcpy(st, string);

    tokenPtr = strtok(st, " \n");

    while( tokenPtr != …
kokopo2 0 Newbie Poster

the problem is as follows
read content of file,
break the content into words; using strtok, delimiter is 'white space'
now the problem im facing is how do one put the 'tokenPtr' into an array?

Im thinking of, after breaking into words, but the word into one array . However im facing a problem, initializer must be constant.

After breaking each word into 'letters,numbers or operators'.
use atoi to change each 'letters,numbers or operators' into a integer. it will return a integer if integer but return 0 if it is a letter or operator.

i add all the individual parts of the array.
if array= 0, then it is not a number.
if array is 1 or more , it is a number.

another problem im facing if the number(from content of the file) is 0, it will return not a number..

i can't seem to set the content of pointer tokenPtr into another array either.

some1 help pls?

thanks in advance.

the code i come up so far.

i assume each word has a maximum of 4 size.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>



int main()
{
   FILE * mFile; /* mFile is myfile.txt file pointer */
   char string [100]; /* string array */
   const char *tokenPtr; /* tokenPtr is a pointer to a char */


   mFile = fopen ("myfile.txt" , "r"); /* open file for reading*/
   if (mFile == NULL) …
kokopo2 0 Newbie Poster

Hi pros and experts. I am experiencing some trouble in doing up a code which could evaluate the input into the different data types.

What i managed to acheived is to read a file and its content.
I used strtok to break the string into various 'tokens'.
Now the problem is how do i differentiate the tokens into the different data types. I tried using 'isdigit' and 'atoi' to no avail. help pls.

For example contents of 'myfile.txt' as follow:

12 43 + - = / -9ya -300

the program should print the output as

12 is a number.
43 is a number.
+ is a operator.
- is a operator.
/ is a operator.
-9ya is invalid.
-300 is a number.

The code i managed to acheived so far is as follows:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>


int main()
{
FILE * pFile;
char string [100];
char *tokenPtr;
int i;


pFile = fopen ("myfile.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
else {


fgets (string , 100 , pFile);
puts (string);


tokenPtr = strtok( string, " ");


while( tokenPtr != NULL ){



printf("%s\n",tokenPtr );


tokenPtr = strtok( NULL, " ");


}


fclose (pFile);
}
return 0;
}

Any help is deeply appreciated.