Adding variables

This is a discussion on Adding variables within the C Programming forums, part of the General Programming Boards category; I need to add to variable and get the result to be held in another variable, here what ive out ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    5

    Adding variables

    I need to add to variable and get the result to be held in another variable, here what ive out but its giving me errors:

    printf("The total number of passengers onboard is %d\n\n", &totalpassengers = passengers + cardpassengers);

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    printf("The total number of passengers onboard is %d\n\n", totalpassengers = passengers + cardpassengers);
    ssharish2005
    Last edited by ssharish2005; 01-15-2006 at 08:41 AM.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    The code is going round on a loop and everytime I need it to add the prevoius result to the total which it total passengers;

    Code:
    /*************************************************/ 
    /****          Bus Ticketing System           ****/ 
    /*************************************************/ 
    
    #include <stdio.h> 
    #include <conio.h> 
    #include "bcio2.h" 
    #include <time.h>
    
    void cash(void);//function prototype
    void card(void);//function prototype
    void time(void);//function prototype
    void welcome(void);//function prototype
    void totalpassengers(void);//function prototype
    
    main()
    {
    	int passengers =0;
    	int stops;
        int testnum;
    	int totalpassengers = 0;
    	int cardpassengers = 0;
    
    		for(stops = 1; stops < 13 ; stops++) {
    	do{
    		
    		welcome();
    		time();
    
    		totalpassengers = passengers + cardpassengers;
    
    		printf("The total number of passengers onboard is %d\n\n", totalpassengers = passengers + cardpassengers);
    
    		printf("This is stop number %d\n\n", stops);
    		
    		printf("Enter number of passengers for this one ticket\n\n");
    		
    		scanf("%d", &passengers);
    
    		printf("Any bus passes? enter number\n\n");
    
    		scanf("%d", &cardpassengers);
    		
    		printf("The cost of the ticket will be %.2f\n\n", passengers * 0.75);
    			
    		getch();
    		clrscr();
    		
    
    		switch(testnum) {
    		case 0 : continue;//function call
    			break;
    		case 1 : cash();//function call
    			break;
    		case 2 : card();//function call
    			break;
    		
    		}
    	}while(testnum !=0);
    	return 0;
    }
    }
    
    	/***********************************/
    	/*** Start Of Function Defintions***/
    	/***********************************/
    
    	void cash(void)//function defintions
    	{
    		printf("There are %.2f passengers on this ticket, passengers * 0.75");
    	}
    
    	void card(void)//function definitions
    	{
    		printf("Welcome aboard card holder!");
    	}
    
    	void time(void)//function definitions
    	{
    		        char dateStr [9]; 
              char timeStr [9]; 
              _strdate( dateStr); 
              printf( "The current date is %s \n", dateStr); 
             _strtime( timeStr ); 
             printf( "The current time is %s \n\n", timeStr); 
    	}
    
    	void welcome(void)//function definion
    	{
    		puts("\t\t\tNAME EDITED OUT\n\t\t\tBus Ticketing System\n");
    	}

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    u need to align the code properly first of all
    Code:
    int main()
    {
    	int passengers =0;
    	int stops;
        int testnum=0;
    	int totalpassengers = 0;
    	int cardpassengers = 0;
    
    		for(stops = 1; stops < 13 ; stops++) 
            {
    	       do
               {
    		      welcome();
    		      time();
    
    		      printf("The total number of passengers onboard is %d\n\n", totalpassengers = passengers + cardpassengers);
    		      printf("This is stop number %d\n\n", stops);
    		      printf("Enter number of passengers for this one ticket\n\n");
    		      scanf("%d", &passengers);
    		      printf("Any bus passes? enter number\n\n");
    		      scanf("%d", &cardpassengers);
    		      printf("The cost of the ticket will be %.2f\n\n", passengers * 0.75);
    		      printf("Test num : ");
    		      scanf("%d",&testnum);
    		      getch();
    		      clrscr();
    		      switch(testnum) 
                  {
    		          case 0 : continue;//function call
    			         break;
    		          case 1 : cash();//function call
    			         break;
    		          case 2 : card();//function call
    			         break;
    		      }
    	       }while(testnum !=0);
            }
        return 0;
    }
    2. u dont get any value to exit from a loop u chek testnum is not equal but initally u havn't initialzed the testnu value. which actuall contains some junk
    3. see the highlighted code

    ssharish2005

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    totalpassengers = passengers + cardpassengers;
    printf("The total number of passengers onboard is %d\n\n", totalpassengers);

    Also, shouldn't you be doing this calculation AFTER you've input some data?

    Also, time() is the name of a function in time.h. Declaring your own time() function as well simply confuses things.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 11:02 AM
  2. Father and Son Variables
    By khdani in forum Linux Programming
    Replies: 3
    Last Post: 11-28-2008, 05:42 PM
  3. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  4. Headers and Global Variables
    By ajoo in forum C++ Programming
    Replies: 1
    Last Post: 05-15-2004, 04:49 AM
  5. Replies: 6
    Last Post: 01-02-2004, 12:01 PM

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