treysentio
treysentio

Reputation: 3

adding an outer while loop to my c program - beginner 1 to continue 0 to stop

This is my first post on stack overflow, so this is my code so far, i'm just starting computer engineering and am having some trouble.

#include <stdio.h>

int main ( void ) {
   int num, sum = 0, i, ssq = 0, isq, n;

   printf("Enter an integer: ");
   scanf("%d", &num);

   for (i = 1; i <= num; i++) {
      sum = sum + (i * i);
   }
   printf("The sum of the squares of integers from 0 to %d is %d\n", num, sum);

   while (i >= 0) {
      printf("Would you like to go again? (1 for yes, 0 for no): ");
      scanf("%d", &i);

      printf("Enter an integer: ");
      scanf("%d", &num);

      for (isq = 1; isq <= n; isq++);
          ssq = ssq + (isq * isq);

      printf("The sum of the squares of integers from 0 to %d is %d\n", num, sum);

      if (i == 0) break;
   }

   return 0;
}

This is what I have so far believe it or not it took me 12 hours to do the first part, I've literally been up all night working on this, before the while loop and now I'm completely lost. I added the ssq=0, isq, and n ints in to try to help with no avail. At this point I'm just rearranging stuff for hours on end, this is my first post so please don't be too hard on me!

Upvotes: 0

Views: 146

Answers (6)

Shreyan Mehta
Shreyan Mehta

Reputation: 550

what you want to do was basically a menu driven program... i would suggest , like the above one's are right too... but another way you can do is using do{ //the task , i.e. the sum } while(i!=0);

doing this since do while is an exit controlled loop as you might be knowing... so as in earlier stages u can be free from using break; keyword... also as you are starting an early bird tip i would like to give is that the suggestion of using functions by @John above ....is good but not if you are doing mistakes in a normal int main() 15 lines code... since if you go in user defined functions like stated above you might go wrong in passing the arguments or basics of function passing... so go for a normal int main code for now...

Note i didnt meant that what @ John said was wrong or something...just gave my view/advice

Upvotes: 0

Arun Nair
Arun Nair

Reputation: 335

You have made a few mistakes here:

  1. You have put a semicolon after the for loop inside the while block which should be removed.
  2. Inside the while block you have accepted value for 'num' while you have used 'n' in the for loop (for which value is uninitialized). So you should accept the value of 'n' instead of 'num' or else replace 'n' in the for loop with 'num'.
  3. If you want to exit the while loop immediately after user inputs 0 then move the if (i == 0) break; statement to below the scanf ("%d", &i) statement.
  4. In the printf() statement next to the for loop, you have used value of 'sum' while you have calculated value for ssq.
  5. Also you should write ssq = 0 after the printf() statement to reset its value to 0.

Correct all these and the program will work.

Upvotes: 1

John Hascall
John Hascall

Reputation: 9416

Here is @iksemyonov 's answer refactored to honor the "no more than 7 lines in a method" rule (using a hard and fast arbitrary number like 7 is absurd, but the principle of making function do a specific understandable task is reasonable).

#include <stdio.h>

static int getBoundary ( void ) {
    int boundary;

    printf("Enter an integer: ");
    scanf("%d", &boundary);
    return boundary;
}

static int computeSSQ ( int limit ) {
    int ssq = 0;

    for (; limit > 0; --limit) ssq += (limit*limit);
    return ssq;
}

static int again ( void ) {
    int choice;

    printf("Would you like to go again? (1 for yes, 0 for no): ");
    scanf("%d", &choice);
    return choice;
}

int main ( void ) {
    do {
        int boundary = getBoundary();
        int ssq = computeSSQ(boundary);
        printf("The sum of the squares of integers from 0 to %d is %d\n",
            boundary, ssq);
   } while (again());
   return 0;
}

Upvotes: 1

iksemyonov
iksemyonov

Reputation: 4196

This contains a whole host of errors, from typos to code duplication, as @HappyCoder has noted above.

First of all, the outer part and the loop part do exactly the same. Think about it for a moment. You first do some task, unconditionally, then ask the user if they want to start over. The task itself doesn't change! Hence, what we can do is this:

do the task;
ask the user if they want to quit or go on;
if yes, return to the start.

In code, this can be done with an endless loop that you break out of if the user wants to stop:

while(1) {
    // do user input and calculations here;
    printf("Would you like to go again? (1 for yes, 0 for no): ");
    scanf("%d", &i);
    if (i == 0)
        break;
}

See, now we only have one instance of the calculation code! Now, you can throw away half the variables declared in the beginning, since they are duplicate.

Now on to the calculations. You have an uninitialized variable, ssq, in the loop. See where code duplication can get you. In the outer part, it is initialized properly. Inside the loop, however, it is not guaranteed to hold any concrete value, most likely it contains garbage.

Also, as noted by @JohnHascall, this subtle error introduced most likely by a typo:

for (isq = 1; isq <= n; isq++); // <---- the evil semicolon
    ssq = ssq + (isq * isq);

The semicolon after the for loop makes the loop empty, and the summation only happens once, but not in the loop, as you want it to be.

Then, you output (print) sum not ssq inside the loop, which is obviously not what you want to print. And, you use the uninitialized n variable from outside the loop as the boundary, instead of the user inputted num.

I want to add yet one more. Sanely naming the variables is a big deal as it helps you to catch potential errors and keep track of how variables are being used throughout the code, not to mention easier understanding of the code by others. Look: int i -> int choice better isn't it?

So we can rewrite the code like this:

#include <stdio.h>

int main ( void )
{
    int boundary, choice, isq, ssq;

    while (1) {
        printf("Enter an integer: ");
        scanf("%d", &boundary);

        ssq = 0;
        for (isq = 1; isq <= boundary; isq++) {
            ssq = ssq + (isq * isq);
        }

        printf("The sum of the squares of integers from 0 to %d is %d\n", boundary, ssq);

        printf("Would you like to go again? (1 for yes, 0 for no): ");
        scanf("%d", &choice);

        if (choice == 0)
            break;
    }
    return 0;
}

Upvotes: 4

pranav
pranav

Reputation: 144

for (isq=1; isq<=n; isq++);  
        ssq = ssq + (isq*isq);

The problem here is that 'n' is not initialized and not used in above scanf statement, you need to use 'num' instead of 'n'

for (isq=1; isq<=num; isq++)  
        ssq = ssq + (isq*isq);

Upvotes: 3

John Hascall
John Hascall

Reputation: 9416

One likely problem is here:

      for (isq = 1; isq <= n; isq++);
          ssq = ssq + (isq * isq);

You probably want:

      for (isq = 1; isq <= n; isq++) {
          ssq = ssq + (isq * isq);
      }

Also, you should add:

      ssq = 0;

above that loop (think about your 2nd trip through the loop).

Upvotes: 1

Related Questions