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;
}