Hi My name is C.D.,

I am currently taking C++ 1, I have an assignment that asks me to write a function that displays the prompt string and then reads a floating point number and then returns that number. The book gives an example of the code to use in the main. My problem is I have been trying for days to get the prompt part of the functon to show on the console and I can't figure out why it won't I have tried numerous ways. Here is my latest attempt that still does not work. the program accepts and returns the numbeer but I can not figure out how tho get the function to display the prompt. Please Help! Thank You, C.D.



    #include <iostream>

    #include <string>

    using namespace std ;

    double get_double(string Prompt)

    {
        string prompt ;

        cout << prompt ; 

        double salary ;

        cin >> salary ;

        return salary ;

    }

    void main()

    {

        double salary = get_double("Please enter your salary $") ;

            double perc_raise = get_double("What percentage raise would you like? ") ;

                double new_salary = salary * (perc_raise / 100) + salary ;

                    cout << "If I give you that much of a raise you would be making $ " << new_salary 

                          << ".\nYou better be worth it \n" ; 

            system("PAUSE") ;

    }

Your problem is on line 16 -- you are tring to output the wrong prompt variable. Just delete line 14 and change line 16 to output Prompt (capital 'P').

Thank You very much. I can't believe I have been driving myself crazy for two days over the p not being capitalized. The Textbook chose the name for the function and I never realized it was capital Thanks again.

Or:

    #include <iostream>
    #include <string>
    using namespace std ;
    double get_double(string Prompt)
    {
        double salary ;
        cout << Prompt << flush; 
        cin >> salary ;
        return salary ;
    }
    void main()
    {
        double salary = get_double("Please enter your salary $") ;
            double perc_raise = get_double("What percentage raise would you like? ") ;
                double new_salary = salary * (perc_raise / 100) + salary ;
                    cout << "If I give you that much of a raise you would be making $ " << new_salary 
                          << ".\nYou better be worth it \n" ; 
            system("PAUSE") ;
    }

If you don't include the flush io manipulator, then the system may, or may not display the prompt output. The endl manipulator will also flush the output buffer, but you may not want a new-line in the output.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.