0

This is my code for incrementing either positively or negatively but somehow the code compiles but dosen't work.

#include <cs50.h>
#include <stdio.h>

int main()
{
    int Ini, Con, Incre;

    //ask user for input
    printf("Initialization: \n");
    scanf("%d", &Ini);

    printf("Condition: \n");
    scanf("%d", &Con);

    printf("Increment: \n");
    scanf("%d", &Incre);

    //how to increment variable
    char PN = '\0';
    {
    printf("\nPositive or Negative increment?\n");
    PN = GetChar();
    int count = 0;

    {
        if(PN == 'P' || PN == 'p')
        {
            for(count = Ini; count < Con; count = (count + Incre));
            printf("%d\n", count);
        }

        else if(PN == 'N' || PN == 'n' )
        {
            for(count = Ini; count > Con; count = (count - Incre));
            printf("%d\n", count);
        }
    }
    }
}

And this is what it compiles

/home/ubuntu/workspace/fooling/ $ make counter clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wshadow counter.c -lcs50 -lm -o counter /home/ubuntu/workspace/fooling/ $ ./counter Initialization: 2 Condition: 30 Increment: 5

Positive or Negative increment? Retry: p 32 /home/ubuntu/workspace/fooling/ $

can you tell me where i went wrong?

2
  • I tried to understand what the program is supposed to do, but it is unclear whether certain code is intentional or in error. Can you explain in words what the program is supposed to do? This is also the reason for sufficient comments in a program, so that others can understand what each section is supposed to be doing.
    – Cliff B
    Aug 24, 2016 at 5:04
  • the program is suppose to ask the user for the initialization,condition and increment for a for loop. The increment can either be negative or positive, P stands for positive increment and N for negative increment and the for loop prints out the numbers using the increment until the condition is met
    – Jere
    Aug 24, 2016 at 5:12

1 Answer 1

0

It looks like you need to review the construction of a for loop. Look at the following:

        for(count = Ini; count < Con; count = (count + Incre));
        printf("%d\n", count);

As written, the for loop will until the condition is no longer met, but will not do anything other than change the value of count. Note the semicolon at the end of the line on the for loop statement. This essentially means for(...;...;...) do nothing;. The printf statement will execute once and only once, since it follows the loop, but is not part of the loop.

If you had wanted to execute the printf statement as part of the loop, then a single line of code to be executed should be between the for loop setup statement and a terminating semicolon, or the for loop setup line should have been followed by a curly brace pair with a block of code inside, like this:

for(...;...;...)
{
    //code block here
}

This is the preferred way to write a for loop.

if this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • it worked but the GetChar i used at the end of the printf question :printf("\nPositive or Negative increment?\n"); PN = GetChar(); when i compile it prints out the question and retry like this: Positive or Negative increment? Retry: N 100 85. why does it keep doing that?
    – Jere
    Aug 24, 2016 at 5:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .