Jump to content
  • Advertisement
Sign in to follow this  
Noxxid

Calculation prog

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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?

Share this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!