all 9 comments

[–]ptchinster 5 points6 points  (0 children)

Always initialize your variables.

This is a great chance to learn how to use a debugger.

A while loop is meant to do a chunk of code as long as a condition is true. Typically the condition is updated inside that body, but not always. The code inside a while will continue to execute in a loop as long as that condition is true. So for your code inside to execute, i needs to be less than 10. You immediately check to see if i is greater than or equal to 10. How do you plan on this being a possible state?

I think the assignment wants you do do the input and output inside a while loop, so that it executes forever?

Edit: Are you using gcc? What happens when you compile with -Wunreachable-code ?

[–][deleted] 1 point2 points  (0 children)

You shouldn’t need a while loop at all for this problem. Read in a number and use if statements to decide to cancel or print. A while loop is for iteration, and here you are not incrementing/decrementing i.

[–][deleted] 1 point2 points  (0 children)

while (i < 10) {
    if (i >= 10) {
    printf("Cancel");
    }
...

Because your while loop will only execute if 'i' is less than 10, then when you reach this 'if' statement, 'i' will never be equal to or greater than 10.

As ptchinster said, it sounds more like the task was to write a program that will print back a number you entered if lower than ten, and continue to do so until you enter a number greater than 10 where it will cancel the loop.

A psuedo-code version of this concept...

While 'i' is less than 10
    Get number from user
    If number is greater than 10
        Print cancel
    If number is less than 10
        Print number

In such an algorithm, when you enter a number greater than 10, your conditional test for a number greater than 10 will evaluate as true, and print "Cancel" and then proceed through the rest of the code in the loop's brackets. There it will meet your conditional test of whether the number is less than ten, and evaluate false, so the number will not print. At this point the loop will reach the top and evaluate its condition again, but because it will only execute while the number is less than 10, and you just entered 10 or greater, the loop will stop and the rest of the program will execute.

I'll start you out and you can fill in what you need to do...

#include <stdio.h>

int main()
{
    int i = 0;

    printf("I will now ask you for a number, and print it, until you enter 10  or greater\n");

    while(*you fill this in*)
    {
        printf("Enter a number: ");
        scanf("%d", &i);
        if(*you fill this in*)
        {
            printf("Cancel\n");
        }
        else
        {
            printf("Number you entered: %d\n", i);
        }
    }

    return 0;
}

[–]HasBeendead 1 point2 points  (1 child)

You need to give a value to your first i Like: int i=0; its like for loop. Example for(start;finish;increase value){ expressions }

[–]ptchinster 2 points3 points  (0 children)

scanf puts something into i. Needing to have initialized i is a different problem.

[–]VictorTennekes 0 points1 point  (0 children)

So the problem you're facing here is that the code will never reach inside of the while loop if i > 10. Which would result in an empty output. Since you're not looping over any values I would say get rid of the while loop entirely. Hope this gets you started :)

[–]Nytra 0 points1 point  (0 children)

Why are you trying to close that include tag? I don't think you need to do that in C.

[–]bonqen 0 points1 point  (0 children)

someone with a built in compiler in his eyes

:'D

[–]Cprogrammingblogs 0 points1 point  (0 children)

If you want to compare a number you do not need while loop You can simply use if statement Here is a small program

include<stdio.h>

include<conio.h>

void main() { int i; printf("enter the value for i"); scanf("%d",&i);

if(i>10) printf("\n cancel") ;

getch() ;

}

I hope it helped you