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;}
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.
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;}
#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' ;
}