• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

C++ program issue compiling...

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Tacoman667

Member
Joined
Apr 28, 2001
Location
Kingwood, TX
I am writing this program for class. This assignment is to familiarize me with arrays. I am using Dev-C++ IDE and when I try to compile/run the program I get a wierd error but cannot make out where the issue is. I think it is pointing to my #include <iostream.h> but I am not positive. Take a look if you will please:

#include <iostream.h>
#include <conio.h>

int functUnits(int);

int main()
{
const int maxnum = 5;
int units[maxnum], x;
const float price[maxnum] = {9.92, 6.32, 12.63, 5.95, 10.29};
float amount[maxnum], total;

total = 0;

for (x = 0; x < maxnum; x++)
{
units[x] = functUnits(x);
amount[x] = price[x] * units[x];
}

cout << "\n Price Units Amount";
cout << "\n ----- ----- ------";

for (x = 0; x < maxnum; x++)
cout << "\n " << price[x] << units[x] << amount[x];

for (x = 0; x < maxnum; x++)
total = total + amount[x];

cout << "\n -------";
cout << "\nTotal: " << total << endl;

cout << "\n\n\nPress any key to continue...";
getch();

return 0;
}

int functUnits(int num)
{
int units[num];

cout << "\nPlease enter the amount of units: ";
cin >> units[num];

return num;
}


Thanks for any input anyone can provide!
 
First, you should have pasted the complete error (from the "log" tab)
WRT your #include. statements: this is C, not C++. Try the following:
Code:
#include <iostream>
#include <conio.h>
using namespace std;
 
Also, your function doesn't do anything but return the number that was passed to it.
 
This is what I get when I try to compile:



# Project: assignment 7
# Makefile created by Dev-C++ 4.9.9.0

CPP = g++.exe -D__DEBUG__
CC = gcc.exe -D__DEBUG__
WINDRES = windres.exe
RES =
OBJ = "assignment 7_.o" $(RES)
LINKOBJ = "assignment 7_.o" $(RES)
LIBS = -L"C:/Dev-Cpp/lib" -g3
INCS = -I"C:/Dev-Cpp/include"
CXXINCS = -I"C:/Dev-Cpp/include/c++/3.3.1" -I"C:/Dev-Cpp/include/c++/3.3.1/mingw32" -I"C:/Dev-Cpp/include/c++/3.3.1/backward" -I"C:/Dev-Cpp/lib/gcc-lib/mingw32/3.3.1/include" -I"C:/Dev-Cpp/include"
BIN = "assignment 7.exe"
CXXFLAGS = $(CXXINCS) -g3
CFLAGS = $(INCS) -g3

.PHONY: all all-before all-after clean clean-custom

all: all-before "assignment 7.exe" all-after


clean: clean-custom
rm -f $(OBJ) $(BIN)

$(BIN): $(OBJ)
$(CPP) $(LINKOBJ) -o "assignment 7.exe" $(LIBS)

"assignment 7_.o": assignment\ 7_.cpp
$(CPP) -c "assignment 7_.cpp" -o "assignment 7_.o" $(CXXFLAGS)



This last 2 lines is where it stops. I try to run and it says that the file isn't compiled yet. As for the "using namespace std;" statement, I don't think we have covered that part yet because I haven't used it yet and all of my other programs have run fine. Thanks for the suggestions thusfar, keep em commin!
 
iostream.h is an old style header which new compiliers arn't required to handle. It has been replaced by iostream, which requires the using namespace statement. Are you using the same compilier as your professor expects? If you have only been taught about iostream.h then the assignment isn't guarenteed to work on new standard compliant compiliers.

You also have a problem on line 42
Code:
int units[num];
You can only initialize an arrays size with a constant value. Since num is a variable, the code is invalid.

Notice that it does work on line 11
Code:
int units[maxnum], x;
Here maxnum is a const and therefore the code is valid.

I'm familiar with the environment you are using. Somewhere it should output errors that it finds including the lines it encounters them on. You should find where that information is. What command are you using to compile?
 
If you haven't used namespace in any of your previous programs, and they compiled, then we can eliminate that as a cause.

What you just posted weren't the actual error messages though. The error messages that the compiler throws are very helpful in finding and correcting the errors. Also, did you try making the suggested changes yet?

Actually another issue exists with that functUnits function. True it is a syntax error to initialize an array using a varaible, but it isn't necessary to be using an a array there. I am not sure how much expereience you have with C++, but using an array in that manner won't work. Everytime the function is called all of the information stored in the array is only available until the return statement from that function. Thus the array will only contain a value units[num] at any one time. It will never be filled with all of the values, which is what I am guessing you wanted it to do. If you want or need to use an array there it needs to be declared outside of the function, or as a static array.

Also as I said before it just passes back the number that was passed to it.

The only actual syntax error I can see is that previously mentioned array. And as it isn't needed, and wouldn't work as programmed anyway, just replace it with int units; and then return units.

Hope this helps you.
 
This is a introductory class. I really only have experience with QBasic and Pascal. I am required to use a function in this program but we haven't touched on subroutines yet, if there is such a thing in C++. I was using this function as a subroutine, is this wrong?
 
C++ doesn't use sub-routines like QBasic, only functions. Your basic idea of using the function is fine, just in execution it won't work the way you intend it to.

In a general format:
returnType functionName(parameters)
{
return someValue;
}

You are passing the function the value x, an array subscript and withing the function it is named num. What the function returns is num, but it never does anything to change its value. The function does however take a value and put it into units[num]. So you are losing the value that the user inputs, as the function will always return the subscript it was passed.

Also the way C++ deals with arrays and variables in general is different from QBasic. When the function is called any values declared in the fucntion will have space allocated to them. When the return statement is reached the space allocated to these local variables is put back as free space, and any values stored in them are now lost. This is called the scope. A variable declared inside of a function only has the scope of that function, so as soon as the variable (array units[] in this case) is out of scope, its values are lost. So when you call the function the first time units[0] is assigned some value. But when you call it the second time this value is lost, and units[1] will have a value.

There are ways around this, such as declaring the array outside of the function, or declaring it as a static. But these are probably beyond what you have learned anyway, and are not necesary for this project. The value being returned by the function is getting stored into an array, and this will hold your data. So even if the array inside of the function was being used properly, it would just be a waste of space. It would end up just being a copy of the array in main.

Did you get the compile errors taken care of yet? All of this is really just a logic error, and wouldn't cause the program not to compile. Except for the declaration of an array using a variable.

Hopefully this clears things up for you. If not just post back, and I would be happy to help you out more.
 
Here is a SS of the error log I get, I really do not see where there is an error...
 

Attachments

  • error log2.JPG
    error log2.JPG
    54.8 KB · Views: 51
FINALLY got this figured out. For some reason, after deleting all the associated files other then the .cpp file for this program, it finally compiled without issue. I fixed my logic errors and now she is working beautifully. Thanks to everyone who assisted me in this crap. I still love programming though! :)
 
Back