a simple program

This is a discussion on a simple program within the C Programming forums, part of the General Programming Boards category; I am writing this simple program of depositting and withdrawing an amount a printing the balance. The program is not ...

  1. #1
    JDmac
    Guest

    a simple program

    I am writing this simple program of depositting and withdrawing an amount a printing the balance.

    The program is not working correctly.....Any suggestions

    I am not too sure whether I should use double , int or float
    and also whether I should use gets fgets, scanf, or sscanf...I think this is where my problem is......


    Thanks


    Code:
    #include <stdio.h>
    #include<stdlib.h>
    
    int main(void)
    {
    	double old_balance, balance;
    	double deposit, withdraw;
    	int choice;
    	char line[15];
    
    	balance=0;
    
    	printf("Enter old balance: ");
    	gets(line);
    	sscanf(line,"%ld",&old_balance);
    
    	for(;;)
    	{
    		printf("\n   MENU\n");
    		printf("   1. deposit\n");
    		printf("   2. withdrawal\n");
    		printf("   3. quit\n\n");
    
    		printf("   choice: ");
    
    		if (scanf("%d", &choice)==1)
    		{
    			printf("\nEnter deposit amount: ");
    			fscanf(stdin,"%ld",&deposit);
    			balance= old_balance + deposit;
    			printf("\nNew balance: %ld\n", balance);
    		}
    
    		if (scanf("%d", &choice)==2)
    		{
    			printf("\nEnter deposit amount: ");
    			fscanf(stdin,"%ld",&deposit);
    			balance= old_balance + deposit;
    			printf("\nNew balance: %ld\n", balance);
    		}	
    
    		if (scanf("%d", &choice)==3)		
    				exit(1);
    	}
    	
    	return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's not working correctly because you're reading three different times. You need to read into the variable and then compare them, not just read one, then another then another.

    You're comparing the return value of scanf, not what's in the variable. Try using 1 scanf call, and then using the variable you read into for comparison checks.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>gets(line);
    Don't use gets(), that's the worst of the lot.

    You seem to have a variety of input functions.... I think you should stick to one, maybe fgets(). Use this for getting both text and numbers, then, if you want to convert the input to a number, use something like strtod() to convert to a double, or strtol() to convert to a long int.

    Alternatively, if all you are getting are numbers, you might want to learn about scanf(). But you won't need fscanf() or sscanf() in this case.

    >>if (scanf("%d", &choice)==1)
    >>if (scanf("%d", &choice)==2)
    Here you are getting two lots of input, whereas you should be getting one input and testing it against two values.

    >>sscanf(line,"%ld",&old_balance);
    Using %ld will get you a long int, to get a double, you need %lf.

    [edit]beat!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    JDmac
    Guest
    Thanks for the suggestions...

    Here is my updated code......How can I keep on adding a deposit and display each time an updated new_balance.

    Code:
    #include <stdio.h>
    #include<stdlib.h>
    
    int main(void)
    {
    	double old_balance, new_balance;
    	double deposit, withdraw;
    	int choice;
    	char menu[]="   MENU\n   1. deposit\n   2. withdrawal\n   3. quit\n\n";
    
    	printf("Enter old balance: ");
    	scanf("%lf",&old_balance);
    
    	for(;;)
    	{
    		printf("\n%s",menu);
    		
    		printf("   choice: ");
    		scanf("%d", &choice);
    
    		if(choice==1)
    		{
    			printf("\nEnter deposit amount: ");
    			scanf("%lf",&deposit);
    			printf("\nNew balance $ %0.2lf\n", old_balance+deposit);
    		}
    
    		else if (choice==2)
    		{
    			printf("\nEnter withdrawal amount: ");
    			scanf("%ld",&withdraw);
    			printf("\nNew balance $ 0.2%ld\n", new_balance-withdraw);
    		}	
    
    		else if (choice==3)	
    		{
    			puts("\n   Program Terminated");
    			exit(1);
    		}
    	}
    	
    	return 0;
    }

    Salem Disabled Smilies in This Post

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 02:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 03:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21