## Summary:

I have an error message I don't understand.
I have an error on this program I don't understand, it said stack corrupted around the 2 dimension array sales.
C++:
//2D sales in divisions in quarters.
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
const int numDiv = 3;// 3 divisions
const int numQtr = 4; // 4 quarters
double sales[numDiv][numQtr];//2D array for number of divisions and number of quarters
double totalSales = 0;
int div=1, qtr=1;

cout << " This program calculate the total sales of all divisions.\n";
cout << " Enter the following sales information: \n\n";
for (div = 1; div <= numDiv; div++)
{
for (qtr = 1; qtr <= 4; qtr++)
{
cout << " Enter sales figure of division " << div << ", quarter " << qtr << "= $"; cin >> sales[div][qtr]; cout << "\n"; } cout << endl; } for (div = 1; div <= numDiv; div++) { for (qtr = 1; qtr <= numQtr; qtr++) { totalSales += sales[div][qtr]; } } cout << " The total sales for 3 divisions in 4 quarters is$" << totalSales << ". \n\n";
return 0;
}
The error message is:

Thanks

Last edited by a moderator:

Related Programming and Computer Science News on Phys.org
jedishrfu
Mentor
Because it’s defined inside main() it’s placed on the stack.

C arrays index from zero to their length-1

your two for loops are indexing from 1 to length meaning you’ve indexed outside the array bounds.

DaveE, FactChecker, berkeman and 2 others
Because it’s defined inside main() it’s placed on the stack.

C arrays index from zero to their length-1

your two for loops are indexing from 1 to length meaning you’ve indexed outside the array bounds.
Thanks. I was experimenting, if I go from 0 to 3, it's fine.

Now I know.

thanks

berkeman
The initial reply was spot on. I'm impressed that the runtime environment was able to name the array near the corruption.

It occurs to me that I've been programming in C++ for almost 20 years and I've never used cout or ever included iostream for that matter. I wonder what else I've never used.

strangerep
I wonder what else [in C++] I've never used.
I'd guess you've never used this. Try it -- you'll like it.

I'd guess you've never used this. Try it -- you'll like it.
I have the book of course. It's the user-space libraries I don't have. For instance, in the program in the OP, endl in line 23 seems undeclared. It compiled, so it must be something global from the libraries, but I don't know that because I've never used them.

I program for file servers at the multi-thread OS level, so there are no standard libraries, no user space from which other processes might be run, not even a main(). There's just the one process, ~10,000 threads with very limited stacks, always competing with itself.

Mark44
Mentor
It's the user-space libraries I don't have. For instance, in the program in the OP, endl in line 23 seems undeclared. It compiled, so it must be something global from the libraries, but I don't know that because I've never used them.
The line in @yungman's code near the top, using namespace std; allows everything in that namespace to be used without having to be qualified. Lots of C++ textbooks do this, which is OK for small examples, but it's considered a very bad practice to do so, especially at global scope. The std namespace is very large, so there's the potential of a collision if you happen to choose a variable name that's also declared in the std namespace.

Here's an example I found that highlights this problem, showing code that will not compile.
C++:
#include <iostream>
using namespace std;

int count = 1;
int main() {
cout << count << endl;
}
The symbol count, as declared, is ambiguous, since count is also a symbol declared in std., so in the output statement, the compiler can't determine which count is intended, the one at file scope here or the one in the std namespace. Two possible fixes:
1) move the definition of count inside main(), making it local to main()
2) use the scope operator, ::, in the output statement, like so.
C++:
 cout << ::count << endl;

Mark44
Mentor
@yungman, this is a habit you need to break, possibly left over from writing Fortran code.
for (div = 1; div <= numDiv; div++)
C and C++ arrays are indexed starting from 0, as already mentioned.

This is what you should have had for both nested loops.
C++:
for (div = 0; div < numDiv; div++)
{
for (qtr = 0; qtr < numQtr; qtr++)

yungman
@yungman, this is a habit you need to break, possibly left over from writing Fortran code.
C and C++ arrays are indexed starting from 0, as already mentioned.

This is what you should have had for both nested loops.
C++:
for (div = 0; div < numDiv; div++)
{
for (qtr = 0; qtr < numQtr; qtr++)
Yes, did I learn my lesson this time. Caused me over an hour!!!

I am glad this thread is open up again, I hate to keep starting new threads all the time. I don't want to be a pest here.............At least not as obvious!!!

Mark44
Mentor
I am glad this thread is open up again, I hate to keep starting new threads all the time.
This is a different thread from your earlier one that went to almost 400 posts. For different questions, we prefer that they go in separate threads.

This is a different thread from your earlier one that went to almost 400 posts. For different questions, we prefer that they go in separate threads.
Hopefully I won't have as many questions as before, a good book really helps, also, when I google now, some of them actually feels like it's written in English, not like before that they all looked Russian to me. It helps when I know more about C++. Also, I try to keep good notes, now I even put pages on the notes so I can go back to the book if there are still question.

#### Attachments

• 58.4 KB Views: 10
I have a very simple question. Does the source.cpp file work with other compilers? I know a lot of people here use VS, so we can post our .cpp here. How about someone with other compilers?

Mark44
Mentor
Does the source.cpp file work with other compilers?
Are you asking about the file extension or the contents of the file?
If it's the extension, the gcc and g++ compilers work with that extension and a few others. If you're asking about the contents of the file, for what you're doing, pretty much any c++ compiler should accept it. However, there are some Microsoft-specific extensions that Visual Studio will accept that other compilers won't or probably won't accept, including such keywords as __int16, __int32, __asm, and others.

yungman