Public Group

# Calculation prog

This topic is 4831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
Quote:
 Original post by NoxxidI 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?

##### Share on other sites
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.)

##### Share on other sites
Also, keep in mind that = does not test whether two values are equal. == does.

1. 1
2. 2
Rutin
21
3. 3
4. 4
A4L
15
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633736
• Total Posts
3013605
×