Thread: Two Repeat, one While Loop

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    4

    Two Repeat, one While Loop

    So I have a programming assignment.

    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
    
    
    	int input;
    	int sum;
    	int i;
    	int t;
    
    
    	sum = 0;
    
    
    	printf("Enter an integer. " );
    	while(scanf("%d",&input)==1) 
    	{
    		for(i=1;i<=input;i++)
    		{
    			t = (i*i)+1;
    			printf("%d ",t);
    			sum = sum+t;
    		}
    		printf("%d",sum);
    		sum=0;
    		printf("\nEnter an integer. ");
    	}
    	printf("That is not an integer.");
    
    
    	return 0;
    }
    So you input an integer ex) 30
    the printing of t =(i+i)+1 and i++ is carried out until i=30, which is when the sum of all the t's is printed.

    If the value you entered is not an integer, the loop ends.

    I'm supposed to do this with one while loop, and no more. I can't use the for loop either.

    How can I do this? :/

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should try planning this out on paper first, perhaps with a flow chart, to determine how the logic should be structured.

    You might want to consider using the value of 'i' to control the loop instead, and using an "if()" within the loop to validate the input.

  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    4
    Quote Originally Posted by Matticus View Post
    You should try planning this out on paper first, perhaps with a flow chart, to determine how the logic should be structured.

    You might want to consider using the value of 'i' to control the loop instead, and using an "if()" within the loop to validate the input.
    Thanks, I was less confused when I made a flow chart.

    This is what I came up with:
    I'm pretty sure that the logic is right, but
    printf("%d",input)==1 must not work, if that's the problem :/

    What is wrong with my code?

    Code:
    #include <stdio.h>
     
    int main(void)
    {
        int input;
        int i;
        int t;
        int sum;
    
    
        i = 1;
        sum = 0;
    
    
        printf("Enter an integer.");
        scanf("%d",&input);
    
    
        while(input>=i)
        {
            if(printf("%d",input)==1);
            if(input>i)
            {
                t = (i*i)+1;
                printf("%d ", t);
                sum = sum + t;
            }
            else
            {
                printf("= %d",sum);
                sum = 0;
                i = 1;
            }
        }
        printf("That is not an integer.");
    
    
        return 0;
    }

  4. #4
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    As you've written it, the while loop will never end. Think about it, at what point in loop is input not more that I? In order for the loop to break at some point you need to operate on I (at the point you want to break the loop). A good expression at the point might be I=I+input, that way if input is 50, I would then be 51, ending the loop.


    The if else statements mean that if I enter a number more than I (which is 1), I will then be entered into an unending loop that prints to screen the number I entered and t (which is 2 as explained above). Elsewise if input is less that 2, an unending loop that prints first the value of sum.

    the reinitializing of I (and sum, I think) at that point doesn't do anything, because that value of I (or sum) hasn't been operated on.

    Hope that helps/makes sense. Good luck.
    Last edited by Alpo; 04-21-2014 at 10:07 AM.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I'm not completely clear on the requirements, which makes it difficult to give advice. Are you only supposed to be reading an integer once?

    Code:
    if(printf("%d",input)==1);
    Checking the return value of "printf()" won't be of much help in this case. Besides, you have a semi-colon after the "if()", which would render the results of any comparison moot.

    However, checking the return value of "scanf()" will help you validate the input. (Hint: read the "Return Value" section of that link.)

    If you're only supposed to be reading in a single number, I'd recommend following a process like this:

    - Attempt to read input from the user
    - If the input is invalid, print an error message
    - Otherwise, run your loop

    Code:
    while(input>=i)
    You're supposed to be running your loop until 'i' reaches a certain value. This value does not seem to be related to "input", so why are you using this for your comparison condition?

  6. #6
    Registered User
    Join Date
    Apr 2014
    Posts
    4
    I got it figured out by using two conditions for the while loop.. I never really thought that you could do that. Wow

    Thanks a lot for helping out.

    Code:
    #include <stdio.h>
     
    int main(void)
    {
       int input;
       int i;
       int t;
       int sum;
       int res;
       i = 1;
       sum = 0;
     
       printf("Enter an integer.");
       res = scanf("%d",&input);
     
       while(res == 1 & input>=i>0)
       {
          if(input>i)
          {
             t = (i*i)+1;
             printf("%d ", t);
             sum = sum + t;
           i++;
           if(input <= i)
           {
              printf("%d\n",sum);
              sum=0;
              printf("Enter an integer.");
              res = scanf("%d",&input);
    		  i=1;
              continue;
           }
          }
       }
       printf("That is not an integer.");
     
       return 0;
    }

  7. #7
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    There are still some of the problems mentioned, I (<-auto correct) will only increment by one on each printing, also I is always more than 0. So the loop will repeat until I is greater than whatever number input turns out to be, at which point the second if statement becomes true, and I is reduced to less that input, meaning that the first if statement is then entered into again, and so on.

    What happens if I enter input as 1 inside the while loop?

    Edit: Just noticed, the correct logical and expression is &&, which basically means- while both of these things are true. You'll need to put the other ampersand in your while loop.
    Last edited by Alpo; 04-21-2014 at 11:03 AM.

  8. #8
    Registered User
    Join Date
    Apr 2014
    Posts
    4
    That was the point of the whole thing, you input a number, (i*i)+1 is printed and i increases by 1 until the value of i is equal to the value of the input and you enter another integer to start again.
    The thing stops only if the input is not an integer.

    and you're right, I have to work on the part where input is equal to 1 :/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Repeat this!
    By skittlesaddict in forum C++ Programming
    Replies: 8
    Last Post: 10-04-2012, 04:25 AM
  2. The loop does not repeat itself! Why?
    By rutgersmis in forum C Programming
    Replies: 5
    Last Post: 10-11-2011, 06:43 AM
  3. repeat until any key
    By jatoo in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2008, 03:55 AM
  4. repeat loop
    By taurus in forum C Programming
    Replies: 14
    Last Post: 09-11-2007, 07:31 AM
  5. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM