I'm trying to write a function called (sumTo) that takes as arguments two integers. My function should use a loop to sum all of the numbers between the two (This includes the starting and ending numbers) and return the sum.
I want the function to allow for either argument to be the larger. for instance, passing the numbers 1, 5 or 5, 1 should return the same sum

From main I want to ask the user for two numbers, call the function, and print the sum when the function returns.
Here is an example:

Enter two number
1 5

The sum of all the numbers is 15

Here is the code I have to far? I've been working on this for a couple hours and have really confused myself, any help is greatly appreciated... Thank you in advance!!

#include<iostream>
using namespace std;
int main()
{
	int x,y;
	int sum;
	int low;
	int high;
cout<<"please input two integers " <<endl;
cin >> x >> y;

}
int sumTo()
for (int i = low; i<high; i++)
{
	if(x <= y)
    {
        for (int i = x; i<=y; i++)
        {
            sum+=i;
        }
        cout<<sum;
    }
    else
    {
        for (int i = y; i<=x; i++)
        {
            sum+=i;
        }
        cout<<sum;
    }
	cout<< "the sum of your number is: " <<sum+=i <<endl;
}
return 0;
}

Not sure why you are making it so complicated and you are using a int function when the way you have it you could use a void function.

int SumIt(int x, int y)
{
    if( x == y )
        return x;

    int high = x, low = x, sum = 0;

    if( y < x )
        low = y;
    else
        high = y;

    for( int i = low; i <= high; i++ ) //start from the low number and up until the highest number
        sum +=  i;

    return sum;
}

Unfortunately line 12 ends main() and the rest, as written, is ignored.

I'd try something like this:

if (x == y)
 sum = 2 * x;
else
 if(x < y)
  low = x;
  high = y;
 else else
  low = y;
  high = x;
 for(int i = low; i <= high; ++i)
  sum += i;

Hi again,
First of all, your code is really messy. You can't write functions inside of main. You should write your function after main concludes. Also your function needs to be enclosed in braces{ }. Finally, you need to provide a function prototype before main.
Your code should look something like:

#include<iostream>
using namespace std;
int sumTo( int x, int y); //function prototype
int main()
{
    int a,b;
    cout<<"please input two integers " <<endl;
    cin >> a >> b;
    int sum = sumTo(a,b); //call the function
    cout<<sum;
    

return 0;

}//end main

int sumTo(int x, int y)
{
   //write code for the function here
    return sum;
}//end function

You are making progress. Keep it up and good luck.

I have got a working code now it is as follows:

#include<iostream>
using namespace std;
int sumTo( int x, int y); //function 
int main()
{
	int x,y;
    cout<<"please input two integers " <<endl;
    cin >> x >> y;
    int sum = sumTo(x,y); //this is how I call the function
    cout<<sum<<" is the sum of all the numbers from " <<x << " and "<<y<<endl;
    
return 0;

}//end main, DON'T FORGET TO END MAIN, took me 2 hrs to realize this!!

int sumTo(int x, int y)
{

	if( x == y )
        return x;

    int high = x, low = x, sum = 0;

    if( y < x )
        low = y;
    else
        high = y;

    for( int i = low; i <= high; i++ ) 
        sum +=  i;

    return sum;
   }

Hi again,
First of all, your code is really messy. You can't write functions inside of main. You should write your function after main concludes. Also your function needs to be enclosed in braces{ }. Finally, you need to provide a function prototype before main.
Your code should look something like:

#include<iostream>
using namespace std;
int sumTo( int x, int y); //function prototype
int main()
{
    int a,b;
    cout<<"please input two integers " <<endl;
    cin >> a >> b;
    int sum = sumTo(a,b); //call the function
    cout<<sum;
    

return 0;

}//end main

int sumTo(int x, int y)
{
   //write code for the function here
    return sum;
}//end function

You are making progress. Keep it up and good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.