Hello Theloneinteger,
After seeing what
salem c and
Thomas1965 have said I came up with this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
# include<iostream>
using namespace std;
int main()
{
constexpr int MAXROW{ 1 }; // <--- Should be 2.
constexpr int MAXCOL{ 4 }; // <--- Should be 6.
int bonus[MAXROW][MAXCOL];
int totalKaren{}, totalMurphy{}, col{};
while (col < 4)
{
// <--- Needs a prompt.
cin >> bonus[0][col];
totalKaren += bonus[0][col];
// <--- Needs a prompt.
cin >> bonus[1][col]; // <--- Row out of bounds.
totalMurphy += bonus[1][col]; // <--- Row out of bounds.
col++;
}
cout << "Karen's total tally is: " << totalKaren << endl;
cout << "Murphy's total tally is: " << totalMurphy << endl;
}
|
The blank lines help the readability of the code and make it much easier to follow. A good variable name also helps, I am not saying what I did is a good name, but it is just to give you an idea, this will be helpful in the future.
Lines 17 and 21 are a shorter way of writing what you did.
C++ is one of several (0) zero based languages, so when you write
int bonus[1][4];
you are saying that you have 1 row (0) zero and 4 columns 0 - 3. When you get to
cin>>bonus[1][i];
the row is outside the boundaries of the array and you do not know what memory location you are writing to. That is where your program crashes.
When it comes to {}s do not mix them. Pick a style and be consistent with its use.
https://en.wikipedia.org/wiki/Indentation_style#Brace_placement_in_compound_statements for some ideas. When it comes to the code the first goal is to make it as easy to read and follow as you can, The first benefit it to you.
I hope this gives you some ideas that you can use.
Andy