0

My professor is saying that The while loop runs provided n>=1. But I did not put any value into the variable n so depending on its “default” value, the loop may not be entered. And I'm not sure how to fix what he is talking about!?

#include <iostream>
using namespace std;

int main()

{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

int n, count;
double sum;

while (n >=1)
{
    cout << "Enter a positive integer N (<1 to stop): ";
    cin >> n;
    sum = 0;
    for (count = 1; count <= n; count++)
    sum = sum + (1.0/count);
    cout << "Sum = " << sum << endl;
   }
cout << "Bye! ";

return 0;
}
MSK
  • 35
  • 8
  • n will never enter since its not initialized – ivan Jan 22 '16 at 17:06
  • 2
    @ivan Unitialized variables can have any value in c++, therefore your comment is not correct (cf. the answers below). – jofel Jan 22 '16 at 17:09
  • @jofel It throws a runtime exception when you run the code in visual studio – novice Jan 22 '16 at 17:10
  • @novice This is a special feature of visual studio and not needed due to the C++ standard. With uninitialized variables there can happen anything. – jofel Jan 22 '16 at 17:30

5 Answers5

4

Here is the line where n is declared

int n, count;

In this case the value of n is unspecified as it is left uninitialized. You should initialize it

int n = 1;
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • That does solve my problem. However Sum= 0.00 is displayed, and that is technically not suppose to be in the output, just the statement "Bye !" Any idea how to fix that? – MSK Jan 22 '16 at 17:11
2

If you always want a loop to run at least once then you want a do...while(); loop. It will always enter the loop on the first iteration and execute the loop body then it will check the while condition to determine if it loops again or exits. A do...while(); has the form of

do
{
    statements
} while (condition);

In this case it would save you from having to initialize n before you get the input from the user.

In this case though that doesn't seem like exactly what you want as you want nothing to happen if the user enters a number less than 1. One way you can solve that is to put your output and input into the while loop along with the check for n. This will stop anything from happening if the user enters less than 1 but still allowing you to prompt the usr and get the input on each iteration.

while (cout << "Enter a positive integer N (<1 to stop): " && cin >> n && n >= 1)
{
    sum = 0;
    for (count = 1; count <= n; count++)
        sum = sum + (1.0 / count);
    cout << "Sum = " << sum << endl;
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
1

The problem is that n has not been initialized. Unlike in other languages like Java or C#, primitive variables do not have any pre-defined "default" value. The simply occupy whatever stack space was there previously; for all intents and purposes, the default value of uninitialized variables can be considered "random".

To fix this, simply initialize the variable before entering the loop.

n = 1;
Colin Basnett
  • 4,052
  • 2
  • 30
  • 49
  • That does solve my problem. However Sum= 0.00 is displayed, and that is technically not suppose to be in the output, just the statement "Bye !" Any idea how to fix that? – MSK Jan 22 '16 at 17:15
  • Uh, remove the line that prints the sum? `cout << "Sum = " << sum << endl;` – Colin Basnett Jan 22 '16 at 17:28
  • No, I think you misunderstood me, A proper output for the program is Enter a postive integer N (<1 to stop): 6(user input) Sum=2.45 Enter a positive integer N (<1 to stop): 0 (user input) Bye! – MSK Jan 22 '16 at 17:30
0

Set n to a value greater than or equal to 1 so the loop is guaranteed to enter. Since you aren't setting it yourself, the default value can be something less than 1 meaning that the look has a chance, but isn't guaranteed to fire.

int n = 1

Also, you should set count = 0 in your for loop, because if n and count is equal to each other, the for loop automatically breaks and doesn't execute at all, leaving sum at 0.

To make sure your dividing by 0, set count to count + 1.

for (count = 0; count <= n; count++)
    sum = sum + (1.0 / (count + 1) );
  • That does solve my problem. However Sum= 0.00 is displayed, and that is technically not suppose to be in the output, just the statement "Bye !" Any idea how to fix that? – MSK Jan 22 '16 at 17:17
  • set count equal to 0 not 1. If count is equal to 1 and n is equal to one the for loop doesn't execute at all, leaving sum at 0. Also, set count to count + 1 within the for loop so you aren't dividing by 0 – Naomi Dennis Jan 22 '16 at 17:19
  • yeah, but you still get the output statement of the sum=0.00, then "Bye!" If n<1, i need it to just output "Bye!", without the sum=0.00 statement – MSK Jan 22 '16 at 17:27
  • You could always enclose the sum print in an if statement. Since sum has to equal at least 1, you can tell it to only print the sum if 1 > 0. Just in case, I would check your index/braces. Programs can act strange when they aren't used correctly. I would enclose the for loop in brackets and see if that helps. – Naomi Dennis Jan 22 '16 at 17:46
0

You need simply to use another kind of loop that is the do-while loop. For example

do 
{
    cout << "Enter a positive integer N (<1 to stop): ";
    cin >> n;
    sum = 0;
    for (count = 1; count <= n; count++)
    sum = sum + (1.0/count);
    if ( n > 0 ) cout << "Sum = " << sum << endl;
} while ( n > 0 );
cout << "Bye! ";

And instead of the declaration

int n, count;

you should use declaration

unsigned int n, count;

You could else check whether the input is valid. For example

if ( !( cin >> n ) || n == 0 ) break;

Taking this into account you could use also the following kind of loop

while ( true ) 
{
    cout << "Enter a positive integer N (<1 to stop): ";

    unsigned int n;

    cin >> n;

    if ( !( cin >> n ) || n == 0 ) break;

    sum = 0;
    for (unsigned int count = 1; count <= n; count++)
        sum = sum + (1.0/count);
    if ( n > 0 ) cout << "Sum = " << sum << endl;
}
cout << "Bye! ";
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335