0

Can anyone explain whats wrong with this program why this is not working.

void main(void)
{
long a,b=1;
clrscr ();
printf ("Enter number to calculate its factorial:");
scanf ("%ld",&a);
if (a=0,1)
printf ("You entered 0 or 1");
else
for (a=a;a>0;a--)
{
b*=a;
}
printf ("Answer is %ld",b);
getch ();
}
5
Contributors
18
Replies
19
Views
7 Years
Discussion Span
Last Post by Xufyan
Featured Replies
  • 2

    line 7 is the problem. a=0 is an assignment. a==0 is a conditional statement. you need to understand the difference. also, you can't test multiple conditions by throwing a comma between values. you need to fully express each condition, separated by an AND (&&) or an OR (||) as the … Read More

  • 3
    WaltP 2,905   7 Years Ago

    [QUOTE=Xufyan;1162174]Can anyone explain whats wrong with this program [/quote] [ICODE]void main(void)[/ICODE] - [url=http://www.gidnetwork.com/b-66.html]see this[/url] [ICODE]clrscr ();[/ICODE] - very old non-standard function that hasn't existed since 1986 [ICODE]getch ();[/ICODE] - non-standard function that is very non-portable, works in very few compilers and [url=http://www.gidnetwork.com/b-38.html]no formatting[/url] making the program hard to follow. Read More

  • 3
    WaltP 2,905   7 Years Ago

    [QUOTE=Xufyan;1162623]@WaltP , Our teacher teaching us from the basic and they told us to use such functions, i don't know any other function to use then these [icode]void main(void)[/icode] and [icode]clrscr ()[/icode] & [icode]getch ()[/icode] , can you tell me which functions should i use istead of the functions above … Read More

  • 2

    [QUOTE=Xufyan;1162623]@jephthah, thanks alot :) but there is no such thing in my lesson how to give multiple values for [B]IF[/B][/quote] well, you were certainly trying to do so! but you were doing it wrong. IF takes one conditional statement. the statement may have one or more values. you need to … Read More

  • 2
    jonsca 1,059   7 Years Ago

    When you don't use braces to delineate your if/else groupings only the statement immediately after the if or else gets executed: [code] if(condition) This would get executed when condition was true A statement here would cause an error when an else is present else This would get executed if condition … Read More

2

line 7 is the problem.

a=0 is an assignment. a==0 is a conditional statement. you need to understand the difference. also, you can't test multiple conditions by throwing a comma between values. you need to fully express each condition, separated by an AND (&&) or an OR (||) as the logic you're trying to acheive requires.

so, what you are trying to say in line 7 should be if (a == 0 || a == 1) this is fundamental stuff. you need to review your early lessons on IF statements, conditional and assignment operators.

3

Can anyone explain whats wrong with this program

void main(void) - see this clrscr (); - very old non-standard function that hasn't existed since 1986 getch (); - non-standard function that is very non-portable, works in very few compilers
and no formatting making the program hard to follow.

0

@jephthah, thanks alot :)
but there is no such thing in my lesson how to give multiple values for IF

@WaltP , Our teacher teaching us from the basic and they told us to use such functions, i don't know any other function to use then these void main(void) and clrscr () & getch () , can you tell me which functions should i use istead of the functions above ?

3

@WaltP , Our teacher teaching us from the basic and they told us to use such functions, i don't know any other function to use then these void main(void) and clrscr () & getch () , can you tell me which functions should i use istead of the functions above ?

Then you are being seriously mistaught. The last version of Turbo C was ver 4 in 1993. That's 17 years ago. Many compilers have been following the changes in the standards and the industry since then. Maybe you should ask your instructors why they insist on using such old tools when they have been surpassed by many free compilers since then.

Edited by WaltP: Correcting a brain fart.

Votes + Comments
u'r great :)
2

@jephthah, thanks alot :)
but there is no such thing in my lesson how to give multiple values for IF

well, you were certainly trying to do so! but you were doing it wrong. IF takes one conditional statement. the statement may have one or more values. you need to be able to deal with ANDs and ORs in your conditional statement, as you can see by your need to find the condition when 'a' equals either 0 or 1. to try and do this with only one value at a time, would be ridiculous

@WaltP , Our teacher teaching us from the basic and they told us to use such functions, i don't know any other function to use then these void main(void) and clrscr () & getch () , can you tell me which functions should i use istead of the functions above ?

i wasn't going to bother correcting this, since you've got more fundamental problems... but since Walt brought it up:

(1) always use int main (void) ... you will need to add a return 0; at the end of your main block. "void main" is wrong. it's undefined and can cause all sorts of problems in larger projects. any instructor that tells his student to do so, is immediately suspect of incompetence.

(2) you should never use "clrscr()". it's entirely unnecessary and obnoxious.

(3) use getchar() instead of getch().

(4) stop using the conio.h library altogether. use stdio.h and stdlib.h functions for what you need to do as far as printing text and getting command line input. fancy formatting in command terminals has been out of style since the mid 1990's. we should all stop pretending that the command terminal is a GUI.

0

Thanks alot Jephthah & WaltP i've learned alot today !!
though

We're learning Turbo C from the book,

The write Group's Trubo C Programming for PC and Turbo C++ Revised Edition by Robert Lafore

can you suggest me a better b0ok to learn modern C language instead of these all functions ?

Edited by Xufyan: n/a

0

and here is my program now,

void main(void)
{
long a,b=1;
clrscr ();
printf ("Enter number to calculate its factorial:");
scanf ("%ld",&a);
if (a==0 || a==1)
printf ("You entered 0 or 1");
else
for (a=a;a>0;a--)
{
b*=a;
}
printf ("Answer is %ld",b);
getch ();
}

but there is still some problem, when i am writing 0 or 1 in the output , it is showing both the statements You entered 0 or 1 and Answer is 1 ..why is this happening so ? i want it to show only first statement when i enter 0 or 1

0

Thanks alot Jephthah & WaltP i've learned alot today !!
though

We're learning Turbo C from the book,


can you suggest me a better b0ok to learn modern C language instead of these all functions ?

When was that published? Looking for anything published in the last 5-8 years would be better.

i am in my second year doing part time teaching..thanks for asking

Then you need to relearn C/C++ and stop using ancient compilers that are not standard. Teach your students on current compilers (there are many free ones) and each them Standard C/C++, not all the old Borland/Turbo crap that's worthless in today's industry.

0

and here is my program now,

void main(void)
{
long a,b=1;
clrscr ();
printf ("Enter number to calculate its factorial:");
scanf ("%ld",&a);
if (a==0 || a==1)
printf ("You entered 0 or 1");
else
for (a=a;a>0;a--)
{
b*=a;
}
printf ("Answer is %ld",b);
getch ();
}

but there is still some problem, when i am writing 0 or 1 in the output , it is showing both the statements You entered 0 or 1 and Answer is 1 ..why is this happening so ? i want it to show only first statement when i enter 0 or 1

No more help until you get rid if void main() , clrscr() , and you format your code. No longer wasting my time.

0

so here is the code now without void main(void)

int main(void)
{
long a,b=1;
printf ("Enter number to calculate its factorial:");
scanf ("%ld",&a);
if (a==0 || a==1)
printf ("You entered 0 or 1");
else
for (a=a;a>0;a--)
{
b*=a;
}
printf ("Answer is %ld",b);
return 0;
}

Edited by Xufyan: n/a

1

this is written on the book
1990 by The Waite Group, Inc.

20 years old? Never learn from a computer book older than yourself! Jeez! :icon_rolleyes:

0

so can you suggest me a book ??
and what about the program now ??
can you solve the problem now ?

2

When you don't use braces to delineate your if/else groupings only the statement immediately after the if or else gets executed:

if(condition)
   This would get executed when condition was true
A statement here would cause an error when an else is present
else
   This would get executed if condition is false
   {
       The stuff in these braces would be executed if the condition was false because it is with 
        the "This would get executed" statement 3 lines before
   }
This statement would be totally on its own, not part of the if or the else

So now you see why your output on line 13 was executing regardless.

I don't have a good text recommendation for you. I used to like Kelley/Pohl "A Book on C" but it's getting a little outdated and might be a bit too dense for folks just starting out. Their book "C By Dissection" (meant for beginners) is not very effective IMO.

I'll chide you one more time about the formatting too. Read the article WaltP linked in Post #3 (it's written by some guy WaltP at another site, probably just a coincidence). It's definitely hard to match your braces up and what goes with what when everything is crammed up against the left hand side like that. If it's easier to read it's easier to help you.

Edited by jonsca: n/a

Votes + Comments
great - xufyan
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.