#include <iostream>
#include <fstream>
usingnamespace std;
ifstream infile;
ofstream outfile;
int main ()
{
int num;
int value [10];
int sum=0;
infile.open("E:\\CFiles\\DataFile4.txt");
for (num=0;num<=9;num++)
{
infile>>value[num];
cout<<value[num]<<" ";
}
if (num=3)
{
cout<<endl<<endl;
infile>>value[num];
cout<<value[num]<<endl;
cout<<endl;
}
for (num=5;num<=9;num++)
{
infile>>value[num];
sum=sum+value[num];
cout<<sum<<endl;
}
}
My question is about line 33. I want to output ONLY the total sum of nums 5-9, but it's outputting each incremental step. 5+6, 6+7, etc. What should I change?
I assume with if (num=3) you meant if (num==3) , = assigns, == compares. In either case, it's pointless. If you are assigning then there is no need for the if-statement, and if you're comparing the block will never execute because you left num==9 at the end of your for-loop. Also, avoid using global variables in for-loops, they allow for the declaration and definition of a new variable, like so
1 2 3 4 5 6
for (int a=0; a<10; a++)
{
//code
}
Additionally, I don't understand why you take in 10 values, put them in the array, then take in another 5 values, put them in the last 5 spaces of the array and add those last five values. If that's what you meant to do, then ignore my comment.