Solved

Basic C++ Programming

Posted on 2013-01-25
7
434 Views
Last Modified: 2013-01-25
I need to write a basic program that takes a number from user input and adds all  numbers leading up to that number. For instance, a user enters 15, I need to add 1+2+3...15. The number "15" could be any positiving integer that the user decides to input.
Here is what I have so far but with no results:

int main ()
{
int num;
int sum = 0;
cout << "Enter a positive int value.";
cin >> num;

while (num >=1)
{
sum = sum ++
}
if (sum > num)
break;
cout << sum;

return 0;
}
0
Comment
Question by:lostinspace9
  • 3
  • 3
7 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 38818423
Your num appears to be going into an infinite loop.  You need to increment num.  A for loop on num would be easier for you.

You should not use sum for comparison for breaking out of loop.  You have to keep a variable like i that counts from 1 to num.
0
 

Author Comment

by:lostinspace9
ID: 38818600
Okay, so I changed to a for loop and am not using a sum comparison to break the loop and I am getting an odd number as output.

int num;
int total = 0;
int sum;
 cout << "Enter value.";
cin >> num;

for (sum=0; sum<num; sum++)
{
total += sum;
cout << total;
}
return 0;
0
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 500 total points
ID: 38818721
It looks like you're off by a factor of 1.

While a typical C++ for loop looks like
    for(int i=0; i<count; i++)
so that the loop with iterate 'count' times, you're trying to use the iterator for the math as well.  But that means rather than adding up the values from 1 to num you are adding up the numbers from 0 to num-1

Try changing your for loop to
    for(sum=1; sum <= num; sum++)
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:lostinspace9
ID: 38818775
This gives me the same output I saw before of 1361015 when entering a value of 5 at the prompt when the goal would be to get the value of 15 on return.
0
 
LVL 31

Expert Comment

by:farzanj
ID: 38818862
sum name is confusing.  Make it i and total should be some, makes it easier to think.

Second, you have to add to num so it should be <= not <, as expert suggested.


Third, you should print the value after the loop executed, unless you want to see value in each step, still you should cout after the loop as well.
0
 

Author Comment

by:lostinspace9
ID: 38818880
Nevermind. Thank you for your help. The loop works fine. I just had my output inside the loop and needed to move it outside.
Thank you!
0
 
LVL 31

Expert Comment

by:farzanj
ID: 38818881
Your problem is that your cout is in the loop and there is NO endl after it.  So it is showing you different numbers all mixed up on the same line together.

1361015

Is actually
1
3
6
10
15
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Access DB on open (All Objects) 5 61
White board coding practice 3 80
Path to  STL Map header file 1 43
How can i compile this github project?? 2 76
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
MS Access 2003 or later To MySQL Migration Project Hello All, this is my second article in the category of MS-OFFICE Automation. In internet I am not able to find any comprehensive resource on the Migration of MS Access back-end to MySQL so I fin…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now