Hi,

I Have tried to make average calculator but I cant make few things happen and I would really appreciate some help with this.

This is the code I came up with.

Questions are below.

#include <iostream>
using namespace std; 

int main ()
{
      int a, b;
      float result;
      cout << "Please enter first number" << endl;                      
      cin >> a; 
      cout << "Please enter second number" << endl;
      cin >> b;
      cout << "Average is" << endl;
      result = (a + b)/2.0;  
      cout << result << endl;      
      system ("pause");
      return (0);

is it possible to input unlimited amount of nummbers nut just number 1 + number 2?
If yes then how ? for example 1+45+34, is there a way to define all positive numbers for example.

And how can I change result function so it understands how many numbers I have inputed and calculates the average value? IF its 1+45+34 the program understands that result should be (1+45+34)/3

If I input a letter instead of nummber how can I make it so program says that it is invalid input value or something like that ?


Thank you in advance,
And please dont be very harsh, i've been learning C++ for few weeks !

P.S Sorry for any english mistakes

Recommended Answers

All 7 Replies

>is it possible to input unlimited amount of nummbers nut just number 1 + number 2?
Yes. You need a running sum, a count of how many numbers there are, and one variable for the number. Then you'd do this:

int count = 0;
int sum = 0;
int x;

while ( cin>> x ) {
  sum += x;
  ++count;
}

if ( count > 0 )
  cout<<"Average: "<< sum / count <<'\n';

>how can I make it so program says that it is invalid input value
If you tell cin that you're going to type an int, but you don't, it fails. You can test for that and act accordingly:

int count = 0;
int sum = 0;
int x;

while ( cin>> x ) {
  sum += x;
  ++count;
}

if ( !cin.eof() )
  cout<<"Invalid input\n";

if ( count > 0 )
  cout<<"Average: "<< sum / count <<'\n';

If the loop terminates it means that cin failed for some reason. Most likely the reason is that you signaled end-of-file with something like ctrl+z (for Windows), and the eofbit will be set (which you can test for with the eof() member function), but if you type a non-digit then it will fail with an error and the eofbit won't be set.

Yes. One thing you could do is create a running-sum of numbers and ask the user at the start how many numbers they want to insert, then loop that many times. After it's all looped divide by the number of iterations which were inputted.
Alternatively you could read the numbers in as strings, stringstream to a number (you familiar with stringstreams?), and keep the same running-sum as before and after loopin' divide by the number of iterations.
Or you could read in integers until someone enters an invalid value ... like a character where you require an integer.

>> If I input a letter instead of nummber how can I make it so program says that it is invalid input value or something like that ?

#include <ios>
#include <iostream>
#include <limits>

int main( void ) {
  /* ... */  

  int num;

   while ( !(std::cin>>num) ) { // If the inputting failed
     std::cout<< "You didn't enter an int!\n";
     std::cin.clear(); // Make sure to clear the stream's errors
     std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); // and remove the offending characters.
   }

  // num fine here.

  return 0;
}

Thank you very much for your responses but I have one more question now.

when I input 1+2= and press enter then I get this response:

2+1=
Invalid input
average is: 1

(if I input just 1+2 and press enter nothing happens)

As far as I understand this, program understands "=" as invalid input
what code should I add in order to do something like this:
1+2+3+4 > then I press enter and average shows

#include <iostream>

using namespace std; 

int main ()
{
      int count = 0;
      int sum = 0;
      int x;
      float sum/count;

          cout << "enter your numbers " << endl; 
          while ( cin>> x ) 
          {
          sum += x;
          ++count;
          }
          
          if ( !cin.eof() )
          cout<<"Invalid input\n";
          
          if ( count > 0 )
          cout<<"Average: "<< sum / count <<'\n';   
      system ("pause");
      return (0);
}

>but Im not sure what should I float in code like this.
Change int sum = 0; to double sum = 0; . Easy, huh? :)

>And when I input 1+2= and press enter
That's a completely different input mechanism. The code you've been shown so far expects only numbers, separated by whitespace. So your execution should look like this:

enter your numbers 1 2 3 4 5
^Z
average is: 3

Remember that if you type a non-digit, cin pukes? Well, '+' is a non-digit. ;) I'd recommend getting it to work first, then you can add fancy input parsing.

>Change int sum = 0; to double sum = 0;. Easy, huh?

I actually changed all int to float and it worked aswell :)

enter your numbers 1 2 3 4 5
^Z
average is: 3

This probably is a very stupid question but witch button toggles average ?

>I actually changed all int to float and it worked aswell
The only one that needs to be changed is sum, but you can change them all if you want. Though I would recommend getting into the habit of using double instead of float when you want floating-point values. The default floating-point data type is double, so it'll make your life easier.

>This probably is a very stupid question but witch button toggles average ?
Well, since you seem to be on a Windows machine, you press and hold down either ctrl key, and then press z. This signals end-of-file in the Windows console.

Thank you very much for all your help! :)

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.