Adding numbers

I am trying to learn c++. One interesting program requires to accumulate a running total of numbers up to a user provided number. Here is the description of the program and the code I've come up with so far. Any help will be greatly appreciated.

//This program asks the user to enter a positive integer value.
//The program then uses a loop to add all the integers up to
//the value entered by the user.

#include <iostream>

using namespace std;


int main ( )
{
double num = 0.0; // Loop counter variable
int maxValue; // Maximum value to display

// Request a number from the user
cout <<"Please enter a positive number: ";
cin >> maxValue;
cout <<"Number Addition" <<endl;
cout <<"____________________________" <<endl;

//Use a loop to add all numbers up to number entered
for (num = 0; num <= maxValue; num++)
num += maxValue;
cout <<num<< "\t\t"; // <<(num + num)<<endl;
return 0;
}
In your loop, you are adding "maxValue", which is basically a constant. I think you want to read in numbers inside the loop and add them up.
#include<iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{
int num=0,sum=0,maxvalue=0;

cout <<"Please enter a positive number: ";
cin >> maxvalue;
cout <<"Number Addition" <<endl;
cout <<"____________________________" <<endl;

for(num=1;num<=maxvalue;num++)
sum=sum+num;
cout<<"The sum is ...."<<sum;
return 0;
}

I have modified your code slightly. I think this is the program you want.
Last edited on
Topic archived. No new replies allowed.