Can anyone spot the semantic error in this triangular number calculator here? It correctly reports 21 as being a triangle number but incorrectly that 15 is not a triangle number. However if you input 15 as the first value entered it correctly says that 15 is a triangle number!!

int main(int argc, char* argv[])
{
int Number;
int Index;
char Again;
  Index = 1;
  Again = 'y';
  while (Again == 'y')
  {
    Number = ReadIntPr("Enter number for testing: ");
    while (Index*(Index+1) < 2*Number)
    {
      Index = Index + 1; //'i' was used rather than index here
    }
    if  (Index*(Index+1) == 2*Number) //the correct 'is equal to' was not used
      WriteStringCr("Triangular");
    else
      WriteStringCr("Not triangular"); //the semicolon here was not included
    Again = ReadCharPr("Test another number (y/n)? ");
  }

  getchar();
  return 0;
}

Recommended Answers

All 11 Replies

Can anyone spot the semantic error in this triangular number calculator here?

ReadIntPr("Enter number for testing: ");
WriteStringCr("Triangular");
Again = ReadCharPr("Test another number (y/n)? ");

}

Are you including the proper #defines at the beginning of the source file?.
Are you providing the proper prototypes for those functions.
Did you include those functions?.

Yes defines are correct. Prototypes are also unique to the library file that I am using. I wonder if there is a flaw in the logic of the design?

Index = 1;
should be inside the outer while loop.

Index = 1;
should be inside the outer while loop.

Perfect I just could not see it!! Works perfectly now. Thank you.

Index = 1;
should be inside the outer while loop.

I'm afraid that is not correct. Index is a counter and is ok were is being declared.

I'm afraid that is not correct. Index is a counter and is ok were is being declared.

But moving Index = 1 does allow the program to correctly identify triangular numbers.

I guess I'm not understanding what your saying.
Where did you change the Index = 1 to?.

... Is it working now?.
Could you post your changes?.

You keep editing your original question. Is is very confusing. You has changed you question three times.

Hi Edited the question once. Not changed in over an hour.

You keep editing your original question. Is is very confusing. You has changed you question three times.

Hi Edited the question once. Not changed in over an hour.

The first time you original question was about a specific line error that you had. You wrote:

Again = ReadCharPr("Test another number (y/n)? "); //statement missing here?

Second time you question was edit again and also you included this:

while (Index*(Index+1) < 2*Number)
    {
      Index = Index + 1; //'i' was used rather than index here
    }
    if  (Index*(Index+1) == 2*Number) //the correct 'is equal to' was not used
      WriteStringCr("Triangular");
    else
      WriteStringCr("Not triangular"); //the semicolon here was not included
    Again = ReadCharPr("Test another number (y/n)? ");

And after that you edited the question to:

Can anyone spot the semantic error in this triangular number calculator here? It correctly reports 21 as being a triangle number but incorrectly that 15 is not a triangle number. However if you input 15 as the first value entered it correctly says that 15 is a triangle number!!

No desire to start a war of words here. But like I say. You want help,
you will get help here, however editing post to different questions are
confusing.

I guess I'm not understanding what your saying.
Where did you change the Index = 1 to?.

Reread vijayan's post. If the OP did what was suggested, it should be very clear where the assignment was placed:

Index = 1;
should be inside the outer while loop.

I'm afraid that is not correct. Index is a counter and is ok were is being declared.

There's nothing wrong with initializing it every iteration; in fact the lack of doing so is what caused the problems.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.