I am new to C++ and still trying basic programs. Here i am trying to get the sum of two arrays call A and B. I always get the sum of first array by adding one to the sum . As an exsample . if i feed 2,2,2,. I get 7 . if i feed 3,2,3. i get 9. But second array produce the correct figure. Last comparison is also working fine. Need to know whay first array produce a wrong figure.
Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
After everything that has been said consider this:
#include <iostream>
usingnamespace std;
int main()
{
int region1[12]{};
int region2[12]{};
int sumRegion1{};
int sumRegion2{};
int numOfMonths{};
cout << "Enter Number of months" << endl;
cin >> numOfMonths;
cout << "\nEnter sales figures of region1" << endl;
for (int x = 0; x < numOfMonths; x++)
{
std::cout << "Enter month " << x + 1 << ": ";
cin >> region1[x];
//cout << region1[x] << ' ';
sumRegion1 += region1[x];
}
std::cout << '\n';
for (int x = 0; x < numOfMonths; x++)
cout << region1[x] << ' ';
cout << endl;
//for (int x = 0; x < numOfMonths; x++)
// sumRegion1 += region1[x];
cout << "\nTotal = " << sumRegion1 << ' ' << endl;
A good variable name is a big help especially in larger programs.
The empty {}s known as the uniform initializer will set variables to some form of (0) zero depending on the type of the variable. This available from C++11 on. You can also put something inside the {}s to give the variable a starting value. It is a good idea to always initialize you variables when defined. If fo no other reason to know that they do not contain garbage.
I have only shown 1 part, but the first for loop can do more than 1 thing and it is a good practice to output a prompt before a "cin" statement, so that the user knows what to enter. Leaving out any new line, (\n) or "endl" at the end of the prompt will put the "cin" on the same line.
The variable names "i", "j" and "k" are most often used in for loops, but this is not required just more common.
When posting code of any size it is best to use code tags, see previous post, and indent properly. Using code tags will keep the indenting if it was there to begin with.
1 2 3 4 5 6 7
for (int x = 0; x < numOfMonths; x++)
{
std::cout << " Enter month " << x + 1 << ": ";
cin >> region1[x];
//cout << region1[x] << ' ';
sumRegion1 += region1[x];
}
When I first tried this I also thought of doing everything in 1 for loop until I saw the output on the screen. At first I put a comment on that line to test it, but I should have deleted that line. It is to soon to print out the input.
When you say x doesnt recognize in last line. You will have to explain this better. When I ran the for loop in VS 2017 "x" has the proper value in each line it is used.
When I tested the program here the for loop worked correctly.
If you are not sure about the value of "x" you could try this:
1 2 3 4 5 6 7 8 9
for (int x = 0; x < numOfMonths; x++)
{
std::cout << " Enter month " << x + 1 << ": ";
cin >> region1[x];
sumRegion1 += region1[x];
std::cout << "\n The value of \"x\" is: " << x << '\n'; // <--- Used for testing. Comment or remove when finished.
}
The output to the screen does not look very nice, but it is for testing.