2

Accepting user empty input and passing default value from the constructor

I just start learning C++. Now I was trying creating a simple requestion header. What I was expected is the "ABC Industries" should be used as a default value for the purchaser name and "XYZ Supplier" should be used as a default for the vendor name.

I created a default constructor and a param constructor. Here is my code:

class Requisition{
    private:
        string purchaser, vendor;
    
    public:
        Requisition()
        :purchaser("ABC Industries"),vendor("XYZ Supplier"){}

        Requisition(string pname, string vname)
        :purchaser(pname),vendor(vname){}

        void getStaffInput(){
            cout << "Enter a purchaser name: ";
                cin>>purchaser;
            cout << "Enter a vendor name: ";
                cin>>vendor;
        }

        void printHeader(){
            cout << "\n********************************************************\nPurchaser: "
                 << purchaser <<"\nVendor: "<<vendor
                 <<"\n********************************************************"<<endl;
        }
};

Here is my question:

  1. How can I accept user inputting a empty string (user direct press enter or spacing) because in C++ will force to input something to continue.

  2. If user doesn't key in any data, or just inputting either one of the data (purchaser or vendor), How can I pass the default value to the print function?

int main(){
    Requisition req;
    req.getStaffInput();
    req.printHeader();
}

My program output is forcing the user to input something (can't be blank and spacing), the program should accepting those blank or spacing input and get the default value from the default constructor and display it.

Thanks.

3
  • 2
    use std::getline Commented Nov 11, 2022 at 14:13
  • I edited the title to more accurately reflect the essence of your question. Commented Nov 11, 2022 at 16:34
  • Thank you, the title is actually referring to the second question, i tried to use "getline" but if user inputting blank or spacing, how should i modify the program to get the default value? Commented Nov 12, 2022 at 1:59

1 Answer 1

2

cin into a string will read, effectively, one token. The exact rules are complicated, but it basically pulls content until a space or newline. If you don't input any content, it waits until there's something.

You're looking for getline, which reads until the next newline and returns whatever it finds, even if what it finds is nothing at all.

std::getline(std::cin, purchaser);
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, there is actually two question here, I used the "getline" but the result giving me blank instead of the default value, how can I actually pass the default value to the output?
getline will return a blank string if given no input. What you do with it after that is irrelevant. If you want to set your string variable to some specific default value, that's your prerogative. It's not getline's job. getline gives you the user's input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.