Homework help

Write your question here.
1) I need to sum the values from 1 to N using a for loop
2) I need to input two values and find their range
3) I need to input a number to a power

There is a condition to 1) that is must be a positive that I enter
I can do 2) by just ordering the numbers and subtracting right?
and for 3) I need it to be a positive power

When I run this it with something that does not fit the conditions it will let me re-enter but not re-calculate. also when it lets me reenter for the first one it shows the prompt for the second one. PLEASE HELP i am so lost :(


it should print out like this:


Please input a #>1: -3
Please input a #>1: -8
Please input a #>1: 0
Please input a #>1: 2
The sum of integers from 1 to 3 is: 6

Please input 2 numbers: -1 9
The interval between -1 and 9 is: 10

Please input two numbers(no negatives): 4 -8
Please input two numbers(no negatives): 4 0
The product of 4 ^ 0 is: 1


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
       using namespace std;

       int main()
       {
           int num, sum=0, i;
           
           cout<<"Please input an integer greater than or equal to 1: ";
           cin>>num;
           if (num<1)
			   cout<<"Please input an integer greater than or equal to 1: ";
		   else
		   {
			   for(i=0; i<=num; i++)
					
						sum+=i;
						cout<<"The sum of integers from 1 to "<< num <<" is: "<< sum << endl;
					
		   }	
	   
		   

		   int n,p,r=1;
			cout<<"Please input two integers, the second must be non-negative: " << endl;
			cin>>n>>p;
					while (p>1)
					{
						r=r*n;
						cout<<"The product of " << n << "^" << p << "is: "<< r << endl;
					}
			if (p<1)
				cout<<"Please input two integers, the second must be non-negative: "<< endl;

			cin.ignore();
			cin.get();
       return 0;
        }
  
1
2
3
4
5
while (p>1)
					{
						r=r*n;
						cout<<"The product of " << n << "^" << p << "is: "<< r << endl;
					}


Here is a big problem! Infinite loop! Nothing in the while loop changes p, so we either get a loop that doesn't run at all (if p is 0), or an infinite loop. You will need to put in an
1
2
3
4
if(p == 0)
{
    //Answer is 1, don't bother with the loop. 
}


You also need a while loop to check that p is non-negative. Something like:
1
2
3
4
5
while(value <= 1)
{
    cout << "Enter in a value greater than 1: "; 
    cin >> value; 
}


This while loop will continue prompting until a value higher than 1 is entered.
Thank you so much for your help
Topic archived. No new replies allowed.