Still in class for intro to C++ and I'm working on one of the labs. In this lab, the program prompts the user to input a number (x), from there the program sums up the numbers from 1 up to (x). For example:
input sum
1 1
2 3 //(2+1)
3 6 //(3+2+1)
4 10 //(4+3+2+1)
5 15 //(5+4+3+2+1)
etc... etc...
This is the code for the function called by main:
int find_sum(int value)
{
int i=0;
i=value;
if (value==1)
{
sum=value;
}
else
{
for(i=0; i<=value; ++i)
{
sum=value+i;
}
}
return sum;
}
I know something is wrong in the loop, but can't figure out what. I've tried several different loops and nesting just about everything I know of in C++ but still missing something.....(something simple, I'm sure). Just need some fresh eyes to look at this, thanks for your time,
Tony

beginner C++
nasr0001
Tony
Potyos
Tony
tobyxu
Tony
HarmonicSoftware
i=value;
This is redundant. You can put int i = value, but either way, i is not used her, it's reset to 0 at the start of the loop which does use it. You shouldn't make variables visible outside the scope where they are needed.
I don't see anywhere that sum ( which I assume is a member variable ) is reset to 0. Is it always o
sum=value+i;
You probably mean sum = sum + i; You can also start the loop at 1, because adding 0 won't do anthing :-)
int find_sum(int value)
{
int sum=1;
if (value!=1)
{
for(int i=1; i<=value; ++i)
{
sum=sum+i;
}
}
return sum;
}
Dgates123
The core difference is that one is standard C++ and one is not. VC2005 no longer has the non standard headers, so if you get into the habit of using iostream.h, two things will happen
1. your code will be less portable
2. if your code tries to work with code that uses the c++ headers, you will have problems.
I'm not sure that there are tons of differences, the obvious one is namespace std, but it's just better to use the 'real' one. And I like being able to explicitly state what's coming into scope from that #include statement.
I apologize for all the questions
Not at all, that's what these forums are for. Good luck with your exams :-)
ko712000
OK - that wasn't going to work, because you don't keep adding to sum in your code. You'd end up with value * 2 being the sum, because each time, sum is not incremented, it is changed to i + value, and the last value for i is the same as value. If you replace your function with the code below, it should work:
int find_sum(int value)
{
sum=0;
for(int i=1; i<=value; ++i)
{
sum += i;
}
return sum;
}
I've used += to increment sum by the value of i, and removed the if statement ( the code I posted before had a bug, it would be off by 1 ).
Also, iostream.h is not valid C++, it comes from the version of C++ that predates the C++ standard. You should replace
#include <iostream.h>
with
#include <iostream>
using namespace std;
Or, you could do even better and replace
using namespace std;
with
using std::cin;
using std::cout;
Ross Goodell
That's a hell of schedule, I hope it all pays off for you.
Glad to help, from the sound of it, I'll probably get the chance to help some more before the week is out :-)
djee
This is the entire program:
#include <iostream.h>
int find_sum(int);
int sum=0, value;
int main()
{
char answer='y';
do{
cout << "\n Please enter value to sum. ";
cin >> value;
find_sum(value);
cout << "\n Your sum is " << sum << endl;
cout << "\n Want to sum again y=yes, n=no ";
cin >> answer;
}while (answer=='y'||answer=='Y');
return 0;
}
/*========================================================================*/
int find_sum(int value)
{
int i;
i=value;
if (value==1)
{
sum=value;
}
else
{
for(i=0; i<=value; ++i)
{
sum=value+i;
}
}
return sum;
}
What I was trying to do is use the loop counter to add previous numbers to the value the user entered, (user enters 5, the loop would run through 4 times adding the counter to the variable "value", if that made any sense, lol) basically if they entered 5, the 5 being "value" is passed to the called function, from there it would add all numbers less than or equal to 5(5+4+3+2+1=15, 15=sum) and sum, would be passed back to main where the "sum" would be displayed through cout.