how to pass function return from switch

This is a discussion on how to pass function return from switch within the C++ Programming forums, part of the General Programming Boards category; How to do i fix this code im not good at using function, im trying to pass a statement from ...

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    3

    Post how to pass function return from switch

    How to do i fix this code im not good at using function, im trying to pass a statement from switch to my double passengersR i always come out with a error

    this output:
    Name passenger:
    Enter number passenger:
    Choose from A to D
    if choose A
    Total Regular Passenger: 6.19624e-312

    did i do something wrong im not really good at function and im trying to learn how to pass switch to void function

    Code:
    #include<conio.h>
    #include <iostream>
    #include <Iomanip>
    #include <string>
    
    using namespace std;
    
    double passengersR ()
    {
           
    double m;
    int num;
    
    m=120*num;
    return m;
    
    }
    
    int main()
    {
        double passengersR ();
        double m;
        char x ;
        string Name;
        float tickPrice;
        int  num;
        string morePassengers,name;
        
        cout<<"Enter Passenger Name:";
        cin>> Name;
        cout<<"Enter Number of passenger ";
        cin >> num;
        cout<<" A.Regular B.Student  C.Senior D.Children ";
        cin>>x ;
             
        cout << endl << endl; 
        cout << x << endl ;
        
        
        switch (x)
        {
        case 'a':
        case 'A':       
        cout<<"Total Regular Passenger:" << m << endl;      
        break;
        }
        
        
        
        
        getch();
    
    }

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
        double passengersR (); // this is a declaration of the function passengersR
        double m; // Here a variable m is declared, its value is undefined
    guess that is not what you want
    Code:
        double m = passengersR (); // Here a variable m is declared, it is initialized with the return value of the call to passengersR
    But then you still have a problem
    Code:
    double passengersR ()
    {
           
    double m;
    int num;  // the value of num is undefined could be any value 
    
    m=120*num; // if you multiply an undefined value by 120 still gives you an undefined value
    return m;
    
    }
    Kurt

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    3
    cout<<"Enter Number of passenger "; cin >> num;


    Code:
    m=120*num;
    The num here is for the how many the passenger. for example 120 x 12 = 1440

    i just don't know how to call my function and display or return value on my switch case

    Total Regular Passenger: 6.19624e-312 this always my problem how do i fix this problem?

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I understand but the num in passengersR () is a different num from the one in main.
    Guess you have to pass it as a parameter to the function
    e.g.
    Code:
    double passengersR ( int num ) {   
        double m;
        m=120*num;
        return m;
    }
    and call it like this

    Code:
    cout<<"Enter Number of passenger "; cin >> num;
    double m = passengersR (num);
    Kurt

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    3
    Thanks Alot Its my first time using function when trying to call from a switch you help me alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 09-15-2010, 05:38 AM
  2. Return to menu after switch executes
    By wco5002 in forum C++ Programming
    Replies: 5
    Last Post: 07-06-2008, 10:46 PM
  3. Replies: 3
    Last Post: 11-21-2007, 11:58 PM
  4. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  5. Pass values to C++ dll and return result
    By joeyzt in forum C++ Programming
    Replies: 2
    Last Post: 06-21-2004, 11:26 PM

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