Value always returned as 'nan'

I am running into trouble adding a variable to itself. I am trying to make a while loop that will ask you how many numbers you'd like to add together, then loops through the code that many times. Now the problem I have been running into has been that it always returns the value as 'nan'.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  if (choice == 'A' || choice == 'a'){

                int choice_add1, choice_add2;
                float num1_add;
                choice_add1 = 0;
                choice_add2 = 0;

                cout << "How many numbers would you like to add together?: ";
                cin >> choice_add1;
                    while (choice_add1 > choice_add2){
                        cin >> num1_add;
                        choice_add2++;
                        num1_add += num1_add;}
                        cout << "The sum of those numbers is " << num1_add << endl;}
in line 11, num1_add already has a value from previous cin, and you overwrite it
this accounts for why he got a wrong answer, but not for why he got NaN.
1
2
cin>>num1_add;
num1_add += num1_add;


You should still get a number here, even if it is the wrong answer (i.e. twice the last input).

I suspect that you've entered the number incorrectly into the console. What key's did you type before hitting "enter" on the cin?
how do i make it so it continuously adds to itself? so if I enter 1+1+1+1 it equals 4 and not 2? And I am slightly confused by that question. All i entered was numbers. Where I enter the number of times i want it to loop i enter 10, then i just press either 1, followed by enter 10 times. or 2, followed by enter 10 times. In the case of 1, it always equals 2 now. and in the case of 2 it now always equals 4.
Last edited on
just do something like:

1
2
cin >> x; 
sum=sum+x;
going to re-post the code just to show you what i have. But im running into a simmiler problem, only in this case it just prints out the last number i entered.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 if (choice == 'A' || choice == 'a'){

                int choice_add1, choice_add2;
                float num1_add, num2_add;
                choice_add1 = 0;
                choice_add2 = 0;

                cout << "How many numbers would you like to add together?: ";
                cin >> choice_add1;
                    while (choice_add1 > choice_add2){
                        choice_add2++;
                            if (choice_add2 == 1){cout << "Pleas enter the first number: ";}
                            else {cout << "Pleas enter another number: ";}
                        cin >> num1_add;
                        num2_add=num2_add+num1_add;}
                        cout << "The sum of those numbers is " << num1_add << endl;}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
    std::cout << "please enter numbers to add seperated by a '+'\n"
                  "and end with a zero. for example: 12.6 + 5.4 + 9.2 + 0\n? "  ;

    float sum = 0.0 ;
    float number ;
    char c ;

    while( std::cin >> number && number != 0  && std::cin >> c && c == '+' )
        sum += number ;

    std::cout << "sum is: " << sum << '\n' ;
}

http://ideone.com/5xxsuI
I'd prefer to get my code working but i will check out that code.

And i figured it out... dumb mistake. i forgot to replace 'num1_add' with 'num2_add' in the last line. Haha thank you very much for the help.
Last edited on
Topic archived. No new replies allowed.