Can u plz help why dis happens?

I was told to write a cpp program to find the factorial of a number using go to

Can u plz help why dis happens?

You've spelt "you", "please" and "this" incorrectly. Many people here speak English as a second language and deliberately misspelling words while asking for help won't encourage people to help you.

So your code goes through the if statement, and then the goto sends it back to before the if statement, so your code goes through the if statement, and then the goto sends it back to before the if statement, so your code goes through the if statement, and then the goto sends it back to before the if statement, so your code goes through the if statement, and then the goto sends it back to before the if statement, so your code goes through the if statement, and then the goto sends it back to before the if statement, so your code goes through the if statement, and then the goto sends it back to before the if statement, so ....

when will your code EVER not go back to A: ?

As an aside, just so you know, you're using non-standard C++ from twenty years ago that won't compile with a modern, correct C++ compiler. I expect you have no choice, but if you ever want to work as a programmer, this is something you need to know.

So what shall i do to correct the program? Moschops...

The goto should be conditional. Only go back if something.

As another aside, whilst there are times and situations when using goto is reasonable, it's generally a terrible idea (as it is here). What you're doing here is clumsily implementing your own loop instead of just using while or for. You've been told to do it, so do it, but this is something that would never be done in "real" code.

Thanks brother
You are awesomeee....

Also goto is legal in c++, its not a good practice to use it at all. Debuging and teamworking will be a hell for both parties. Its one big comand you should stay away from. Why not simple recursive function or a while loop?

When I was working a programmer would have been repremanded for using the goto statement. On the otherhand, I saw it used once in Micorosoft source code.

There are a few legitimate uses of goto, but it's not that it should ever be used carelessly. The main legitimate uses I can think of are: the "multi-break" and the "C-style cleaning". The "multi-break" refers to the problem of breaking out of a more than one nested loop at once, which is awkward without goto (usually involves flags and chaining of breaks). The "C-style cleaning" refers to the clean-up code that is needed when one chooses to use C-style constructs like dynamic array (with new[] or malloc()) instead of C++-style RAII objects (which automatically clean up during stack-unwinding). In that case, a goto (or set of gotos) beats repeating the clean-up code at every return-point, and risking repeating bugs. That C-style stuff is arguably no longer relevant in C++ though (I don't think I ever wrote a C++ function that required clean-up code, beyond the destruction of the stack objects).

Anyways, for such simple code, a goto is clearly not needed and should be avoided for good practice, in favor of a for-loop:

for(int i = 1; i <= n; ++i)
  f *= i;

Oh, and I can't finish without pointing out that the code shown is pre-standard (and non-standard). It seems to use a version of C++ before namespaces existed. This seems to match the screen-cap, i.e., MS-DOS from late 80s early 90s (i.e., more than 20 years old). The modern version of the code is, more or less, this:

#include <iostream>

int main() {
  int n = 1;
  std::cout << "Enter a number:\n";
  std::cin >> n;
  std::cout << "Factorial is ";
  int f = 1;
  for(int i = 1; i <= n; ++i)
    f *= i;
  std::cout << f << std::endl;
  return 0;
};

richieking
Brother i was given that question as a part of Test Paper in c++ using go to
so during that time i had no option to use anything other than go (understands that for or while makes it more easier

Ancient Dragon
Can u just expalin that point?

You can always make a recursion instead..

Can u just expalin that point?

It was company coding standards that the goto statement should never be used. Most software companies have coding standards that they expect their employees to abide by. Programmers aren't given free reign to code programs any way they wish, but must adhere to certain coding standards. That makes sense because among other things it helps others to quickly read and understand someone else's code. There are no universal coding standards, each company sets it's own.

You can always make a recursion instead..

But recursion should be a last resort, mainly because it's easy to run out of stack space and other memory. That's not to say there aren't legitimate uses for recursion, only that recursion should not be substituted for more efficient loops.

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.