Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Please make sure your code is properly indented before pasting it into the postings. If it was indented before, then you may be having problems pasting it. For the meanwhile, I'll run the code through AStyle to get it formetted usefully:

package waffles;
import java.util.Scanner;
import java.util.Stack;

/**
*
*/
public class Waffles {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
        Stack<String> waffleStack= new Stack<String>();
        Scanner keyboard = new Scanner(System.in);
//Bonus waffles are either millk or berry.
        String buckwheat = "Buckwheat waffle"; // Standard waffles
        String blueberry = "milk waffle"; // Bonus waffles
        String strawberry = "berry waffle"; // Bonus waffles
//Use a for loop to help the cook assemble a Stack of eight buckwheat waffles. Complete the for loop and add the missing statement so the eight waffles are placed on the stack.
        for(int k = 1; k<=8; k++) {
//Now choose some additional Special-Challenge waffles for a friend to eat.
// Add as many as you want. Special Vs must be milk or berry.
            wafflesStack.push("milk waffles") ;
            wafflesStack.add("berry waffles");
        }
// Use the customized static print method below to display the Stack of waffless your sponsoree must eat.
        System.out.println("\n sponsoree must eat: "+waffleStack.size());

// Let the contest begin! Start eating waffless using a while loop!
// Earn $2 for each buckwheat waffles you eat, and earn $5 for each bonus waffles
        int numberEaten = 0;
        int moneyEarned = 0;
        String nextWaffle;
        boolean done = false;
        while(!done) { …
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

As a piece of advice: since the data you are working with is mostly strings, I'd recommend that you do all the computation (i.e., the calls to strcpy(), strcat(), and strlen()) first, then print out the strings in one go. The only thing you can't do this with would be the lengths, which are integers, but you can handle that easily enough by passing the integers as arguments to the print function.

Here's a trick that could save you a bunch of cutting and pasting: put the addresses of (most of) the strings into a fixed array delimited with a zero:

newline: .asciiz "\n"

# this array holds the addresses of the strings to print in the order
# they are to be printed in, all except S6, L1, and L2
print_table: .word string, S1, newline, S2, newline, S3, newline
             .word S4, string4, S5, string5, S6, string6, 0

Then have a loop that walks through the array, printing the strings as it goes:

# use a table-driven loop to print the strings 
# in the appropriate order. 
    la $t0, print_table  # get the beginning of the table of addresses
    li $v0, 4
    j print.test
print.loop:
    syscall
    addi $t0, $t0, 4     # go to the next address in the array
print.test:
    lw $a0, 0($t0)
    bnez $a0, print.loop

This should shorten the code significantly.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

We are willing to help you, but you'll need to show what you've done already, and give us more specific about what help you need.

  • What have you done so far? Do you have a plan for how to do it? Have you thought out what the main data structures and database tables you'll need are?

  • Is this for a school assignment or a real-world project? If the latter, is there already a system in place which you would need to either incorporate or replace, and if there is, do you need to migrate any existing data to your new system?

  • You mention that PHP was recommended as the implementation language for at least part of the project. What languages are you already familiar with, and is PHP one of them? If not, do you have time to learn enough of it for the project? What other options do you have for languages?

  • Do you know any web frameworks such as Zend, Django, or Rails, and would you be allowed to use one for the project?

  • What database engine do you mean to use? Do you have experience with the DBMS in question, and if not, do you have time to learn enough of it for the project?

  • Who would be the users of the system, the company representatives, or the customers themselves, or both?

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Yes, it should allow you to paste code; what browser are you using?

If you look at the editing window, you should see a button marked 'Code'. If you click on that, it will come up with a pop-up window for editing the code in. If you have pop-ups blocked, or are using NoScript or a similar script blocker, that could be the cause of the problem.

Note also that you may not need to paste the whole program, if there is a specific function or section that you know is the source of the issue.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Did you read my last post? Part of the problem is that you are overwriting the message strings. However, the copy function is also flawed, though in a way that is easy to overlook:

strcpy:
    lbu $t0, 0($a0) #load a byte from source string
    sb $t1, 0($a1)  #  <-- the source should be $t0, not $t1
    addi $a0, $a0, 1 #increment both addresses
    addi $a1, $a1, 1
    beqz $t1, cpyExit # <--- this should be $t0 as well
    j strcpy

cpyExit:
    jr $ra
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

To elucidate my earlier post: right now, you have S3 overwriting S5 when you run strcpy1(). You want to declare S5 as a .space of at least 9 bytes (8 plus the zero delimiter), or else have it as a .word (a pointer, in effect) and use system call 9 (memory allocation) to allocate 9 bytes for it. The same is true for S4 and S6 as well, though the sizes will be different. The messages now held by S4, S5, and S6 should be moved to their own locations.

.data
S1: .asciiz "I "
S2: .asciiz "love "
S3: .asciiz "assembly"
S4: .space S4 - S1  # set aside space to hold the three strings concatenated
S5: .space S4 - S3  # set aside enough space for a copy of S3
S6: .space S5 - S4  # set aside enough space for a copy of S4
mesg1: .asciiz "\nOur three strings are:\n"
mesg2: .asciiz "\nThe three strings combined are: "
mesg3: .asciiz "\nWhen string 3 is copied into string 5, string 5 is: "
mesg4: .asciiz "\nWhen string 4 is copied into string 6, string 6 is: "
L1: .asciiz "\nThe length of the first string is: "
L2: .asciiz "\nThe length of string 4 is: "

The sizes may need to be explicitly entered in MARS:

S4: .space 16  # set aside space to hold the three strings concatenated
S5: .space 9   # set aside enough space for a copy of S3
S6: .space 16 …
C-Money commented: I didn't see this post earlier, as I've been working on it. Thanks, I'll try this method. +1
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

In print2

 li $v0, 1    # <--- what type is S5?
 move $a0, $s0 # get argument from $s0
 syscall
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

OK, where is the S1 string being copied to again? Think about it for a bit, and consider what, if anything, is already there.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

You are using beqz for the tests, but you want to use bnez (which is actually a macro for bne x, $zero, label, but both SPIM and MARS should support it). You want to loop when the value is not equal to zero; right now, you are looping only if it is zero.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

As JWenting says, the research is yours to do. We can only give advice.

That having been said, I would start by going over the languages you are already familiar with, and see what relevant libraries are available for each. In most cases, the frameworks in question should work with most languages, given sufficient coaxing, so it probably is a matter of what language you are most fluent in. It is unlikely that you would want to choose an unfamiliar notation for this, though that, again, is your decision to make.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Haven't we been here before? Oh, yeah, we have. In that case, let me repeat what I said last time:

First off, don't hijack other people's threads with unrelated questions; if you have a new question not related to the thread, create a new thread.

Second, didn't you read what I said to the OP? We don't do other people's homework for them like that. If you want help, show us what you've already done, otherwise, we can't help you and won't even respond to you. If you want the work done for you, hie your lazy self to freelancer.com and pay someone to do it, just don't cry to us when you get caught cheating.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

No problem. Fortunately, each of these by itself is fairly easy; actually, strcat() and strcpy() are very similar, enough so that the easiest solution for strcat() in assembly language is embed strcpy() at the end of it, like so:

###################################
## char* strcat(char*, char*)
strcat:
    # save a copy of $a0 on the stack
    jal strlen
    add $a0, $a0, $v0  # reset the dest to the end of dest
    j strcpy.test    #### WARNING: Fall-thru to strcpy() ###

###################################
## char* strcpy(char*, char*)
strcpy:
    # save a copy of $a0 on the stack

strcpy.loop:
    # copy the string in a loop

strcpy.test:
    # test for the end of the loop

    # retrieve the original value of dest and put it in $v0
    # restore the stack

    jr $ra
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

The most likely cause is that you accidentally deleted or commented out the function return (jr $ra) at the end of strlen().

BTW, the size of the string "I " is two, not one; you have to count the space as a character. I've fixed the code as I had it so it now works correctly on QtSpim (I don't think the .align 4 directive works in Mars, though):

.data
.align 4
S1: .asciiz "I "
S2: .asciiz "love "
S3: .asciiz "assembly"
string: .asciiz "Our three strings are:\n"
S4: .asciiz "\nThe three strings combined are: "
S5: .asciiz "\nWhen string 3 is copied into string 5, string 5 is: "
S6: .asciiz "\nWhen string 4 is copied into string 6, string 6 is: "
L1: .asciiz "\nThe length of the first string is: "
L2: .asciiz "\nThe length of string 4 is: "
.text
main:

#display all three strings first
    li $v0, 4
    la $a0, string
    syscall
    la $a0, S1
    syscall 
    la $a0, S2
    syscall
    la $a0, S3
    syscall
    la $a0, S1 #load address of string
    jal strlen #call string length procedure
    move $a0, $v0
    jal print
    addi $a1, $a0, 0 #move address of string to $a1
    addi $v1, $v0, 0 #move length of string to $v1
    addi $v0, $0, 11 #syscall code for message
    la $a0, L1 #address of message
    syscall

    li $v0, 10
    syscall

#########################################
## int strlen(char*)
strlen:
    move $t0, $zero  #initialize count to start with 0 for first character
    j strlen.test


strlen.loop:
    addi $a0, …
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

I'm going to recommend two things: first, that you change the return type to string and second, that you declare a char ch. If you then take the return value, and append x to it:

string copy()
{
    cin >> ch; 

    if(ch != '\n')
    {
        return ("" + ch + copy());
    }
    else
    {
        return "";
    }
}   

then it should fall into place.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Let's see if you can determine the problems with these two lines:

void copy()

and

    return (x +1);

then figure out what it implies for

            copy()

(Bonus points if you can see the syntax error in that last line.)

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

For the first line of strlen(), change the code to

 addi $t0, $zero, 1 #initialize count to start with 1 for first character

The problem you are experiencing is likely to be because the $t0 register is not guaranteed to be zeroed out when the program starts. You are also loading the character into $t0 in the second line of code, when you presumably want to put it in $t1:

lb $t1, 0($a0) #load the next character to t1

Finally, the usual practice is to use $v0 and $v1 for the return values of functions. I would recommend changing the program thusly:

.data
S1: .asciiz "I "
S2: .asciiz "love "
S3: .asciiz "assembly"
string: .asciiz "Our three strings are:\n"
S4: .asciiz "\nThe three strings combined are: "
S5: .asciiz "\nWhen string 3 is copied into string 5, string 5 is: "
S6: .asciiz "\nWhen string 4 is copied into string 6, string 6 is: "
L1: .asciiz "\nThe length of the first string is: "
L2: .asciiz "\nThe length of string 4 is: "
.text
main:

#display all three strings first
    li $v0, 4
    la $a0, string
    syscall
    la $a0, S1
    syscall 
    la $a0, S2
    syscall
    la $a0, S3
    syscall
    la $a0, S1 #load address of string
    jal strlen #call string length procedure
    move $a0, $v0
    jal print
    addi $a1, $a0, 0 #move address of string to $a1
    addi $v1, $v0, 0 #move length of string to $v1
    addi $v0, $0, 11 #syscall …
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Wait, I apologize, I would expect it to make an infinite loop, as it would never even get to A[]. A stack overflow is still surprising to me, though.

EDIT: Oh, right, it is overflowing into a 64-bit value. Makes sense now.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

in the inner loop, you are incrementing i instead of j. This leads to you walking off of the end of the array A, but I would expect it to give a segfault, not a stack overflow. How odd.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

I would recommend using the new operator rather than the C-style memory allocation, but that is what it sounds like you're expected to use, yes.

From the sounds of it, the intention is for you to create the first array in main() (or whatever function you are calling prefixAverages() from), then pass it to prefixAverages() as an argument along with the size of the array. You would then use new to allocate the second array.

int *A = new int[n];

Then, after filling A with the computed values, you would return it as an int pointer.

 int* y = prefixAverages(x, SIZE);

Oh, but don't forget to use delete before the end of the program to free the allocated array:

delete[] y;

EDIT: I forgot the square brackets on the delete.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Has the instructor covered dynamic memory allocation (new and delete) yet?

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

mike_2000_17: While that is indeed a solution to the problem as given, I'm not sure it is the answer Labdabeta needs. This has the smell of a shoe or bottle problem to me; I think Labdabeta should possibly reconsider the direction he's taking, if he finds himself needing such an unusual solution. I may be wrong; there certainly are places where a stream might have to serve multiple purposes like this. I am just not certain this is one of them, and would like a broader picture of the problem.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Well, we could take the existing function and simplify it like this, if you like:

def print_something(ctr):
    if ctr == '1':
        print("This is a valid entry.")
    else:
        print("Sorry, but what you have typed in is invalid,\nPlease try again.")

But I am not certain what that would clarify for you.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

A web framework is a set of interconnected libraries and interfaces that allow you to work with web pages in a more structured manner than the default Python library does (actually, in most languages, there isn't any standard library for HTML and HTTP at all; Python has only a very basic one). Typical web frameworks for Python include Django (probably the most widely used), web2py, Zope, and TurboGears.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

If output alone were your goal, I would say to use an ostream for the variable class and ofstream for the output file. However, you are then using the file for input, which does indeed lead to the problem you describe.

Wait, what is the goal, here? Why would you need to be able to use a single stream serve for multiple purposes like that?

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

First off, don't hijack other people's threads with unrelated questions; if you have a new question not related to the thread, create a new thread.

Second, didn't you read what I said to oppu.qhar? We don't do other people's homework for them like that. If you want help, show us what you've already done, otherwise, we can't help you and won't even respond to you. If you want the work done for you, hie your lazy self to freelancer.com and pay someone to do it, just don't cry to us when you get caught cheating.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster
total=0

This is initializing total to zero; by doing it in the declaration list, it means it is done at compile time, speeding up the program slightly. The reason we need to initialize it to zero is because later on, we are using the value of total in an assignment that resets total itself, accumulating the final value. Thus, we need to ensure that it starts at zero (instead of whatever garbage happens to be in that part of memory), or the value will be incorrect.

"%16s"

This is the format string being passed to scanf(), but you probably understood that already. I assume the question is, what is the 16 part? Well, that modifies the s (which indicates it should read the data in as a string), limiting it to the first 16 characters. We need to do this because the buffer we're storing the value in only has enough room for 16 characters (plus a zero-delimiter at the end of the string).

len=strlen(bin2);

This gets the length of the the string bin2. We need it because that is the number of times we need to repeat the for() loop following it.

value=bin2[i]-'0';

OK, I'll admit this part is a bit of a trick, but I've exlained it before so I'll just go over it in more detail. The '0' is the character for zero (as opposed to the integer value zero). In C, and most other languages, the default character …

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

OK, and where is the question? More importantly, where is the code you've already tried writing?

To quote Daniweb rules:

Do provide evidence of having done some work yourself if posting questions from school or work assignments

So, what have you done on this so far yourself?

Or perhaps you didn't understand the assignment, which is understandable because it makes no sense whatsoever. Where's the rest of the assignment statement?

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

in the line in question:

 sum[M][3] = inputarray[M][3] + inputarray2[M][3];

You are treating inputarray() and inputarray2() as arrays, rather than calling them as functions. The correct syntax would be:

 sum[M][3] = inputarray(M, 3) + inputarray2(M, 3);

BTW, I strongly recommend you get into the practice of indenting your code suitably. The usual practice is to indent one level - three to eight spaces, depending on your indent style - every time you have a block of code that is inside a loop or an if statement. This practice, called 'nesting', is very important. The goal of formatting code is to make the nesting levels explicit in the layout of the program. While in C, it is purely for the benefit of the programmer, it should make reading and editing the code easier, which is why it is important to get into the habit as soon as you can. I recommend reading this article on the subject for further clarification.

Now, as this Wikipedia entry explains, there are several different styles which one can use to indent their code; what is important is not the specific style, but consistency in applying your chosen style. As long as you follow the main rule - indent inside a block, de-dent after the end of a block - you should be able to use the style you are comfortable with, **so long as you are consistent in appyling it*. those two rules are the …

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

I think you'll find a purely functional sub-set of Scheme sufficiently simple for you needs. It is famously easy to implement in a short program, and requires relatively few resources (mainly, some form of garbage collection or reference counting - the latter of which PHP already supports IIRC). Several online books, including Lisp In Small Pieces and Structure and Interpretation of Computer Programs, discuss implementing a Scheme interpreter in detail.

Peter Michaux's Scheme from Scratch blog series has a step-by-step explanation of how to do it (in C, but it would be a small matter to adapt it to PHP). I would recommend the article to all programmers, but especially to someone looking to do what you are doing - even if you don't end up choosing Scheme as your language.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

I would actually recommend Gnome Sort, as it is a little smaller marginally faster than Bubble Sort, though it is a little harder to understand. The main reason bubble sort is such a topic of fascination for computer scientists is that it appears to be the 'naive' algorithm for sorting, that is to say, it is the one which most people, if asked to sort a list by computer and not given any algorithms to follow, will come up with on their own.

FYI, in case you are wondering, in practice there are no ideal sorting methods that work fastest for all input. Every practical sorting algorithm is sensitive to the order of the original unsorted list, and even where there is a clear difference in the optimal performance (i.e., O(n log n) for the average case of quicksort versus O(n^2) for Bubblesort), the theoretically slower sorting algorithm may perform better in practice, either because of the order of the input, or because the overhead is smaller for smaller inputs.

In theory, the worst 'practical' sorting algorithm is Bogosort, which consists of repeatedly shuffling the list and testing to see if it has randomly entered a sorted state. The average performance for this is O((n-1) n! (that is to say, the number of items in the list minus one, times the product of every number between 1 and the size of the list - a very, very bad performance); the worst case is unbounded (that is …

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

I'm not sure just how to answer that question. What aspects of it are you unclear on?

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

As far as the size issue is concerned, yes. There really isn't any solution to it in Turbo C that wouldn't take at least twice as much coding as you have already done.

I would fix the issues mentioned in my last post, though.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Oops, I found an error in my last post; the octal code should have been

    total=(total*8)+value;

not

    total=(total*8)+8;

Sorry about that.

Also, the printf() should have been after the end of the loop, I thought I had fixed that but apparently not.

As for the maximum size of the value, I am afraid there isn't much to be done about that, at least not reasonably. With a 16-bit word, which is the size of both the int and the long in Turbo C, FFFF (base 16) is the largest number it is capable of holding. I didn't foresee this, as almost all modern systems use a 32-bit int and a 64-bit long.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

I see what the problem is now; the lines

total=(total<<8)|8;

and

total=(total<<16)+8;

need to be

total=(total*8)+value;

and

total=(total*16)+value;

respectively. The reason the binary version worked with this approach is because it is working on the bits directly; the << operator is the left shift, which turns a bit pattern like 0000000000000101 into 0000000000001010 when shofting by 1. This works because it is the same effect as multiplying by the powers of 2. The 'bitwise or' operator, |, has the effect of comparing each bit in pair of a bit patterns, and returning the result where if a bit is set in either pattern, it is set in the returned value. for example,

10011000
00101101
--------
10111101

In this case, since you have already shifted the value by one, then it has the effect of putting the single bit in value into the least significant bit of the total. For example,

101

1
1 << 1 | 0 = 10  | 0 = 10
1 << 1 | 1 = 100 | 1 = 101

This works fine for binary conversion, but not for any higher numbers.

I see you're trying to get the indentation fixed, which is good, but the problem is you are mixing spaces and tabs; I personally use spaces, but the default in the Turbo C++ editor is tabs. This sometimes works, and sometimes doesn't. You also want to stay consistent in how you indent …

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Why is the indentation relevant? Because it would have made the problem with the ocatal function clear immediately - you have the printf() statement inside the loop, instead of after it.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

I know it doesn't sound relevant here but you still aren't indenting your code in a useful manner. The last function should look like:

void Hexa2Decimal()
{
    char hex16[17];
    unsigned long dec16,i16,sum16,len16;

    sum16=0;
    dec16=0;

    gotoxy(1,13);printf("[HEXADECIMAL TO DECIMAL CONVERSION]");
    gotoxy(1,15);printf("Enter a Hexadecimal number: ");
    scanf("%16s", hex16);
    printf("\n");

    len16=strlen(hex16);
    for(i16=0;i16<len16;++i16)
    {
        if(isdigit(hex16[i16]))
        {
            dec16=hex16[i16]-'0';
        }
        else
        {
            dec16=toupper(hex16[i16])-'A'+10;
        }
        sum16=(sum16*16)+dec16;
    }
    printf("Decimal equivalent is: %d\n", sum16);
}

If you look, this version of the code indents every time you enter a block, and de-dents when you leave the block. As I explained here, the whole purpose of indentation like this is to make the nesting of the different conditional blocks obvious just at a glance.

As for the error, it looks as if only the final digit is being converted for some reason. Let me look at it some more and I'll get back to you.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Because each digit in an octal value is equal to three bits of binary, and each hex digit is equal to four bits. The higher the base, the more compact the representation. This is related to why decimal doesn't suit many purposes in computer mathematics - base 10 doesn't line up to an exact number of bits, that is to say, three bits is too small to hold a decimal number (it can only represent eight values), while 4 bits is too large (it can represent up to sixteen values, not just ten).

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Fortunately, the code for the octal function is very similar to that for the binary:

void Octal2Decimal()
{
    char buffer[7];
    int len, i, total = 0, value;

    printf("[OCTAL TO DECIMAL CONVERSION]\n\n");
    printf("Enter an Octal number: ");
    // accept a maximum of 6 chars
    scanf("%6s", buffer);
    printf("\n");

    len = strlen(buffer);

    // convert and check if we have valid data
    for (i = 0; i < len; ++i)
    {
        value = buffer[i] - '0';
        if (value < 0 && value > 7)
        {
            printf("Invalid octal data\n");
            return;
        }
        total = (total * 8) + value
    }
    printf("Decimal equivalent is: %d\n", total);
}

What it does is read the input string into buffer[], and then for each digit, it subtracts the ASCII character value of the digit from the ASCII value of '0'. Since the ASCII values for numerals is in order 0 to 9, this has the effect of getting the numeric value of the digit.

It then checks whether the value is in the value range of an octal digit, that is to say, between 0 and 7. If it itsn't, it prints an error message and ends the function.

finally, if the digit is valid, it multiplies the running total by the base of the representation - eight, in this case - to 'push' the existing values up one octal place, so that if the previosu total was, for example, '417' (base 8), then the new total would be '4170' (base 8). It then adds the digit's value …

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

All you need to do is delete or comment out that one line. This should do fine:

void Hexa2Decimal()
{
    char hex16[9];
    unsigned long dec16,i16,sum16,len16;

    sum16 = 0;
    dec16 = 0;
    gotoxy(1,13);printf("[HEX TO DECIMAL CONVERSION]");
    gotoxy(1,15);printf("Enter an Hex number: ");
    scanf("%8s", hex16);
    printf("\n");

    len16 = strlen(hex16);
    for (i16 = 0; i16 < len16; ++i16)
    {

        if (isdigit(hex16[i16]))
        {
            dec16 = hex16[i16] - '0';  /* get the value of the digit */
        }
        else
        {
            dec16 = toupper(hex16[i16]) - 'A' + 10;
        }

        sum16 = (sum16 * 16) + dec16;
    }
    printf("Decimal equivalent is: %u", sum16);
}
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Oops. Sorry about that debug-print line I left in there.

OK, I've checked into this and found that I was wrong about the word sizes in Turbo C++. I knew that the int values were one word (16 bits) wide; but I thought that the long would be a double word 32 bits. Apparently not. With a only a 16 bit long value, the largest number you can have is 65535(base 10), which in hex is FFFF. By comaprison, the largest 32-bit number would be FFFFFFFF(base 16), or 4294967295(base 10), or 37777777777(base 8). You would need a 64-bit long to hold more than eight hex digits (well, actually, 39 bits would be sufficient, but the x86 processors all have word sizes that double at each step up - 8, 16, 32, and 64). Given that each byte requires two hex digits to represent it, a quadword value (64 bits, or 8 bytes) can hold 16 hex digits, naturally enough.

An unsigned value is one is always interpreted as a positive number, rather than treated values which have the last bit set as negative numbers. It let's you represent larger positive values, but at the expense of repreenting negative values.

As for why I used an unsigned long instead of a signed long, that's because if you entered a value greater than half the maximum value allowed in an unsigned long, it would come out as a negative value. The Intel processor (and most others today) used two's complement

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

I went to the extraordinary step of setting up Tubo C++ 1.01 in DosBox on my system, and was able to get the following program to run correctly. Thry it an see if it is acceptable to both TC++ and your professor.

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

void Screen_Header();
void Binary2Decimal();
void Octal2Decimal();
void Hexa2Decimal();

int  main()
{
    char key;

    do
    {    
        Screen_Header();

        scanf("%c", &key);
        gotoxy(1,20);
        printf("Press <ENTER> to Continue or Press <ANY KEY> to Exit ");
        key = getch();
    }
    while(key == '\r');

    return 0;
}

void Screen_Header()
{
    char base;

    clrscr();
    gotoxy(1,3);
    printf("Conversion from any base to base 10");
    gotoxy(1,5);
    printf("a - Binary");
    gotoxy(1,6);
    printf("b - Octal");
    gotoxy(1,7);
    printf("c - Hexadecimal");
    gotoxy(1,11);
    printf("Select a base to be converted: ");
    scanf("%c", &base);

    switch(base)
    {
    case 'a':
        Binary2Decimal();
        break;
    case 'b':
        Octal2Decimal();
        break;
    case 'c':
        Hexa2Decimal();
        break;
    default:
        printf("Sorry, %c is not a valid menu option", base);
    }
}


void Binary2Decimal()
{
    int bin2,f2=1,d2=0;

    gotoxy(1,13);
    printf("[BINARY TO DECIMAL CONVERSION]");
    gotoxy(1,15);
    printf("Enter a Binary number: ");
    scanf("%d", &bin2);

    printf("\n");

    while(bin2>0)
    {
        if((bin2%10)==1)
        {
            d2=d2+f2;
        }
        bin2=bin2/10;
        f2=f2*2;
    }
    printf("Decimal equivalent is: %d", d2);
}

void Octal2Decimal()
{
    char oct8[12];
    unsigned long dec8,i8,sum8,len8;

    sum8 = 0;
    gotoxy(1,13);printf("[OCTAL TO DECIMAL CONVERSION]");
    gotoxy(1,15);printf("Enter an Octal number: ");
    scanf("%11s", oct8);
    printf("\n");

    len8 = strlen(oct8);
    for (i8 = 0; i8 < len8; ++i8)
    {
        dec8 = oct8[i8] - '0';  /* get the value of the digit */
        sum8 = (sum8 * 8) + dec8;
    }
    printf("Decimal equivalent is: %u", sum8);
}


void …
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

A commendable ambition, I suppose, though I would personally recommend aiming a little lower to start with - C++ is a tremendously complicated language and IMAO not well suited for beginners. Learning a simpler programming language such as Python would be advisable as a start. Indeed, you would probably go faster and with better results if you learned Python first, then C++, than you would jumping into C++ at the very start.

Beyond that, do you have a question to ask?

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

I posted one last message in the previous thread, if you haven't read it yet please do so before doing anything else.

As for the language issue, I suspected that this was going to be the case. It was one of the reasons I kept asking about that earlier.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

A base class (or parent class, or superclass) is a class which one or more child classes inherits from; a child class (or sub-class) is one which inherits from a base class. Depending on the language, you may have some programmer-defined classes which have no base classes, or (more often) all programmer-defined classes may inherit implicitly from a global, abstract root class (often called Object).

When a class inherits from another class, it is as if the properties and methods of the base class are copied into the child class; the child class 'inherits' all the properties and behaviors of the parent, but may add to them, and can override (replace) some or all of the methods of the parent class.

An object reference (i.e., a variable) of the parent class type may refer to members of its child classes, in which case any message (method call) can be sent to the object that the parent class declares, but the method used will be the one of the child class. That is to say, if I have

class Foo 
    method a()
    method b()

class Bar of Foo
    method b()  // overrides Foo's b()
    method c()  // adds a new method c()

variable baz of Foo
variable quux of Foo

baz := new Foo()
quux := new Bar()

baz.a()    // this uses the Foo method a()
baz.b()    // this uses the Foo method b()

quux.a()    // this uses the Foo method a()
quux.b()    // this uses the Bar method b()
quux.c() …
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

What you need to see here is that the 'decimal equivalent' is actually a string of characters, for which there may be (in the case of a 64-bit two's-complement integer representation) 19 digits and a sign to represent. Depending on the character encoding, this may mean as much as 80 bytes (in the case of Unicode-32), though the most common encodings (ASCII, Latin-1, and UTF8) will use 1 byte per character for encoding the Arabic numerals.

Few if any CPU instruction sets have any direct support for any fixed character set; and while some (including the ubiquitous x86 and x86-64) have some very basic string manipulation instructions, these generally don't have anything to do with the character encoding - they are mostly limited to things like copying the string from one place in memory to another.

Now, your standard PC has some firmware support for a small number of character encodings, primarily for the use in displaying text on a text-mode screen, but that too is software (just software that is stored in the ROM chips). The conversions done by most language libraries are entirely in software, as they have to be able to handle different encodings. How the conversion would be done varies with the encoding.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Whats wrong with my octal and hex I thought it was right because when I run the program the conversion is correct

To illustrate what is going on with the two functions in question, I propose you try the following test: comment out the lines between the scanf() and printf() calls in Octal2Decimal(), then compile and run the program to see what happens.

void Octal2Decimal()
{
    gotoxy(1,13);printf("[OCTAL TO DECIMAL CONVERSION]");

    long oct8,dec8,rem8,i8,sum8;
    gotoxy(1,15);printf("Enter an Octal number: ");
    scanf("%o", &oct8);
/*
    printf("\n");

    {
        rem8=dec8%8;
        sum8=sum8 + (i8*rem8);
    }
*/     
    printf("Decimal equivalent is: %d", oct8);
}

What I expect you will find is that it still appears to work, even without the 'conversion' code.

Now, if you look at the code in question, you'll see that it does not even reference oct8, nor does it change it; the entire section of code has no effect on the behavior of the program! The conversion is being done by scanf(), not by your code.

The solution, as stated earlier, is to have a char array at least large enough to hold the maximum size of a 32-bit value in the representation being used (11 digits for octal, 8 for hex, plus one for the zero delimiter), and read the value into that buffer rather than directly into a long.

void Octal2Decimal()
{
    char oct8[12];
    unsigned long dec8,i8,sum8,len8;

    gotoxy(1,13);printf("[OCTAL TO DECIMAL CONVERSION]");
    gotoxy(1,15);printf("Enter an Octal number: ");
    scanf("%11s", oct8);
    printf("\n");

    len8 = strlen(oct8);
    while (i8 = 0; i8 < len8; …
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

I am sorry to keep bring this up, but you still haven't answered my question about wether you are supposed to be learning C++ or C. It is a very important distinction, as it would (among other things) explain why your professor isn't allowing the C++ iostreams.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

When I run it the program will repeatively rewrite everything when I type 'y'.

Really? Weird. It doesn't do anything of the sort when I run it. Mind you, what it doesn't do is clear the screen, since I removed all the <conio.h>-related function calls (the compiler I was using doesn't have any equivalent to that), which may be what you are actually noticing.

Loking at your code, you still haven't written the octal and hex conversions correctly; instead, what you are reading the value into a variable as a long value, then after some ineffective 'conversion', you are printing that same variable out as a decimal, without changing anything. You are not actually converting anything in your code; the scanf() and printf() functions are doing the conversions. This does not actually fulfill the requirements of the project, at least not as I understand it.

As for the default case, it is not necessary to have one, but it is best to handle all possible cases, so I would have kept it in.

BTW, you are still using void main(). Please, please, please fix that.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

It shouldn't be necessary to do so, but it might help put the class in context. Posting the error log would probably be more helpful, in this case, especially since I was able to compile the code (with the open brace restored and the c'tor name corrected) with no trouble (using GCC). I did get a warning that recommended that you move the initialization of value to the initalization list, which I have done here:

//itemtype.h:::::
#ifndef ITEMTYPE_H_INCLUDED
#define ITEMTYPE_H_INCLUDED

class itemtype
{
public:
    itemtype(): value(0) {return;};
    void print()const;
    void initialize(int number);

private:
    int value;
};

#endif

and

//itemtype.cpp::::::
#include "itemtype.h"
#include <iostream>

void itemtype::initialize(int number)
{
    value=number;
}

void itemtype::print()const
{
    std::cout<<value<<" ";
}

But even that is just a recommendation, not an error.

BTW, as an aside, it is better to avoid using directives in class definition and implementation files, and use explicit scoping for elements in the std namespace (e.g., std::cout instead of just cout). This helps avoid certain kinds of namespace conflicts, especially later when you start woring with istream and ostream selector operators. It isn't required, but it is a good practice to get into.

Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Hmmn, I can see the problem there, yes. While I would be glad to explain in detail any unfamiliar library functions or algorithms I used to you, your professor would still question how you happened to learn them yourself. That makes for a difficult situation, and I'm not sure I know how to solve it, as it really doesn't sound to me as if she gave you enough information about how the ones you have used work to solve the problem yourself, without resorting to something like this.

What did the professor explain to you about the I/O functions? Did she cover any of them besides printf() and scanf(), such as getchar() or fgets()? Were the basics of function definitions and calls covered (I can tell that you didn't know them before, but I am not sure if that's because they weren't explained, or because they weren't explained well enough)? Was the algorithm for converting a string representation to an integer given to you (it is unintuitive enough that you couldn't be expected to come up with it on your own)? Did she give any general design advice, such as separation of computation from user interface?

More generally, how does the professor explain things to you, and how does the textbook explain the same material? What resources were you given, or referred to (e.g., cplusplus.com)?

Oh, and to repeat my earlier question, are you certain that the course is on C++, and not C? Despite their …