Calculation prog

Started by
2 comments, last by Sneftel 18 years, 7 months ago
Hi all, I've recently started programming in C++ and am trying to create a program that does the following: calculates the amount of numbers a user enters, the average, sum, min, max and standard deviation. My code looks something like this so far.

#include <iostream>
#include <string>
using namespace std;
	int main()
	{
		double numbers = 0,total = 0,totalCounter = 0,less = 0,more = 0,tester = 0;
		char breaker = static_cast<char>(numbers);
		
		cout << "Enter a list of numbers to get the average, max, min, sum, and standard deviation" << endl;
		while(tester = 0)
		{
			cout << "Enter a number or s to quit: ";
			cin >> numbers;
	
			if(breaker != 's')
			{
				totalCounter++;
				total += numbers;
				if(numbers >= more)
					more = numbers;
				if(numbers <= less)
					less = numbers;
			}
			if(breaker = 's')
				tester = 1;
		}
		
			cout << "Total numbers inputted: ";
			cout << totalCounter << endl;
			cout << "The average equals: ";
			cout << total/totalCounter << endl;
			cout << "The sum equals: ";
			cout << total << endl;
			cout << "The max is: ";
			cout << more << endl;
			cout << "The min is: ";
			cout << less << endl;
		
		return 0;
	}
I was wondering if there was anything wrong with the way my loop is formatted? My original intention was for the user to input 's' for stop but I don't understand how to convert a double into a char. Thanks for any feedback! Noxxid
"Classes will dull your mind destroy the potential for authentic creativity"
Advertisement
Quote:Original post by Noxxid
I was wondering if there was anything wrong with the way my loop is formatted? My original intention was for the user to input 's' for stop but I don't understand how to convert a double into a char.
You can't convert a double into a char, at least not how you want to. You'd have to read in a string first and then convert it the other way.
However you can just ctrl-c to exit anyway, so why bother?
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
iMalc's right. Read in a string. Check if that string is 's'. Then convert the string to a double. (This bit may require using an atof function, or something. Someone else knows better than I do.)
My stuff.Shameless promotion: FreePop: The GPL god-sim.
Also, keep in mind that = does not test whether two values are equal. == does.

This topic is closed to new replies.

Advertisement