944,188 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2625
  • C++ RSS
Sep 27th, 2004
0

Loop program help

Expand Post »
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:
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
wangstarr is offline Offline
18 posts
since Sep 2004
Sep 28th, 2004
0

Re: Loop program help

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
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Oct 8th, 2004
0

Re: Loop program help

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

sum+=num;
num++;
}
cout <<"total is:" <<sum;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
h3rtz is offline Offline
11 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: displaying * in password entery
Next Thread in C++ Forum Timeline: Why doesn't my code work??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC