Thread: newbie w/compiler troubles

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    1

    Unhappy newbie w/compiler troubles

    newbie here learning c++ code... i've written a program to translate
    basic commands from a programmable HP15C calculator to its assembly
    language (i.e., "ent" = "36"). i believe something is wrong
    with the function that translates; on unix, it compiles but the
    output file shows blocks and $ symbols rather than numbers.
    Please help! I have only posted the code for the function, but i can
    post the rest of the program if necessary.
    Thanks!

    void Assemble(fstream & in, fstream & out)
    // Purpose: Assemble the HP 15C assembly language program contained in "in" into HP 15C machine language stored in "out".

    // Input parameters:
    // in --> the file variable for a text input file
    // out --> the file variable for a binary output file
    {
    index bs[100];
    int counter;
    int x;

    for (counter = 0; !in.eof(); counter++)
    {
    in >> bs[counter].line;

    if (strlen(bs[counter].line) == 1)
    {
    x = strlen(bs[counter].line);
    out.write((char* ) &x, 4);
    }

    else if (strcmp(bs[counter].line, "ent") == 0)
    {
    x = 36;
    out.write((char *) &x, 4);
    }
    else if (strcmp(bs[counter].line, "sub") == 0)
    {
    x = 30;
    out.write((char *) &x, 4); }
    else if (strcmp(bs[counter].line, "mul") == 0)
    {
    x = 20;
    out.write((char *) &x, 4);
    }
    else if (strcmp(bs[counter].line, "add") == 0)
    {
    x = 40;
    out.write((char *) &x, 4);
    }
    else if (strcmp(bs[counter].line, "div") == 0)
    {
    x = 10;
    out.write((char *) &x, 4);
    }
    else if (strcmp(bs[counter].line, "sto") == 0)
    {
    x = 60;
    out.write((char *) &x, 4);
    }
    else if (strcmp(bs[counter].line, "rcl") == 0)
    {
    x = 70;
    out.write((char *) &x, 4);
    }
    out.write(("\n"), 4);
    }
    }

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    the reason it shows characters instead of numbers is beacuse a text editor will read the single byte integer values in as characters not numbers and display them as such, if you want to see the numbers in a text editor you need to convert the number s using itoa() and then write the resulting string to file.

    you can use atoi() like so

    char *_itoa( int value, char *string, int radix );

    where radix is the base, such as 10 for decimal numbers and 2 for binary.

    Code:
    #include <stdio.h>
    #include <sdtlib.h> // contians atoi()
    
    int main(void)
    {
        int num = 2000;
        char number[10];
    
        itoa(num,number,10);
        printf("%d is %s\n",num,number);
    
        return 0;
    }
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-31-2006, 02:15 AM
  2. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  3. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  4. Accumulator Troubles: NEWBIE
    By sevenhallz in forum C++ Programming
    Replies: 4
    Last Post: 12-08-2003, 07:35 AM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM