How do I allow 2 words to be entered...?

This is a discussion on How do I allow 2 words to be entered...? within the C++ Programming forums, part of the General Programming Boards category; How do I allow 2 words entered into my program? Currently my below program works but when user enters 2 ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    21

    How do I allow 2 words to be entered...?

    How do I allow 2 words entered into my program?
    Currently my below program works but when user enters 2 words in pizza name variable the program shuts down...

    Code:
    //Jose Gonzales, key: #c++02, 45
    
    #include <iostream.h>
    
    struct pizza
    {
       char companyname[30];
       float diameter;
       float weight;
    };
    
    int main()
    {
           pizza analysis = {};
    
           cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++ \n";
           cout << " --William Wingate's Pizza-Analysis Service-- \n";
           cout << "\n";
           cout << "\n";
           cout << "\n";
           cout << "Enter the name of the Pizza Company: \n";
           cin >> analysis.companyname;
           cout << "Enter the diameter of pizza: \n";
           cin >> analysis.diameter;
           cout << "Enter the weight of the pizza: \n";
           cin >> analysis.weight;
            cin.get();
           cout << "\n";
           cout << "\n";
           cout << "\n";
           cout << "\n";
           cout << "\n";
           cout << "User entered information... \n";
           cout << "\n";
           cout << "Pizza Company: " << analysis.companyname << "\n";;
           cout << "Pizza Diameter: " << analysis.diameter << "\n";
           cout << "Pizza weight: " << analysis.weight << "\n";
           cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++ \n";
    
    cin.get(); //take out all cin.get statements...
    
    return 0;
    }
    Jose

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    cin.get(buffer, maxInputSize, terminatingChar);
    cin. getline(buffer, maxInputSize, terminatinChar);

    difference is get() won't remove terminatingChar from input buffer/stream whereas getline() will

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    21
    Thanks, but where and how do I incorporate your suggestion in my code, I tried to add it right after..."Enter the name of the Pizza Company: "...line and I get errors.

    I'm new to C++...
    Jose

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    change this:

    cin >> analysis.companyname;

    to this:

    cin.get(analysis.companyname, 29, '\n');

    or this:

    cin.getline(analysis.companyname, 29, '\n');

    either way you should be able to enter and store something like:

    Mario's Best

    or something like:

    Marias

    either one.

    Just be sure there isn't a newline char left in the input buffer before you make the call to get() or getline(). If there is a newline char left in the buffer, then it will be the first char seen by get() or getline(), and it will termintate input without actually putting anything into analysis.companyname.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    21
    Thank you so much, its working now.

    God Bless,
    Jose

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Sorting a list of entered words
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-07-2007, 09:40 AM
  3. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 09:01 AM
  4. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 07:00 PM

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