MandrewP 60 Junior Poster in Training

Hello,
I'm trying to create a data area within an .exe file. Then I will open the .exe file and scan it for the data area. When the data area is found, I will write some data to this data area and write the .exe file back to disk. I want to accomplish this by creating a character array in my source file like so:

char arr[] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";

Then that should be stored in the .exe file somewhere. The problem is, all of those A's don't appear anywhere in the .exe file . I know why, it's because the compiler ignored the array because I didn't reference it. If I reference it, by using a simply for loop and incrementing each element of the array, then those A's do appear in the .exe file as expected.

So, my question is, is there anyway to keep the compiler from deciding for me what I need or don't need in my code? So that I don't have to reference the array in order to get it included in the compilation process? I thought maybe this problem had to do with the compiler's optimization but I turned off the optimization feature in the compiler's configuration but with no luck. I'm using Visual Studio Community 2015. Any help would be appreciated.
Thanks.

MandrewP 60 Junior Poster in Training

Is double pre-incrementing undefined behavior? Like ++++x or even ++++++++x. I think it must be since it all occurs between sequence points. But what could possibly go wrong? Or do you just not ask that question and automatically stay away from any undefined operations? Is double incrementing ever routinely used? Thanks.

MandrewP 60 Junior Poster in Training

Every appliance has a watt rating listed on it somewhere. Watts tells how fast the electricity is being used, that is, at what rate. It's like a faucet that's running water, the "watts" would be the number of gallons coming out of the tap per minute. Turning the facuet on full blast would be a lot of "watts" and having the faucet on only a trickle would be a small number of "watts". Each appliance has a watt rating which tells how fast it's gobbling up energy.

You are charged for the total quanity of energy that you used, like how many gallons total or how many watts total. To calculate the total amount, you have to know the rate at which it was used and for how long. If the rate was 2 gallons per minute and the long was 10 minutes then the total amount you would be charged for would be 20 gallons. At .5 cent per gallon that would be 10 cents.

The same for electrical energy. You are billed by the total "quanity" used. Water is sold by the gallon, electricty is sold by the watt-hour. If an appliance was using electricity at the rate of 100 watts then after one hour that would be 100 watt hours. And a watt hour costs so much. If the appliance was using energy at a faster rate, like 1,000 watts, then for an hour that would equal 1,000 watt hours. That could also be called one kilowatt …

MandrewP 60 Junior Poster in Training

Thanks for the correction Ancient Dragon. But at least the effect is as if it happened as I stated, right?

MandrewP 60 Junior Poster in Training

Here is a slightly modified version of your code (in the function fun):

#include<iostream>
using namespace std;

int& fun()
{
   static int x = 10;
   cout << "In fun x = " << x << endl;
   return x;
}

int main()
{
   fun() = 30;
   cout << fun();

return 0;
}

The line static int x = 10; is only executed during the first call to the function, and after that the line is skipped on all subsequent calls to that function. An error would result if you tried to bring into existence the variable x if x already existed (which is does because it's declared static). So that line is skipped after the first call to the function. If you didn't use the keyword static then that line would be executed each time the function was called.

In Main, the line fun() = 30; is the first call to the function and so x is brought into existence and set to 10. Then right after that when x is returned (as a reference to x) then x is set to 30. Now since static int x = 10; is no longer executed anymore the function actually consists of just a return x statement (plus my cout statement : ). Try removing the static keyword from x and see what you get printed out on the screen.

An L value is something that can hold a value, like a memory location. An L value goes on the Left …

mike_2000_17 commented: Good explanation! +14
MandrewP 60 Junior Poster in Training

b=a++ will first initialise value of a to b then a get incremented should actually read:

b=a++ will first initialise b to the value of a then a gets incremented.

MandrewP 60 Junior Poster in Training

Of course, you will need to be able to determine if the integer is even or odd. Can some mathematical operation (add, subtract, multiply or divide) be done on the integer to determine if it is even or odd? (The answer of course is yes : )

MandrewP 60 Junior Poster in Training

Generally, you connect C++ code to circuitry by storing it inside a memory. Then some circuitry will read that code line by line and do what it says. A microcontroller is like that, it's a computer on a chip and will be under the control of the code stored in it's memory. A microcontroller can cost just a few dollars. To load C++ code into a micro controller, you do the following:

Write the C++ code on a PC using a compiler for the microcontroller you are using.
The compiler will compile the C++ code into what is called a Hex file. This Hex file
will be sent to a programmer (about $18.00) which connects to the USB port on your computer.
A cable from the programmer connects to a few of the pins on the microcontroller.

You then use some programming software (like Avrdude) to take the hex file and run it through the programmer to the microcontroller. This "burns" the code into the microcontroller's memory. When the microcontroller is turned on, it will start obeying the code in it's memory.

Programming a microcontroller is not really hard, you just have to understand a little bit of electronics, and then control the pins on the micrcontroller the way you want via your code.

MandrewP 60 Junior Poster in Training

So long as we are discussing prototypes, let me discuss why we need them in the first place. Actually, in the early days of C programming (and maybe still today?) you didn't need a prototype for a function. The compiler could translate the source code even if there was no prototype and the function was defined at the end of the program instead of before the main function.

If the compiler was going down through the code and suddenly, out of the blue, there appeared a function call like this: x = foo(a,b), then the compiler would just assume there was a function out there somewhere called foo, and it would just assume that it returned an int (if x was defined that way) and that it accepted an int and a double (if a and b were defined that way respectively). In effect, the compiler would use the function call itself as a prototype in order to do the translation into machine code. The problem is, it's all just an assumption. The compiler is just assuming that there is a function called foo that returns an int and accepts an int and a double, because that's what you said (or acted like : ). And that is all fine and dandy, so long as you didn't make any mistakes in writing the function call. But if you did, say, passing two integers instead of a int and double, then the stack would become corrupted and the program would crash …

MandrewP 60 Junior Poster in Training

As a program gets more complex, it's a good idea to use prototypes. One reason to use prototypes is because it makes it easier to see what parameters any particular function uses. If all of your prototypes are in alphabetical order and listed one after the other, it's easy to look up any function.

But a more important reason to use prototypes, and in some cases it will be mandatory, is because of the need to properly list the function definitions in the right order. For example, if function A calls function B then you have to define function B first. And if function A calls function B and function B calls function C then you have to define C first, then B then A. But what if function A calls function B and function B calls function A? Then there is no way to do this, unless you use prototypes.

So if your program gets to be complicated with many functions, do you really want to be burdened with making sure everything is defined in the right order? I don't think so, especially if you decide to change some things, like having function 14 call function 58 which already calls 13 which calls both 26 and 4. And who knows what functions 26 and 4 call! Just use prototypes in any order that you want and all of that mess is taken care of automatically.

Prototypes are a good thing. Just to be consistant, I always use prototypes, even …

alex9620 commented: Pretty good explanation and reason to use prototypes +0
MandrewP 60 Junior Poster in Training

Declaring variables is to create them which brings them into existence. You have to specify the type of variable that you are creating.

There are different types because there are different types of data to be represented. For example, there are integers which are simply whole numbers (like 1,2,3, 86, 1443, etc). Integers cannot represent fractional parts, like 2.5 or pi (3.141592653589). These kinds of numbers with fractional parts are called floating point numbers in computing. These floating point numbers (numbers with fractional parts) have to be represented in special ways inside the computer. Integers (only straight whole numbers) can be directly represented inside the computer. For example, if there is a number 8, then 8 is what is sitting inside the computer (inside it's memory or CPU register) in binary form (ones and zeros 8 = 1000, 10 = 1010, 46 = 101110). But if the number is fractional, then how do you represent 2.543 using ones and zeros? You can't directly, it has be encoded somehow. Obviously, because some kind of encoding scheme is used for floating point numbers they can't be directly read the way integers can be. They have to be decoded first and thus require more computing power.

Since there is a difference between integers and these floating point types, the compiler has to know this so it can handle each one respectively. That is why you have to declare the type when you create the variable. Sometimes you may only want to use whole …

mike_2000_17 commented: Nice and complete! +14
Tumlee commented: Far more detailed answer than I would have bothered with for such a question. +5
Moschops commented: Sure is a lot of effort here :) +8
MandrewP 60 Junior Poster in Training

Well, consider this: When you pass a pointer to a function, that pointer is being passed by value. So which is faster, passing a pointer by value or passing a value by value? I don't see how a pointer can be faster. If I am figuring this right, when the source code is translated into machine code, the machine code for dealing with a pointer would be a tad bit larger and take a little bit longer to address the data than directly addressing the data. But that may depend on the processor.

MandrewP 60 Junior Poster in Training

In line 40, in your function box_vol, the two identifiers "l" and "w" are strangers there. Once you are inside of the function, there is no way to see outside of it. So any mention of "l" and "w" outside of the function is not observable from within the function, and therefore the identities of these two variables cannot be determined.

MandrewP 60 Junior Poster in Training

You don't just write some code to send a HIGH output to a USB port. In days gone by you could simply write some code to send a byte to the parallel printer port, or more commonly to a serial port (com1 or com2). But things have gotten at lot more complicated with the USB ports.

However, there is a chip manufacturer that makes a chip which simulates an old fashion serial com port (virtual com port) from a USB port. This chip requires almost no additional components, it just hooks up to the USB cable on one end and you get your serial output (and input) from the other end of the chip. And the chip can even be powered by the USB's own power supply lines. The manufacturer also supplies a royalty free driver software for the chip that you can download for free from their website.

So I would think that this would be his path of least resistance in getting a program to output serial data (which can easily be converted into parallel data). With this setup, the operating system sees this as the usual USB port but your application sees this as a regular com port. You also will get much higher data rates than the old com ports gave, unless you set the thing up as an RS232 port, since the level shifting IC will slow things down to about 1 megabit per sec.

So then, with this setup, writing to the USB port …

MandrewP 60 Junior Poster in Training

I miss Narue. I wish she would come back and give us a piece of her sassy mind.

MandrewP 60 Junior Poster in Training

This code: cout<<(x=(x>='A'&&x<='Z')?x+=' ':x-=' ') works like this:

if(x = capital_letter)
   cout << x + 32;
else
   cout << x - 32;

Because this kind of "if" construct is used so often, the designers of C developed this statement using the conditional operator ?:

x = (test condition) ?  5 : 10;

If the test condition evaluates as true (non zero) then 5 is selected for x, else 10 is selected for x. In other words, the question is (?) is x going to equal 5 or else 10? If the test condition is true, then x will equal 5, else 10. So:

char = (char > 'Z') ? char -= 32 : char += 32;
cout << char;

The question above is, "Is char greater than capital Z? If so, then char is a lower case and needs to have 32 subtracted from it, else it is an upper case letter and needs 32 (' ') added to it. It all boils down to which one will be selected, the -32 or the +32 for char.

Like I said, this is just a one line replacement for the if - else statement above and does the same thing.

Asmaa_2 commented: I got it. Thanks alot (Y) +0
MandrewP 60 Junior Poster in Training

then whats the difference between using namespace and #include? doesnt #include also tells c++ to go look in this header file for this function or statement...Quoted Text Here

No, it doesn't tell the compiler to go look anywhere - the header file is simply "included" in the source file. The header files contain the declarations for the functions that may be used, but the actual functions themselves are still back in the library and will be linked in by the linker at link time, and the name of those functions start with std (or are qualified with).

Namespaces are like different towns or cities. You can have a street called "Main Street" in many different towns, and each "Main Street" is qualified by it's own town. For example,
Detroit::Main Street.
Dallas::Main Street
Bakersfield::Main Street

If you are living in Bakersfield, then you can refer to Main Street by itself, because you are already in Bakersfield so that Main Street is assumed. But if you are in Bakersfield and you are referring to the Main Street in Detroit, then you must use the namespace "Detroit" to indicate that: Detroit::Main Street.

Just because you include a library ( like <cmath> ) doesn't change the fact that the functions in that library are in a different "town" and need to have their names quailified by the "town" name.

MandrewP 60 Junior Poster in Training

The right most digit is the 1 or 0 that is being compressed, and the remaining 3 digits to the left code how many of those 1's or 0's there are.

1011 = 101 1's - i.e., 5 ones.
1110 = 111 0's - i.e., 7 zeros.

MandrewP 60 Junior Poster in Training

If I have a function such as Funct(int *arg1, int *arg2) , how do I use this function in a program.

You use this function the same way you use any function, you pass the specified parameter(s) to it. If the two parameters are pointers (to an int) then those two pointers must be in existance somewhere in the calling function (main) in order to be passed to the function. For integers:

void foo(int, int)  // function prototype

int main()
{
   int x, y;  // x and y exist in main.
   foo(x,y);  //copies of x and y are passed to foo.
}

For pointers:

void foo(int*, int*)  //function prototype - foo accepts two pointers to type int.

int main()
{
   int x = 0, y = 0;
   int* ptr1 = &x;  //pointer to type int exists in main.
   int* ptr2 = &y;  //pointer to type int exists in main.

   foo(ptr1, ptr2);  //copies of the two pointers are passed to foo.
   return 0;
 }

 void foo(int* p1, int* p2)
 {
   *p1 = 10;
   *p2 = 20;
 }

Above, the function foo() assigns x back in main() the value of 10 and y the value of 20. So a copy the two pointers in main, ptr1 and ptr2, are simply passed to foo(), and since they have the addresses of x and y they can alter the content of those variables from within foo() (although they are called p1 and p2 in foo).

MandrewP 60 Junior Poster in Training

Well, how do you propose I fix that because it only says that for triangle and technically, I did the same thing for all the shapes and it keeps talking about the first one.

You did not do the same thing for all of the shapes, this is the same for all the shapes:

int a,b,c,d;

But here is what you did:

int a = e, b,c,d;

Here, a = e which is not the same as the others. In fact, e doesn't even exist at this point. Now look what you did:

USHORT shape1 = triangle, square, rectangle, circle;

How can shape1 be assigned the VALUE of triangle when triangle doesn't even exist yet?? Here is what that above line of code is saying: Create a type unsigned short variable and name it shape1. Then whatever value triangle holds assign that to shape1. After that, create 3 more unsigned short variables and name them square, rectangle and circle. Like I said, you are trying to assign the value in triangle to shape1 when it doesn't exist at this point in your code. That is what the compiler is complaining about.

MandrewP 60 Junior Poster in Training

In line 13 you are assigning the value of triangle to shape1. The problem is, triangle doesn't even exist (at least not at this point) so the compiler is complaining that triangle has not been defined (i.e., brought into existance).

MandrewP 60 Junior Poster in Training

Sorry, you have to help yourself first. Make an effort at writing the code yourself, and post it here so we can see it and help you to carry on.

MandrewP 60 Junior Poster in Training

Basically, what you could do is just declare your three variables in main(), since that is where you want them. Then, with the long arm of your function, reach all the way over into main() and manipulate those variables directly. You do this by passing a reference or pointer to those variables to the function. That way, the function has "remote control" from afar of those variables right where they are at, and this makes things much simpler.

MandrewP 60 Junior Poster in Training

Look at your if statements - at least one of them has to execute in order to not print twice. So if there is a double printout, then neither if executed, thus both elses did.

Frankly, I think something else is going on somewhere else in the code which isn't shown here.

MandrewP 60 Junior Poster in Training

Windows will keep an eye on your data accesses and if you go outside the bounds of your program's allocated space Windows will shut it down ("Your program has encountered a problem and needs to close"). Because Windows is a multitasking operating system it won't allow a program to mess with memory outside of it's own allocated memory.

You could set a pointer to point outside your array, and by chance still be within your program's allocated space, in which case the pointer would just be messing with the other data within your program.

MandrewP 60 Junior Poster in Training

You are asked to write a program that manages a company's stock...

So ..... what's the hold up?

MandrewP 60 Junior Poster in Training
  if(true)
      do_this;
   else if(true)
      do_this;
   else if(true)
      do_this;
   else if(true)
      do_this;
   else if(true)
      do_this;

This is called a decesion tree, and only one if statement will execute out of the whole bunch. When one of the if statements does execute, then control drops out to the bottom bypassing all the other remaining if statements.

Actually, they are not called else-if statements but rather they are the regular if-else statements just written that way for clarity.

MandrewP 60 Junior Poster in Training

Well, if you really want to learn, then I think we should start learning the basics of electronics, since computers are electronic. Once you know some electronics, we can breadboard some simple digital circuits, like “and" and “or” gates and some simple adders and flip flops, counters, etc. Then we can move on to building some Atmel microcontroller circuits and program them in assembly language. Then we can move up to programming the CPU (the Pentium processor) inside your Intel PC. This we would do in basic assembly language as well, since most compilers convert our high level English statements in C++ (or whatever high level language we’re using) into assembly before hitting the floor. Then we can start learning a language like C++.

So, let’s do this. We can start with the measly electron and it’s negative attitude.

To be continued later........ (for the next 10 years!) NOT!

MandrewP 60 Junior Poster in Training

Also, whatever algorithm you use to generate lines 1 - 4 you may be able to use for lines 6 - 9 by just reversing the "direction".

MandrewP 60 Junior Poster in Training

Each of your two .cpp files are treated as separate source files by the compiler and are compiled separately. In each source file you have included "Resistor.h", and thus in each source file those objects in "Resistor.h" are created. No problem until the linker trys to combine the two compiled source files and finds dupilcate objects created, when there should be only one set of objects in existance which are shared or used by both source files.

MandrewP 60 Junior Poster in Training

Maybe strings are giving you errors because you forgot to declare them - just like you forgot to post your code.

MandrewP 60 Junior Poster in Training

Check that all of the variables have been initalized before use.

MandrewP 60 Junior Poster in Training

When you get the file from the desktop, is the file to be printed to the screen, to the printer or sent somewhere else? What line in your code shows this?

MandrewP 60 Junior Poster in Training

HELP!! C++ HELP NEEDED URGENTLY

No this is not a school assignment...just something i thought i'd practice

MandrewP 60 Junior Poster in Training

You should post all of your code the way it actually appears so we can check it out. A misspelling or a missed ";" can give weird errors.

MandrewP 60 Junior Poster in Training

The function isn't called "int" math - it's just called math() (which happens to return an int.) So in your program, you now have a function called math and a variable of type int called math (line 9). You can't have both with the same name except under special circumstances. So you need to remove int math on line 9.

MandrewP 60 Junior Poster in Training

That is just warning you that the variables that you are using have not been asigned any values. They contain only junk values that just happen to be in memory at that time. Why are you adding them up, as though you expect some kind of useful result? Or do you think that math will now equal the sum of those variables forever more? It doesn't work that way - math will equal the sum of those variables only as they exist now, and when they cahnge it will not reflect again in math.

MandrewP 60 Junior Poster in Training

On line 41, while should be on the other side of the closing brace, like this: } while(ans == 'y'). Fix that and see if that corrects the problem. Also, the closing brace for main() doesn't have a ";" after it. Remove that too.

MandrewP 60 Junior Poster in Training

Just include the <string> header - #include<string>

MandrewP 60 Junior Poster in Training

Yes, I would do a bubblesort. If you have five numbers, 84692, you would basically sort them like this:

compare the 8 and 4, if 8 is greater then swap them - 48692
compare the 8 and 6, if 8 is greater then swap them - 46892
compare the 8 and 9, if 8 is greater then swap them - 46892
compare the 9 and 2, if 9 is greater then swap them - 46829

Now repeat that whole process again:

compare the 4 and 6, if 4 is greater then swap them - 46829
compare the 6 and 8, if 6 is greater then swap them - 46829
compare the 8 and 2, if 8 is greater then swap them - 46289
compare the 8 and 9, if 8 is greater then swap them - 46289

Now repeat that whole process again:

compare the 4 and 6, if 4 is greater then swap them - 46829
etc..

You just keep repeating that process until all elements are in numerical order. So you can see that you are going to need two loops - an inner loop and an outer loop. Notice how the 2 gradually moves from the left to the right. That is why this is called a bubblesort, the numbers "bubble" up (or along) until they are all in order.

This is the basic bubblesort algorithm, but it can be made more efficient. I'll leave you to "sort" out the details.

MandrewP 60 Junior Poster in Training

Each source file is compiled separately, so only the file that has the included header will have "var". And only one file can have the header file included.

MandrewP 60 Junior Poster in Training

If you have source code written in a high level language like C++, then:

You would use compiler A to translate it into machine code that processor A understands........
You would use compiler B to translate it into machine code that processor B understands........
You would use compiler C to translate it into machine code that processor C understands........
Etc.

So a high level program can be translated into any of the different machine languages simply by using the right compiler that targets the particular processor of interest.

MandrewP 60 Junior Poster in Training

OK, here is why your program is doing what it's doing:

In line 7, row is set to 1, then the for loop at line 9 loops 5 times, and when it does, row does not change, does it?
After the for loop at line 9 loops 5 times then the for loop at line 7 increments row to the value of 2, and then the for loop at line 9 executes again 5 times in which row remains the same during the 5 loops with only column changing.

In line 10, you are printing out column and then row right after that, and then repeating that 5 times. So row always comes after column, so then row must always be 1 larger than column, right?? What would cause that? All you need to do is print out 12345 and then go down a line and do it again. That can't be that hard, is it? I think that you are over thinking this deal. : )

MandrewP 60 Junior Poster in Training

I know it's already been 3 weeks, but if you are still interested, here is a short tutorial on arrays that I did in response to a poster about a month ago for C++ :

Click Here

MandrewP 60 Junior Poster in Training

Once you change fstream.h to fstream the program reads the file (for me anyway). If it still doesn't read the file for you as you stated, then there is another problem unrelated to "using namespace std" and "fstream". Maybe your doc.txt file is not in the right path and so the file is not found? But the file reads and prints out for me just fine.

MandrewP 60 Junior Poster in Training

Here are the two key questions that you need to understand for case_1 & 2:

1). In case_1 and case_2, the value of n is changed, but the function did not change it. Why then did n change?
2). The value of n was passed to the function, so why didn't the function change the printed out value of n?

Keep in mind that the function returns no value and this is irrelevant as to why n is not changed when printed out. In case_3 the function doesn't return a value either, yet it changes n. So what's going on? You need to know and be able to articulate it clearly in your own words.

So think more about it and take another crack at answering it. We can help you out from there. These are key points that you really need to clearly grasp, for it will help you when you are using functions. Otherwise, you will be struggling when trying to use functions. Might as well get it clear now.

MandrewP 60 Junior Poster in Training

Ooops, findMax(int[max], x) is incorrect. Sorry. But looking over your code, it seems as if you are trying to calculate the maximum or minimum number from a series of numbers that you input, right? But I don't see where you even input any series of numbers. So is this right? If so, then your code is way off. Let me know and I can outline your code and show you where you are making your errors.

MandrewP 60 Junior Poster in Training

Your two functions, findMax() and findMin(), take a pointer as the first parameter because when passing an array to a function the whole array is not passed, but just a pointer to the start of the array. So the first parameter in those two functions are pointers and you are trying to pass an int there. And the compiler is complaining that it can't convert the int that you are passing to the pointer type that is required.

You probably want to do this instead:

findMax(int[max],x);
MandrewP 60 Junior Poster in Training

Case_1 isn't really accurate when you say foo() doesn't return a value SO it prints a garbage vaule, because functions often change the value of a variable directly without ever returning a value. This is accomplished by using pointers and refereneces, or by directly changing a global variable. So the fact that foo() doesn't return anything isn't of much relevence.

MandrewP 60 Junior Poster in Training

Is this about knowing whether or not a program has been run before or is this about something else?