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

Well, what specifically are you having trouble with?

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

That had been my own impression, true, but I felt it best to make sure all possible cases were covered. If I have overstepped or misled in any way, I certainly apologize.

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

Also, is there a way to write a function and then have it output what is going on in that function?

Now, I have to ask: just what do you mean by 'have it output'? Where do you want to results sent - to the console, to the calling function, to somewhere else?

If you want to output to the console, then as said by others earlier, you want to use the standard output ( cout ) to print the result.

If, on the other hand, you want to return the results to the function which called it, then you want to do two things: change the return value from void to the type of the return value ( int in this case), and use the return operator followed by the value you want to return.

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

Might I suggest using a reference to a vector<Pizza> rather than a pointer to an array? It would solve most of the issues revolving around pointers and allocation, I think. I realize that it may not be an option with a school project, but if it is acceptable, it would save a lot of trouble.

For that matter, you could also simplify things by using an enumeration for the size type, and a vector of strings for the toppings:

enum PIZZA_SIZE {SMALL, MEDIUM, LARGE};

struct Pizza
{
    PIZZA_SIZE size;
    vector<string> toppings;
};

Again, this would depend on whether this was covered in your coursework yet or not.

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

I should point out that Dev-C++ is itself a bit old, as it hasn't been maintained after 2005. Also, neither the Code::Blocks IDE or the Visual C++ Express IDE suffer from this particular problem - both pause the output automatically at the end of a console program.

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

You are having the same problem that your classmate lbgladson had: you're declaring and defining the functions as void , most of them should in fact return a Rational value.

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

With all due respect, AD, I think you've misread the code (it happens to the best of us...). There is no class declaration; that's a using namespace directive. Furthermore, even were it a class declaration, there would still be code there which isn't inside of a method.

Ancient Dragon commented: you are correct :) +17
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

The code as written isn't valid; unlike in some languages, you cannot have a code block that isn't part of a function in C++.

Mind you, it is generally inadvisable to have code (as opposed to declarations) in a header file, but that's not the cause of the problem, per se.

So, what was this code supposed to do?

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

Where in the program does it hang? Does it get any output at all?

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

Actually, it predates computer programming entirely; it comes from conventions in mathematical notation, where i, j and k are traditionally used to indicate the indices of arrays and the iterative variables of summation (big-sigma) operations.

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

OK, one problem has to do with the process of addition (and subsequently, subtraction): when adding two rational fractions of different denominators, such as:

1/2 + 2/3

you need to put them into a common denominator. The easiest way to do this is by multiplying both the numerator and denominator by the other fraction's denominator:

1/2 * 3/3 = 3/6

2/3 * 2/2 = 4/6

At that point, you simply add the numerators and reduce:

3/6 + 4/6 = 7/6

then reduce, if possible.

Finally, you come across the final error: you want the functions to return a Rational result, not a double . So, what you really want is:

Rational Rational::addition(const Rational& c)
{
    Rational temp;

    temp.numerator = (numerator * c.denominator) + (c.numerator * denominator);
    temp.denominator = denominator * c.denominator;
    return temp.reduction();
}

However, there's still a tricky part to this: you'll want to overload the assignment operator to ensure that this works correctly. I'm guessing that overloading operators has not been covered, however, or else the code would have been written to use them.

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

OK, I've look at the code now, and I see where the problem lies now. The issue is that the functions in question never return a value to be assigned in the first place! All of your functions are declared and coded as void , which isn't right at all. You want the functions to return a value, rather than changing the value of the object being operated on.

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

The process is pretty much what you would expect: first you find the Greatest Common Divisor of the two integral parts, and then divide each by that. The result will be the reduced form of the rational number.

The easiest way to find the GCD of a number is the recursive form of Euclid's Algorithm:

int gcd(int a, int b)
{
    if b == 0
       return a;
    else
       return gcd(b, a % b);
}

(I would have left this as pseudo-code for you to convert to C++, but the algorithm is so trivial to code that it was just as easy to give it to you whole cloth rather than force you to jump through pointless hoops.)

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

First off, you should use [code] tags around your code samples in the future. I expect one of the mods will do this for you, but you should not rely on this, if only because it makes extra work for them.

Second, there are a number of issues with the code as given, such that - with a modern compiler, at least - the code should not compile. For example, the <IOSTREAM.H> is now just <iostream> - not capitalized - and most current compilers will reject the older form (and capitalized should never have worked in the first place). Another place where there is a problem is the implicit integer type for main() ; you should always declare main() as returning int explicitly, and a modern compiler would flag that as an error.

I am guessing that you are using an older compiler such as Turbo C++, in which case I would recommend moving to a newer compiler - if you have the choice to, that is - such as GCC or Visual C++ Express.

As for the specific error message, the problem presumably lies with this line:

temp=(k/m-k)m;

Here you are using an implicit multiplication by placing m next to a parenthesis; this is a common misunderstanding, as it looks like the usual mathematical notation. The problem is, despite similarities and syntactic sugar, C++ expression syntax is not mathematical notation, and doesn't work like it. The code needs an explicit multiplication operator, like so:


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

You're not the first to make this mistake about fflush() ; it sounds reasonable, but the problem is that it doesn't do what you are expecting. In C, the fflush() function only operates on output, forcing it to finish immediately rather than remain in the buffer. Applying it to an input stream has undefined results, and while some implementations do in fact do something, you can't rely on it to work portably.

As for the original problem, the best solution, sadly, is to not scanf() in the first place, but (to give one possible solution) to instead use fgets() to read the input into a string buffer, which you can then apply sscanf() to. This may sound like a strange way to do it, but it would ensure that the input doesn't get blocked up with unwanted characters, and allows more flexibility in how you parse the input.

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

First off, permit me to recommend a different representation of the cards, one which would almost certainly be easier to work with:

enum SUITS {HEARTS, CLUBS, DIAMONDS, SPADES};

struct Card 
{
    int face;
    SUITS suit;
};

Card deck[52];

Of course, your coursework may not have covered structures and enumerations yet, in which case this wouldn't help much.

That having been said, the real issue is that you are tangling up the representation of the cards, the process of displaying the cards, the process of shuffling the cards, and the process of dealing the cards, all in a single function. You'll want to separate these four issues as best as you can, preferably with separate functions.

Among other things, you'll want to make deck[] available to multiple functions, either by a) making it a global variable, b) passing it to the functions explicitly, or c) writing a Deck class and having the functions all be methods of the class. The first of these options is easiest, but also presents the most potential problems; the second is better, but a bit if a hassle. The third option is probably best, but I am assuming here that you haven't covered classes yet, so it isn't one you could use right now. Let's take approach b - paing the deck[] variable as a function argument - as our solution for the moment.

Backing up a bit, you'll want to have a way of displaying a given card, without having to …

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

Hard-coding is the practice of placing literal constants (e.g., the number 1.0) directly into the program code, rather than holding them in named constants. Repeated use of hard-coded values, or use of inobvious values, is generally frowned upon, as it can make maintenance harder (because if have to change it in the future, you have to know when the number 1.0 refers to the height of a cylinder, as opposed to, say, the angle of an unrelated triangle. What Taywin means here is that you can either have named constants for the two values, or - since they are trivial cases which only appear in two locations - you can simply repeat the default values directly in the original_cylinder() method.

I personally would use named constants, but that's a personal preference.

As for why you can't apply the default c'tor to the existing cylinder object, well, the whole purpose of the constructor is to create and return a new object. You can't replace an object with a new object from a method which is part of the original object itself, if you follow me.

cylinder::reset_cylinder() 
{
  silly.height = DEFAULT_HEIGHT;
  silly.diameter = DEFAULT_DIAMETER;
}
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Could you tell us what error messages you get, please? If I understand correctly, the syntax for defining a single uninitialized word variable in Turbo Assembler is

var dw ?

What exactly is going wrong with it as given?

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

I have bochs installed on my computer, and i have small boot disk OS from floppy.
Could someone help me how to boot floppy on bochs? I read the instructions from
bochs wiki but i'm still confuse.

The main thing you need to do is create a disk image. Fortunately, Bochs comes with a tool, bximage, which makes it easy to create a blank disk image, or (if you already have a disk) copy a disk image to a file.

You can also use a physical disk with Bochs, if you prefer, rather than a disk image file.

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

You're getting closer with this, but in this case, you do not want the double-quotes around the variable names; double quotes are used to demarcate the beginning and end of a string literal. Thus, you should try the following:

cout <<"Encrypted digit:" << digit_three << digit_four << digit_one << digit_two << endl; // display encrypted numbers

As a side note, I should mention that in the future you should always use [code] tags around any code samples you post here. This ensures that the source formatting is retained.

Getting back to the program: at the beginning of the program, you have a variable named main , which you declare as type int and assign the value of zero. I would expect this to conflict with the function name main() , or else get treated as an overload of that name. Either way, having a variable with the same name as a function is almost certain to result in confusion, if not on the part of the compiler then on the part of those reading the code later. I strongly recommend renaming the variable to something other than main .

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

Actually, the problem is that you are not enclosing the switch() block in braces, and as a result, only the first case is being seen as in the scope of the switch() statement. This is no different than if you had written an if statement like this:

if (something)
    statement1;
    statement2;
    statement3;

Despite the appearances due to the indentation, only statement1 is actually part of the if() statement. Thus, you actually want it to be like this:

switch (youngrat_choice)
 {
 case 1:
      cout<<"\nYou strike the young rat with your sword\n";
      youngrat_health = ( youngrat_health - attack );
      cout<<"You deal the enemy " << attack << " damage\n";
      
      if ( youngrat_health <= 0 )
      {
           cout<<"You defeated the young rat and earned 5 experience\n points and 12 gold coins\n";
           experience = (experience + 5);
           gold = (gold + 12);
           getch();
           
      } 
      
      else if ( youngrat_health > 0 )
      {
           cout<<"The young rath is still alive!!";
           getch();
           goto startfightyoungrat;   
           
           }
           
      
 case 2:
      cout<<"\nEnemys Stats: \n\nType: Young Rat\nHealth: " << youngrat_health << "/" << youngrat_maxhealth << " ";
      getch();
      goto menuyoungrat;
      
 case 3:          
         cout<<"Name: " << name << " ";
     cout<<"\nLevel: " << level << " ";
     cout<<"\nGold: " << gold << " ";
     cout<<"\nAttack: " << attack << " ";
     cout<<"\nExperience: " << experience << " ";
     cout<<"\nHealth: " << health << "/" << maxhealth << " ";
     
     getch();
     goto menuyoungrat;
     
     
  case 4:
       cout<<"\nYou run away in panic after being attacked\n";
       getch();
       village();   
      
 }
 getch();
  }
  else if ( level …
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

To get some of the theory behind this, see here. That discusses the role of linkers and inclusion headers, and how they are used in C and it's relatives. This may help put what Narue said into perspective. While what I wrote then focuses on reusable code libraries, the principles also apply to being able to break programs up into multiple files for the sake of modularity and clarity.

The key thing to understand is that yes, there is a main() function somewhere (or some equivalent to it, such as winMain() , depending on the environment in use), but not all of the compiled sections need to have there own main() in the file itself. When you compile a file, you only produce a part of the program; the rest of it has to be linked in before the final executable is completed. Most Integrated Development Environments take care of this for you to some extent, so you simply weren't aware of it before.

As gihanbw said, using an IDE tends to hide a lot of details which you really need to be at least aware of, if not necessarily actively using. You would learn the process a lot more clearly if you were to try compiling a few small programs (e.g., the one Narue posted) with the command line versions of your compiler and linker, just to see how the process works 'under the hood' as it were.

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

To quote one famous comment made many years ago, undefined behavior could mean that the compiler causes demons to fly out of your nose, if the compiler writer so chooses and is able to implement it.

Undefined means just that: that the compiler can give whatever results the compiler writer chooses, and there is no way to predict the behavior short of specific knowledge of the compiler itself. As far as the standard is concerned, code such as this could return a random result, or no result, or raise a compile error, it's all dependent on the way the compiler writers implemented that 'feature'.

This isn't to say that a given compiler won't have a consistent result, just that the result is not bound by the language standard. In a case like this one, the most likely compiler output would be something like this:

increment x         -> x == 1
add x to x          -> x == 2
increment x         -> x == 3
add x to x          -> x == 6
increment x         -> x == 7

However, that's just an accident of the particular compiler output, and the choices the compiler writer made about the order of events; it could just as easily have been:

add x to x          -> x == 0
increment x         -> x == 1
add x to x          -> x == 2
increment x         -> x == 3
add x to x          -> x == 6
increment x         -> …
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Let's break this down somewhat; the increment operator isn't specific to for() loops, and for that matter, for() loops don't necessarily use an increment.

Let's take the simplest case of a separate increment expression:

i++;

Now, whether this compiles to a single assembly language operation or not depends on the processor type, and on how the compiler optimizes the variables as well. On most processors, incrementing a register is a single, atomic operation, but incrementing a memory value may require a separate load and store, which would mean at least two and possibly three operations. Even if the CPU type has an 'increment memory' operation (like the x86 that most people use), it may be slower than the equivalent register operation, sometimes by orders of magnitude depending on all sorts of things such as cache misses. Thus, it all comes down to what processor it is on, and how the compiler paints the registers, and to some extent where in the memory the variable is.

Note that all of this isn't necessarily relevant to algorithmic time complexity; that's usually stated in a manner that abstracts away the actual time for the operations, focusing instead on how many repetitions are needed by the algorithm.

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

You don't have any return value for the case where counter does not equal 5, so the caller basically gets whatever junk happens to be in the returned value register - and since any value other than zero (false) evaluates to true, the return value is almost always going to be true.

Actually, I'm surprised that this compiled at all - at the very least it should have given you a warning that not all paths end in a returned value. While C++ is a lot looser than some other languages about this sort of thing - it goes on the assumption that the you know what you're doing - it still should have at least checked to make sure this wasn't an oversight.

Also, in the conditional, the result variable is unnecessary, as is the ' == true ' part, and the winner assignment as well; you could simplify the whole thing as just:

if((checkFlush(checkCards)))
    {
        return 5;
    }
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Printing the string is straightforward, and the same as you are doing with the other strings: you use the INT 21h, vector 09h DOS call.

lea dx, mm
	mov ah, 09h
	int 21h

You might want to see Ralf Brown's interrupt list for a full list of interrupts.

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

While toupper() is the correct function to use, you want to call it after you've read in the new value, not before. Probably the easiers way to do it is

switch( toupper(membership))
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

The main issue I see with this is that you don't have break statements at the end of the cases. This means that the first case will 'fall through' to the second, which results in the fee always being set to 20.

Nastadon commented: Thanks bro +1
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Just to make sure we're on the same page, here, is this the function you are referring to?

/* K&R's getline() function from p29 */
int getline(char s[], int lim)
{
  int c, i;

  for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
    s[i] = c;
  if(c == '\n')
  {
    s[i] = c;
    ++i;
  }
  s[i] = '\0';

  return i;
}
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Well, the first thing you need to know is that getline() is not a standard C function (as of the C99 standard, at least to the best of my knowledge). It is an extension added in the GNU version of the stream I/O library, and while it is widely supported, it is not necessarily part of any given compiler's library, and is not that widely used.

(There is also a getline() function in the C++ <iostream> library, where it is standard, but it is a completely different thing than the C function. If you aren't sure which language the code you're reading is in, then chances are it's C++; post part of it and someone here should be able to tell you.)

The main advantage of getline() is that it is more secure than, say, gets() , in that it takes a size_t* argument that indicates the actual size of the buffer the line is being read into, and calls realloc() to re-size the buffer if the input exceeds the original buffer size (this assumes that the buffer was dynamically allocated in the first place, otherwise it would fail rather atrociously). This ensures that a buffer overrun cannot happen (short of running out of memory entirely), eliminating a common source of bugs and security flaws.

WaltP commented: RTOP! -4
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

asitmahato: were you able to get NASM working with VS6? While I don't really recommend it, the instructions given above should have made it possible. If you were able to do it, could you mark the thread as solved so that we know?

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

Have you made any progress on this? Could you explain what you're trying to get the computer to do?

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

How do you use it for what, and where? the BYTE marker is meant to tell the assembler that you're working with a 1-byte value; you use it to remove ambiguity in places where the value could be either a byte or a word. I don't really see anything like that in your code.

BTW, you have added the string to ask for the size of the string, but you still don't print it out, or read in the size value.

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

What aspect of it do you need help with?

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

The conditional of the while() loop is incorrect. You want and AND operator (&&) there, not OR.

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

I'm not certain I'm following you, here; you should be able to nest any sort of loop inside of any other loop, and there's no need for an if() statement.

Can you explain just what you're trying to accomplish? I can hazard a few guesses as to what you might want, but it would make things much easier if you could explain your intentions, rather than asking for a specific mechanism.

On a hunch, it sounds like what you want is to have a loop which gets broken somewhere other than the loop conditional. If so, then you would indeed want (not necessarily need, but it would be a very limited case otherwise) to use an if() statement controlling a break statement. But as I say, that's just a guess - without knowing your specific intentions, I can't give a specific answer.

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

I just noticed another error in that code, I'm afraid: When removing the newline, it should be buffer[length - 1] , not buffer[length] . Rather careless of me, actually.

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

Useless. By definition fgets() adds the '\0' for you. And it will go into buffer[BUFFER_SIZE-1] if you fill up the entire buffer.

Eh? looks up reference on fgets() Why, so it does... thank you for correcting me on this part. I think I may have been confusing it with some of the delimited string functions such as strncpy() (which does not automatically delimit the string). Damn, I've been making this mistake consistently, too... well, now I know.

I apologize to anyone misled by this error. Again, thank you, WaltP, for correcting me on this.

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

Just noticed a careless error on my part:

movl    4(%ebp), %ecx
         movl    8(%ebp), %edx

should be

movl    12(%ebp), %ecx
         movl    16%ebp), %edx

I forgot to take the return address and %ecx into consideration. Sloppy of me.

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

What cse.avinash said about the re-declarations is correct; you don't need or want to declare those variables there. You want to use the parameters as the values for the input, not local variables.

I could also add that while you are reading in the student's sex from cin , you don't read in the year and exam marks. As a result, when you go to print them out, those will be zero (at best) or garbage (at worst).

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

It doesn't really matter if it is above or below the main() function, though if it is below it, you'd have to add a separate function prototype before the main() function. For now, it's easier to keep it above the main() .

What sort of problems do you get when you declare the variables as references? Be as specific as possible, and if you can, paste in the error messages you get.

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

No, at least not immediately; what fgets() does is it reads a character string into a buffer, which you then would have to interpret as the binary values. You have to write a function which will convert the sub-strings into the actual integer values.

I'm surprised that you are being given such a difficult task at such an early stage; while this isn't much work for anyone who knows C, it's not the easiest thing in the world for someone just starting in the language.

Anyway, this DevShed thread discusses a function for converting a binary string representation into an actual binary value.

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

Oh, I understand this. What I mean is, what does the code you've already written do? does it compile? If it does, does it run, and how does it behave when it runs?

As I said, I've already noted a number of issues, starting with the parameter list. The arguments are all supposed to be reference values, meaning the start of the function should look like this:

void inputAndValidate (char& Sex, int& YearMark, int& ExamMark)

The ampersand means that they are reference values, that it to say, the variable refers to the argument itself, not just it's value. This lets the function change the actual value of the variables passed to it.

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

What is actually going wrong? I can see a number of issues, but I would like to know what you specifically are trying to fix.

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

Oh, my. No, I think that you've misunderstood me, as well as misunderstanding how sscanf() works. What I was recommending was that you read the input into a character string, preferably using fgets() , then interpreting the binary values and the operator.

How about we start with a simpler sub-task: reading in the binary values and printing them back out. This isn't as simple as it sounds, as you'll have to do some manual manipulation of the input if we're going to be using fgets() . Still, this should give you some idea of a good direction to go:

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

#define BUFFER_SIZE 1023

int main()
{
    char choice = 'Y';
    unsigned length;
    char buffer[BUFFER_SIZE + 1];

    while (choice == 'Y')
    {
        printf("Enter a binary number: ");
        fgets(buffer, BUFFER_SIZE, stdin);

        buffer[BUFFER_SIZE] = '\0';        /* make sure that the string gets delimited */
        length = strlen(buffer);

        if (length < BUFFER_SIZE)
        {
            buffer[length] = '\0';        /* get rid of the trailing newline */
        }


        printf("You entered: %s\n", buffer);

        do
        {
            printf("Continue (Y or N) ");
            choice = toupper(fgetc(stdin));
            fgets(buffer, BUFFER_SIZE, stdin);   /* clear the system buffer */
        }
        while (choice != 'Y' && choice != 'N');

    }

    return 0;
}

Try compiling and running this and see if you understand how it works.

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

I am still a beginner in the c language, so i am still working on the correct format of my programs.

Fair enough. We were all beginners, once.

I still have to work out the format of this program, but i am having trouble finding how to code for the binary arithmetic.

Well, here's the first big hint: don't read the data in as a number. That won't work for this, because, first off, the scanf() function has no way of reading in binary numbers; second, you need to be able to read the operator and the second binary value in, which means reading the whole thing in as a string and parsing it (breaking it into the individual values); and third, you need to be able to know when the user enters the word 'exit'.

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

To begin with, you seem to be having trouble understanding the structure of functions such as main() . In the function implementation, you do not put a semi-colon at the end of the function header - you only do that with function prototypes, which you probably haven't covered yet. Also, you need to have braces around the body of the function, to mark where it begins and ends. The same applies to conditional statements such as if() - no semi-colon after the conditional, and the body of the conditional in braces. Finally, it is conventional to indent the code in one of several styles; I've demonstrated this below with a simple four spaces indent and flat braces.

//////Kyle Haas///////
/////Program 4//////

//this program allows the user to do arithmetic in binary

#include <stdio.h>
int main()
{
    unsigned choice, n;

    printf("binary number(+,-,*,/)binary number\n ")
    printf("type 'exit' to quit");
    scanf("%d" &n);

    if (choice = exit)
    {
        return 0;
    }
    else
       // what goes here?

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

add return type for BasicLCG(); and BasicLCG(unsigned long); It should solve you first 4 compilation error

Yes, but it does nothing for the redeclaration error immediately below it. C doesn't allow function overloading; you'll need to give the two different functions different names. Furthermore, those two functions are constructors in the original; there's no fixed equivalent of that in C.

hairo: If you don't mind me asking, why are you converting C++ code to C in the first place? Unlike going in the other direction, there are substantial problems in doing this, and probably little to be gained. What is this for? You might be better off writing the C version from scratch, just using the random number algorithm but writing new code.

Ancient Dragon commented: agree +17
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

I haven't tested this, but I believe it should work as a power function for integer values. You may want to test it yourself in a separate program.

int_pow:
         pushl   %ecx
         pushl   %ebp
         movl    %esp, %ebp
         movl    4(%ebp), %ecx
         movl    8(%ebp), %edx
         movl    $ecx, %eax         

power_loop:
         imul    %ecx
         decl    %edx
         jnz     power_loop   /* repeat until %edx is zero */

power_end:
         popl    %ebp
         popl    %ecx
         ret
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster

Regarding the numeric string, while "1234\0" is correct, in that it will work, you don't actually need the trailing null, as the string is declared .asciz already (which means a null is inserted at the end automatically by the assembler).

As for the %ebx, I think you'll still want to get a count of the elements in the string, as you'll need that to terminate the conversion loop. Also, you don't seem to see that the whole point of pushing the digits onto the stack is that you want to convert the string from the highest decimal place to the lowest; therefore, you need to start with the largest magnitude, and decrease it rather than increase it.

I think that this code should do what you want, though you'll need to replace the call to pow() with your own exponentiation function (the standard power function takes a double, not an int):

main:	
	movl	$1, %edi
	movl	$0, %ebx
	movl	$String, %ecx

character_push_loop:
	
	movb	(%ecx), %dl	/*Take one byte from the string and put in %dl*/
	cmpb	$0, %dl
	je	conversion_loop
	movb	%dl, (%eax)
	pushl	%eax		/*Push the byte on the stack*/
	incl	%ecx
	incl	%ebx
	jmp	character_push_loop

conversion_loop:

	decl	%ebx
        pushl   %ebx
        pushl   $10
        call    pow
        movl    %eax, %edi
        addl    8, %esp         /* clean up stack */     
	popl	%eax		/*pop off a character from the stack*/
	subl	$48, %eax	/*convert to integer*/
	imul	%edi		/* eax = eax*edi */
	addl	%eax, Intg
	cmpl	$0, %ebx
	je	end		/*When …