Thread: Can anyone help me with this??

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    4

    Can anyone help me with this??

    Code:
    #include<stdio.h>
    void main (void)
    {
    	int num;
    	float bbal,tchar,tcred,credl,nbal;
    	
    	printf("Enter account number (-1 to end):");
    	scanf("%d",&num);
    	while (num!=-1)
    	{
    	printf("Enter beginning balance: ");
    	scanf("%f",&bbal);
    	printf("Enter total charges: ");
    	scanf("%f",&tchar);
    	printf("Enter total credits: ");
    	scanf("%f",&tcred);
    	printf("Enter credit limit: ");
    	scanf("%f",&credl);
    	nbal=bbal+tchar-tcred;
    	
    	{if (nbal>credl)
    		{	printf("Account:%d\n",num);
    			printf("Credit limit:%.2f\n",credl);
    			printf("Balance:%.2f\n",nbal);
    			printf("Credit limit exceeded.\n\n");
    			printf("\n\nEnter account number (-1 to end):");
    			scanf("%d",&num);}
    	else (nbal<credl)
    		{	printf("\n\nEnter account number (-1 to end):");
    			scanf("%d",&num);}
    	}
    	}
    }
    Problem is, wether nbal exceeds credl or not...it still follows the function of (nbal>credl), its ignoring else.... btw, this whole thing should repeat itself until account number is -1.

    The result should come out just ask like its shown below if it exceeds or not. Thx! =)
    Attached Images Attached Images Can anyone help me with this??-assg-png 

  2. #2
    Registered User
    Join Date
    Jul 2011
    Location
    Bangalore,India
    Posts
    24
    check the else line...remove the condition after else it will work....
    and you could also reduce the code by removing redundant input of account number..
    Last edited by subhash.rao; 07-14-2011 at 04:14 AM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by subhash.rao View Post
    check the else line...remove the condition after else it will work....
    Not quite. Add the keyword "if" after the "else" keyword. (nbal < cred1) is not the opposite of (nbal > cred1).

    And main() returns int, not void.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Location
    Bangalore,India
    Posts
    24
    Quote Originally Posted by grumpy View Post
    Add the keyword "if" after the "else" keyword. (nbal < cred1) is not the opposite of (nbal > cred1).
    I said to remove condition because if u add if and make it else if(nbal < cred1) what will happen if for some input nbal is equal to cred1..for that u have write a seperate else again..correct me if I am wrong..

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by subhash.rao View Post
    I said to remove condition because if u add if and make it else if(nbal < cred1) what will happen if for some input nbal is equal to cred1..for that u have write a seperate else again..correct me if I am wrong..
    People rarely type in conditions after the else keyword unless they intend those conditions be tested, in which case the "if" keyword is necessary.

    Among people who I've seen doing this type of thing in the past: over 90% chance they've forgotten the "if" keyword, less than 10% they need to remove the condition being tested.

    That equates, across all people I've seen asking this sort of question, to over a 90% chance you are wrong. Either way, the OP will probably be slapping his or her forehead, because it is a simple and obvious error once pointed out.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Location
    Bangalore,India
    Posts
    24
    I was just trying to point out u may get undesirable output for some inputs....

    Quote Originally Posted by grumpy View Post
    People rarely type in conditions after the else keyword unless they intend those conditions be tested, in which case the "if" keyword is necessary.
    Anyway u r right..

    Quote Originally Posted by grumpy View Post
    Either way, the OP will probably be slapping his or her forehead, because it is a simple and obvious error once pointed out.
    hehe..
    Last edited by subhash.rao; 07-14-2011 at 05:01 AM.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    And this is C, not C++.

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    thanks ppl! =) im new in this...

Popular pages Recent additions subscribe to a feed