Help with outut display "No data"

Hello,

I made a simple code to process the root mean square of a sequence of user entered variables. It works for that task but I cannot figure out how to specify that no data is available for values less than 0.

-1 stops the prompt and starts the calculation. If -1 or -2 is the first input I need it to display "No data" and stop like this:

Enter a positive number (-1 exits): -1
No data

Here is my code thus far:

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
#include <iostream>
#include <cmath> 

using namespace std;

int main()
{
	double nSum = 0, counter = 0, nDivide, rms;
	 for(;;) //loop to gather positive numbers from user stops if -1 is entered
    {
        double number;
        cout << "Enter a positive number (-1 exits): ";
		cin >> number;
		counter++;

		double nSquare = number*number; //squares user numbers and adds them to get the sum
        // stops asking if umber is -1
        if (number == -1) 
        {
			break;
		}
		 nSum += nSquare;
		 nDivide = nSum/counter;
		 rms = sqrt(nDivide);
	 }
	cout << "Answer = " << rms << endl;
}


Any help on how to get this to display "No data" as well would be appreciated.
@abby11

The program will display "No data", with any negative number, and will NOT add it to nSum. I used any negative number only because in your post, you mention -1 or -2 as input.
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
// Root Mean Square.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <cmath> 

using namespace std;

int main()
{
	double nSum = 0, counter = 0, nDivide, rms, number;
	number = 0.0;
	while ( number  >= 0) //loop to gather positive numbers from user. Stops if  negative is entered
    {
        cout << "Enter a positive number (-1 exits): ";
		cin >> number;
		counter++;
		if ( number >=0)
		{
		double nSquare = number*number; //squares user numbers and adds them to get the sum
         nSum += nSquare;
		 nDivide = nSum/counter;
		 rms = sqrt(nDivide);
		 cout << "Answer = " << rms << endl;
		}
	 }
	cout  << endl << endl << "No data" << endl << endl;
}
Last edited on
Thank you the while loop fixed the No data problem perfect. How would I modify it to only display "Answer = " and the rms after the user enters a negative number?

Right now it is printing the rms after each number is added by the user until they enter a negative number.

I tried moving the cout << "Answer = " << rms << endl; out of the loop and moving the calculations
1
2
3
4
double nSquare = number*number;
         nSum += nSquare;
		 nDivide = nSum/counter;
		 rms = sqrt(nDivide);
outside of it but nothing seemed to work.

@abby11
Move just the cout << "Answer..." to just below the 'No Data' statement, Maybe even change the wording to "No more data..".
Last edited on
Hello still having no luck with it, it displays the root mean sum calculation after each user entry. I tried adding an else if statement but that did not fix it either.

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
#include <iostream>
#include <cmath> 

using namespace std;

int main()
{
	double num = 0, nSum=0, nDivide=0, rms=0;
	int counter=0;
	while(num >= 0)
	{
		cout << "Enter a positive number (-1 exits): ";
		cin >> num;
		
		counter++;
			double nSquare =pow(num,2); //squares user numbers 
			nSum += nSquare;
			nDivide = nSum/counter;
			rms = sqrt(nDivide);
		if (num >= 0)
		{
			cout << "Answer = " << rms << endl;
		}
		else if(num <= -1)
		{
			cout << "No data" << endl;
		}
	}
}


Last edited on
@abby11
You didn't just move the one line, but changed the program. Here it the program with the change I mentioned.
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
// Root Mean Square.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <cmath> 

using namespace std;

int main()
{
	double nSum = 0, nDivide, rms, number;
	number = 0.0;
	int counter = 0;
	double nSquare;
	while ( number  >= 0) //loop to gather positive numbers from user stops if -1 is entered
    {
        cout << "Enter a positive number (-1 exits): ";
		cin >> number;
		counter++;
		if ( number >=0)
		{
		 nSquare = number*number; //squares user numbers and adds them to get the sum
         nSum += nSquare;
		 nDivide = nSum/counter;
		 rms = sqrt(nDivide);
		
		}
	 }
	cout  << endl << endl << "No more Data.." << endl;
	cout << "Final answer = " << rms << endl;
}
closed account (DSLq5Di1)
@abby11
You have a counter variable.. so put it to use! http://ideone.com/iU1Co
Last edited on
Looks great its working now thanks again.
Topic archived. No new replies allowed.