Loop program help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 18
Reputation: wangstarr is an unknown quantity at this point 
Solved Threads: 0
wangstarr wangstarr is offline Offline
Newbie Poster

Loop program help

 
0
  #1
Sep 27th, 2004
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:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: Loop program help

 
0
  #2
Sep 28th, 2004
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
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 11
Reputation: h3rtz is an unknown quantity at this point 
Solved Threads: 0
h3rtz h3rtz is offline Offline
Newbie Poster

Re: Loop program help

 
0
  #3
Oct 8th, 2004
int value;
int num=1;
int sum=0;
while (num<=value) {

sum+=num;
num++;
}
cout <<"total is:" <<sum;
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC