Thread: Help with chapter 6 (functions) practice problem 1

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    2

    Help with chapter 6 (functions) practice problem 1

    Hi everyone,
    Newbie here

    I tried to solve the first part of the practice problem 1 in chapter 6:

    " Take the "menu program" you wrote earlier and break it out into a series of calls to functions for each of the menu items."

    It works in CodeBlocks 13 on macosx 10.11, with the "return 0;" command added on line 44, but in CodeBlocks 16.01 on windows 7, the program runs, but freezes and closes abnormally and the console gives this message:

    "terminate called after throwing an instance od 'std:logic_error'
    what(): basic_string::_S_construct null not valid
    This application has required the Runtime to terminate it in an unusual way"

    (It does close as well without the "return 0;" line, but without the aforementioned message )

    What am I doing wrong ??

    Thank you for your help!

    Gonzo

    my code:
    Code:
    /*
    1.
    Write a menu program that lets the user select from a list of options,
    and if the input is not one of the options, reprint the list
    2.
    Put this program in a function and call it
    */
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string menu_item_input;
    
    string menu_item_chooser ( string menu_item_input )
    {
    cout << "Please choose an item from the menu: \n\n";
    cout << "pizza \n" << "pasta \n" << "sushi \n" << endl;
    
    for ( int i = 0; i < 5; i++ )
    {
    cin >> menu_item_input;
    
    if ( menu_item_input == "pizza" || menu_item_input == "pasta" || menu_item_input == "sushi" )
    {
    cout << endl << "Bon appetit !!!\n";
    break;
    
    }
    
    else if ( !(menu_item_input == "pizza" || menu_item_input == "pasta" || menu_item_input == "sushi") && i < 4 )
    {
    cout << endl << "This item is not on the list, please choose again\n\n";
    }
    
    else if ( !(menu_item_input == "pizza" || menu_item_input == "pasta" || menu_item_input == "sushi") && i >= 4 )
    {
    cout << endl << "You had 5 chances, too bad!\n";
    break;
    }
    return 0; 
    // this added line - that was needed to run on macosx triggers on windows 7 the following error:
    // "terminate called after throwing an instance od 'std:logic_error'
    // what(): basic_string::_S_construct null not valid
    // This application has required the Runtime to terminate it in an unusual way"...
    
    }
    
    }
    
    int main ()
    {
    
    menu_item_chooser( menu_item_input );
    
    return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    First of all, you should indent your code properly because, as it stands now, it's quite difficult to read it.

    Now, about your problem, if you don't want to return anything from a function it should be void, if you do want to then you should return the proper type.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    2
    Hi GReaper,
    Thank you for a quick reply.
    Sorry for non proper indentation..
    Your answer helped me figure out the problem and get the logic: it works now with
    Code:
     return menu_item_input;
    instead of
    Code:
    return 0;
    inserted just before the "menu_item_chooser" function's end bracket.
    Thanks again and have a great day!
    Gonzo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Jumping into C++ - Chapter 7 - Practice problem 1
    By ambro1987 in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2017, 09:00 AM
  2. Jumping into C++ : Chapter 6 Practice Problem 2
    By Markkusz in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2015, 08:26 AM
  3. Jumping into c++ practice problem poker chapter 9
    By menkle in forum C++ Programming
    Replies: 2
    Last Post: 09-06-2015, 02:43 PM
  4. Chapter 5 Practice Problem #3
    By Tay in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2015, 01:50 AM
  5. Jumping Into C++ Chapter 14 Practice Problem 1
    By ArtemisFowl2nd in forum C++ Programming
    Replies: 5
    Last Post: 04-16-2014, 09:36 PM

Tags for this Thread