sharing balance

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tata123
    New Member
    • Jun 2013
    • 1

    sharing balance

    hello, i m a newbie in c++..i nid u all to help me..how to share the new_balance with void call() and void sms()?

    Code:
    #include<stdio.h>
    void sms();
    void call();
    void topup();
    void logo();
    
    void main()
    {
    logo();
    }
    
    void logo()
    {
    printf("[1] Call, [2] SMS, [3] TOP UP, [4] EXIT ? ");
    	scanf("%d", &choose);
    	if(choose == 1)
    	{
    	printf("\nYou Have Choosen %s\n", "CALL");
    	call();
    	}
    	else
    	{
    	}
    	if(choose == 2)
    	{
    	printf("\nYou Have Choosen %s\n", "SMS");
    	sms();
    	}
    	else
    	{
    	}
    	if(choose == 3)
    	{
    	printf("\nYou Have Choosen %s\n", "TOP UP");
    	topup();
    	}
    	else
    	{
    	}
    	if(choose == 4)
    	{
    	printf("\nYou Have Choosen %s\n", "EXIT");
    	logo();
    	}
    	else
    	{
    	}
    }
    void call()
    {
    double balance = 10.0, call_duration, new_balance;
    printf("The Prepaid Balance = %.2lf\n", balance);
    printf("Call Duration(minutes) : ");
    scanf("%f", &call_duration);
    new_balance = balance - (call_duration * 0.50);
    printf("Your new balance = %.2lf", new_balance);
    }
    void sms()
    printf("The Prepaid Balance = %.2lf\n", new_balance);
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You need to create new_balance outside these functions. There's no sharing when each function has created its own new_balance.

    Next, you pass the address of new_balance to each of the functions.

    Next each function dereferences the address to use the shared new_balance.

    Code:
    float new_balance;
    etc...
    sms(&new_balance);
    call)&new_balance);
    etc...
    Then inside he functions you dereference the address:

    Code:
    void sms(float* ptr)
    {
       printf("%f", *ptr};
    }
    Here the function expects the address of a float. That is, a float pointer. The pointer, ptr, is the address of the float which makes *ptr the float itself. The function will work with any float so it is up to yu to pass in the address of the correct float.

    Comment

    Working...