fejl i kode
faar fejl i denne kode jeg har fra en bog.. kan ikke faa det til at virke.. :(menu.h
#include <sstream>
//#include <strstrea.h>
#ifndef BOOLEAN
#define BOOLEAN
enum Boolean { false, true };
#endif
#ifndef menu_H
#define menu_H
const MAX_CONSOLE_MENU_SIZE = 10;
class Console_Menu {
public:
Console_Menu() { menu_size = 0; }
~Console_Menu() { }
void add_item(char* new_item);
void add_terminator(char* quit_label);
void add_prompt(char* menu_prompt);
void display_menu();
int get_user_selection();
int get_selection() { return selection; }
protected:
Boolean validate_selection(int selection);
char* menu_items[MAX_CONSOLE_MENU_SIZE];
char* prompt_string;
int menu_size;
int selection;
};
void Console_Menu::add_item(char* new_item)
{
char* menu_item;
char* item_string = new char[25];
sprintf(item_string, "%i. %s", menu_size+1, new_item);
menu_item = new char[strlen(item_string) + 1];
strcpy(menu_item, item_string);
menu_items[menu_size++] = menu_item;
delete[] item_string;
}
void Console_Menu::add_terminator(char* quit_label)
{
this->add_item(quit_label);
}
void Console_Menu::add_prompt(char* new_prompt)
{
prompt_string= new_prompt;
}
void Console_Menu::display_menu()
{
cout << endl;
for (int i = 0; i< menu_size; i++)
cout << menu_items[i] << endl;
cout << endl << prompt_string;
}
Boolean Console_Menu::validate_selection(int selection)
{
if((selection <= menu_size) && (selection > 0))
return true;
else
return false;
}
int Console_Menu::get_user_selection()
{
int choice, quit_choice = menu_size;
char choice_char;
Boolean valid_selection = flase;
while(!valid_selection)
{
this->display_menu();
cin.get(choice_char);
cin.ignore(100, '\n');
if (!isdigit(choice_char))
choice = 0;
else
choice = atoi(&choice_char);
if (!this->validate_selection(choice))
cout << "Invalid choice, try again." << endl;
else
valid_selection = true;
}
if(choice == quit_choice)
selection = 0;
else
selection = choice;
return selection;
}
menu.cpp
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#ifndef menu_H
#include "menu.h"
#endif
int main()
{
cout << endl << "CONSOLE MENU" << endl;
int selection;
Console_Menu menu;
menu.add_item("Increment");
menu.add_item("Decrement");
menu.add_item("Reset");
menu.add_terminator("Quit");
menu.add_prompt
("Make a selection: ");
selection = menu.get_user_selection();
while (selection != 0)
selection = menu.get_user_selection();
cout << endl << "The fucking end " << endl;
return 0;
}
kommer med denne fejl
isa@seline:01-fnk:~/myprograms/menu$ g++ menu.cpp
In file included from /usr/include/c++/3.3/backward/iostream.h:31,
from menu.cpp:1:
/usr/include/c++/3.3/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
In file included from menu.cpp:7:
menu.h:4: error: parse error before `false'
menu.h:11: error: ISO C++ forbids declaration of `MAX_CONSOLE_MENU_SIZE' with
no type
menu.h: In member function `Boolean Console_Menu::validate_selection(int)':
menu.h:70: error: return type `enum Boolean' is incomplete
menu.h:72: error: cannot convert `bool' to `Boolean' in return
menu.h:74: error: cannot convert `bool' to `Boolean' in return
menu.h: In member function `int Console_Menu::get_user_selection()':
menu.h:82: error: variable `Boolean valid_selection' has initializer but
incomplete type
menu.h:82: error: `flase' undeclared (first use this function)
menu.h:82: error: (Each undeclared identifier is reported only once for each
function it appears in.)
menu.h:93: error: invalid use of undefined type `enum Boolean'
menu.h:4: error: forward declaration of `enum Boolean'
In file included from menu.cpp:7:
menu.h:7:1: unterminated #ifndef