1st off, just want to say if you're reading this, thank you for taking your time to help me out, I really need it. Ok now on to my question:
Using C++ I'm attempting to use a "for"loop to ask user to input two integers, then the loop should add all the numbers between the "two user supplied" numbers and output the sum. Example:

Please input two integers.
2 6

(loop should do this)
2+3+4+5+6

(then out put)
sum of your numbers is 20.

I'm new at this so any suggestions will really help me thanks!!!!
Also I've written the following code but still many errors when i try to build. What am I doing wrong?

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

for (int i = low; i<high, i++)
{
sum+=i;
}
return 0;
}

Recommended Answers

All 6 Replies

You are very much on the right track. But have a few problems that are holding you back.
First, you can't use variables that haven't been declared with an appropriate data type. You've done that with high, low, and sum.
Low and high can be replaced by the x and y values respectively, as long as you ensure that the first number entered is smaller than the second. Also you need to declare sum and output its value outside of the loop.
Good luck.

Edit: You also want to include the high value when iterating through the for loop. So you'll need to use less than or equal.

I would like the order of the input numbers not to matter so it can be either the high or the lower of the two numbers input 1st. Will I need to write a function to determine which number is higher? if so I have done that but not sure how to string it all together...
here is my code to figure which number is higher:

int main ()  
{
   int x,y;
	    cin >> x >> y;

	cout << smallest(x,y) <<endl;
  }
	int smallest (int x,int y)  
{
   return x>y?y:x;
 }

but mainly i just want the program to add all the numbers, am I far off?? what should I be looking to do next?

Just use an if statement to test which number is larger.

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;
    }

Notice the difference in the two for loops. That should be of great help to you.
Please note that this is definitely not the most efficient way to test. But for such a small program, it will suffice.
Remember to declare your variables!!

You were on the right track:

#include<iostream>
using namespace std;
int main()
{
int x, y, z, sum;
cout<<"Please input two integers: \t" <<endl;
cin >> x >> y;
    if(x > y) {
    z = y;  y = x; x = z;
    }
for (int i = x; i <= y, i++)
{
sum += i;
}
cout << "Sum = " << sum << "\n";
return 0;
}

Please ignore my post in this thread. Accidentally replied on wrong thread

Hello there, you're on the right track! If you want to implement a function for your program it could look something like this!

#include <iostream>
using namespace std;
int sum(int Start_num, int End_num);
int main()
{
	int Start_num;
	int End_num;
	int summ;
	cout<<"Please enter the starting number: "<<endl;
	cin>>Start_num;
	cout<<"Please enter the ending number: "<<endl;
	cin>>End_num;
	
	summ=sum(Start_num,End_num);
	
	cout<<"The sum of 2+3+4+5+6 = "<<summ<<endl;
	return 0;
}
	int sum(int Start_num, int End_num)
	{
		int sum=0;
		
		for(int i = Start_num;i<=End_num;i++)
		{
			sum+=i;
		}
		
		return sum;
		
		
	
}
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.