I'm a total newbie and.. with the code above..

What I want to do is have 10 dValue numbers, but if, of those 10 I only enter 5 values, the exe shouldnt ask me to enter all 10 and just add the 5 values I entered when I press the return key .. or something like that

//    Add 3 numbers 
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
    double dValue1;
    double dValue2;
    double dValue3;

    // enter three numbers
    cout << "This program adds three numbers;\n";
    cout << "Enter three integers:\n";

    cout << "n1 - ";
    cin  >> dValue1;

    cout << "n2 - ";
    cin  >> dValue2;

    cout << "n3 - ";
    cin  >> dValue3;

    //  the sum 
    cout << "n1 + n2 + n3 = ";
    cout << dValue1 + dValue2 + dValue3;
    cout << "\n";

    // wait until user is ready before terminating program
    // to allow the user to see the program results
    return 0;

Recommended Answers

All 2 Replies

You need to give the user the option of typing in a value that represents that there are no more values to enter. Perhaps give a prompt like:

Please enter a value.  Enter -999999 if you have no more values [B]:[/B]

As for the ten (or fewer) values, you should probably have an array and a loop instead of ten separate variable names and ten separate cin statements. So you're looking at something like this:

double dValue[10];
    int numEntries = 0;
    double sum = 0;
    while (numEntries < 10)
          cout << "Enter number (-999999 to stop) : ";
          cin >> dValue[numEntries];
          // check whether dValue[numEntries] == -999999
          // if so, short-circuit the loop with the "break" command
          // increment numEntries
          // adjust sum

As a side note, I picked -999999 as the value to represent no more values because

  1. It's an integer, so it can be represented exactly as a double (no round-off error using == to compare).
  2. It's a number that, chances are, people wouldn't actually want to add, so it's sort of "out of the way". Chances are, few people want to add -999999. If they do, they can't use this program!

edit: don't worry, forget I said anything :icon_redface:

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.