Hi guys, having a little problem with my first c++ course and I'm hoping someone can help me out abit here.

Write a program that asks the user for a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, …50.

This is the work I have so far.

#include <iostream>
using namespace std;

int main()

int getSum;
    num;
{
     if( num==1)
             return 1;
     else
          return num+(getSum(num-1));
}

main
{
    cout << "enter a number";
    int value;
    value=Console.ReadLine();
    while(value<=0)
     {
    cout << "value has to be positive (>0).  Enter
new value.";
           value=ConsoleReadLine();
     }
     finalValue=getSum(value);
     cout << "sum is: " + finalValue";
     return 0;
}

I'm getting 2 error's and also I'm not sure if I've written this program correctly... very confused and my instructor at college being a first time instructor who unfortunately can't teach doesn't help things either
:mad:

Recommended Answers

All 2 Replies

Greetings wangstarr,

One thing I did notice with the provided source code was that there are a few errors, as you have stated:

int main(); // Ending in ; may stop the errors

// Problem here:
int getSum;
num;
// Changed
int getSum(int num)
{

// Another Problem
main
{
// Changed
int main()
{

Hope this helps,
- Stack Overflow

int value;
int num=1;
int sum=0;
while (num<=value) {

sum+=num;
num++;
}
cout <<"total is:" <<sum;

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.