CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2011
    Location
    Bronx, NY
    Posts
    2

    Question C++ code: Unable to construct "char" and "if else" statement

    Hello:
    I have written code for the following problem but, unable to get the correct results, please advise me as to how to fix it.

    I also, tried using char for making a declaration and in the statements. But,I removed it because it had too many errors. What is the correct way to format char for this type of problem.

    Thank You in advance.

    Question
    Jack is organizing a boat trip and needs to decide how many boats to order to hold all the people attending the event.

    Write a complete C++ program that
    1.prompts the user for the number of people that plan to attend the event.
    2. prompts him for how many people will fit in a boat. (Assume each boat will hold the same number of people).

    3. It then prints outs a message telling him how many boats he will need and whether they will be full or not.

    SAMPLE RUN:
    How many people are going?: 137
    How many people will fit in a boat: 10

    You will need 14 boats for 137 people.
    13 of them will be full and one will have only 7 people.

    ANOTHER SAMPLE RUN:
    How many people are going?: 144
    How many people will fit in a boat: 12

    You will need 12 boats for 144 people.
    All of them will be full.

    MY CODE
    #include <iostream>
    using namespace std;
    int main()
    {
    int Total_of_People_Attend_Event;
    int How_Many_Boats;
    int People_Per_Boat=Total_of_People_Attend_Event/How_Many_Boats;
    cout << "What is the Total_of_People_Attend_Event:\n";
    cin >> Total_of_People_Attend_Event;
    cout << "How_Many_Boats:\n";
    cout << "How many people:" << People_Per_Boat <<'\n';

    if (How_Many_Boats = 14)
    {
    cout << "need_14_boats_are_not_full:\n";
    }
    else

    cout << "You_need_12_boats_which_are_full:\n";



    return 0;

    }

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: C++ code: Unable to construct "char" and "if else" statement

    There are quite a number of basic errors in those few lines of code.
    I would highly recommend you to grab a C++ book and read it.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: C++ code: Unable to construct "char" and "if else" statement

    Some issues with the code:
    • You cannot use Total_of_People_Attend_Event and How_Many_Boats to calculate People_Per_Boat before you have assigned values to them.
    • After writing "How_Many_Boats", you should use cin again to read the value from the user.
    • Comparing in C++ uses operator == and not operator =
    • You are hardcoding values 14 and 12, that's not what your assignment is asking for.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  4. #4
    Join Date
    Feb 2011
    Location
    Bronx, NY
    Posts
    2

    Re: C++ code: Unable to construct "char" and "if else" statement

    Thank you for the help and if you know of a good Tutorial or textbook link for C++ beginners please let me know. I've tried several but, some of the information was outdated. I'm now using the Problem Solving with C++ by Walter Savitch.


    Have a Nice Day!

  5. #5
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: C++ code: Unable to construct "char" and "if else" statement

    Take a look at this list of books.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured