I am assigned the sort-of famous RMS assignment. This is what I have so far, it is just beating me. My biggest confusion is how to add the entries up. I think I will okay with squaring them, dividing the total and obviously the sqrt() at the end.

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	double posNum;
	double posPow, sum;
	int counter = 0;
	posPow = 1;
	posNum = 1;
	while (posNum != (-1))
	{
		counter++;
		cout << "Enter a positive number (-1 to exit): ";
		cin >> posNum;
		posPow = pow (posNum, 2);
		sum = posPow + posPow;
	}
	cout << sum << endl;
}

Recommended Answers

All 13 Replies

Member Avatar for MonsieurPointer

Please use CODE tags; without them, it's very difficult to read code.

Member Avatar for MonsieurPointer

This

sum = posPow + posPow;

will continuously overwrite sum with the next values of posPow. What you are looking for is:

sum += (posPow + posPow);

which is equivalent to:

sum = sum + (posPow + posPow);

Then take it from there.

Thanks, now this is where I am. I am having trouble not including the -1 in the counter, and in the sum. I know it is probably something silly, but it is beating me.

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	double posNum = 0, posPow = 0, sum = 0, newVal;
	int counter = 0;
	while ((posNum != (-1)) && (posNum >= 0))
	{
		counter++;
		cout << "Enter a positive number (-1 to exit): ";
		cin >> posNum;
		posPow = pow (posNum, 2);
		sum = (sum + posPow);
	}
		if (posNum < 0)
		{
			cout << "No data" << endl;
		}
	sum = sum / counter;
	newVal = sqrt (sum);
	cout << sum << endl;
	cout << newVal;
}
Member Avatar for MonsieurPointer

It's simple. You need to check to see if the number entered is < 0. If it is, then break out of the loop. You already have your condition

if (posNum < 0)
{
cout << "No data" << endl;
}

But it is in the wrong place, you need to place that in your loop:

while ((posNum != (-1)) && (posNum >= 0))
{
counter++;
cout << "Enter a positive number (-1 to exit): ";
cin >> posNum;
if (posNum < 0)
{
break;
}
posPow = pow (posNum, 2);
sum = (sum + posPow);
}

When I reconfigured the code within the loop including my if statement, the output that was generated still included the "No data" and my sum (which still included -1 in the count and sum). This is the reason I posted my last post because I have put the if statement within and without the loop.

Member Avatar for MonsieurPointer

Please post you most current code.

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	double posNum = 0, posPow = 0, sum = 0, newVal;
	int counter = 0;
	while ((posNum != (-1)) && (posNum >= 0))
	{
		counter++;
		cout << "Enter a positive number (-1 to exit): ";
		cin >> posNum;
		if (posNum < 0)
		{
			cout << "No data";
		}
		posPow = pow (posNum, 2);
		sum = (sum + posPow);
	}
	newVal = sqrt(sum);
	cout << newVal << endl;
}
Member Avatar for MonsieurPointer

What happened to the break; in if(posNum < 0)?

Alternatively, you can solve your problem like this:

while ((posNum != (-1)) && (posNum >= 0))
{
counter++;
cout << "Enter a positive number (-1 to exit): ";
cin >> posNum;
if (posNum >= 0)
{
posPow = pow (posNum, 2);
sum = (sum + posPow);
}
}

Also, since you are already checking for (posNum != -1), everything else is redundant. It's up to you if you want to check if it isn't -1, or less than 0.

You should also read up on while loops and do-while loops.

I apologize, but that just is not working.

the expected output from the input 1, 2, 3, 4, 5 is supposed to be 3.31662 and its not.

I apologize, but that just is not working.

the expected output from the input 1, 2, 3, 4, 5 is supposed to be 3.31662 and its not.

It is because it continues to use -1 in every calculation. I have done harder programs than this, so it is getting under my skin a little bit.

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	double posNum = 0, posPow = 0, sum = 0, newVal;
	int counter = 0;
	while ((posNum != (-1)) && (posNum >= 0))
	{
		counter++;
		cout << "Enter a positive number (-1 to exit): ";
		cin >> posNum;
		if (posNum < 0)
		{
			cout << "No data";
		}
		posPow = pow (posNum, 2);
		sum = (sum + posPow);
	}
	newVal = sqrt(sum);
	cout << newVal << endl;
}

You forgot dividing by counter. Here are some other changes to your code..

while ((posNum != (-1)) && (posNum >= 0))
	{
		cout << "Enter a positive number (-1 to exit): ";
		cin >> posNum;
		if (posNum < 0)
		{
			cout << "No data";
                        break;              //break out of loop if he enters -1
		}
      		counter++;                  //Moved it down, so that it doesn't increments if user enters -1
		posPow = pow (posNum, 2);
		sum = (sum + posPow);

	}
Member Avatar for MonsieurPointer
do
{
counter++;
cout << "Enter a positive number (-1 to exit): ";
cin >> posNum;
if (posNum >= 0)
{
posPow = pow (posNum, 2);
sum = (sum + posPow);
}
} while (posNum >= 0);
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	double posNum = 0, posPow = 0, sum = 0, newVal, div;
	int counter = 0;
	while (posNum >= 0)
	{
		cout << "Enter a positive number (-1 to exit): ";
		cin >> posNum;
		if (posNum >= 0)
		{
			counter++;
			posPow = pow (posNum, 2);
			sum = posPow + sum;
		}
	}
	if (sum <= 0)
	{
		cout << "No data" << endl;
	}
	else
	{
		div = sum / counter;
		newVal = sqrt(div);
		cout << newVal << endl;
	}

}

This is it right here, thanks for the help guys.

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.