2016-08-05 32 views
-2

Beim Lernen von C++ - Klassen - Grundlegende Vererbung - hat mein Programm einen Fehler zurückgegeben, der besagt: "C++ verbietet den Vergleich zwischen Zeiger und Ganzzahl und C++ verbietet den Vergleich zwischen Zeiger und Ganzzahl". Was habe ich falsch gemacht? Danke für Ihre Hilfe! :-)C++ - Basisvererbung

#include <iostream> 
using namespace std; 

class Pizza 
{ public: int slices; char topping[10]; bool pepperoni , cheese ; }; 

int main() { 
// Make your own Pizza! 
Pizza pizza; 
cout << "\n You can have Cheese or Pepperoni Pizza!"; 
cout << "\n Type [cheese] or [pepperoni] \n"; 
cin >> pizza.topping[10]; 
if (pizza.topping[10] == "pepperoni") { pizza.pepperoni = true;} 
if (pizza.pepperoni == true) {cout << "How many slices of pepperoni would you like?";}; 

if (pizza.topping[10] == "cheese") { pizza.cheese = true;} 
if (pizza.cheese == true) {cout << "How many slices of cheese would you like?";}; 

cin >> pizza.slices; 
if (pizza.slices >= 1) {cout << "You ordered " << pizza.slices << " slices of " << pizza.topping[10] << " Pizza!"; } 
else if (pizza.slices <= 0) {cout << "Change your mind?"; } 
else { cout <<"Can't Decide? That's Okay.";} 
} 
+0

Es würde helfen, wenn Sie Ihren Code ein wenig besser formatiert. – PaulMcKenzie

+1

Wo ist die Erbschaft? – juanchopanza

+0

'cin >> pizza.topping [10];' - Was hast du damit erwartet? Sie haben 'std :: string' nicht in dem, was Sie gelernt haben, behandelt? – PaulMcKenzie

Antwort

-1

Vielen Dank für alle Antworten :-) PROGRAM funktioniert!

// C++ Class - Basic Inheritance - User Inputs Values 
#include <iostream> 
using namespace std; 

class Pizza 
{ public: int slices; string topping; bool pepperoni=false, cheese=false ; }; 

int main() 
{ 
// Make your own Pizza! 
Pizza pizza; 

cout << "\n You can have Cheese or Pepperoni Pizza!"; 
cout << "\n Type [cheese] or [pepperoni] \n"; 
cin >> pizza.topping; 

if (pizza.topping == "pepperoni") { pizza.pepperoni = true;} 
if (pizza.pepperoni == true) {cout << "How many slices of pepperoni would you like?"; 
cin >> pizza.slices; } 

else if (pizza.topping == "cheese") { pizza.cheese = true;} 
if (pizza.cheese == true) {cout << "How many slices of cheese would you like?"; 
cin >> pizza.slices; } 

if (pizza.slices >= 1) {cout << "You ordered " << pizza.slices << " slices of " << pizza.topping << " Pizza!"; } 
else if (pizza.slices <= 0) {cout << "Change your mind?"; } 
else { cout <<"Can't Decide? That's Okay.";} 
} 
0

Hier:

pizza.topping[10] == "pepperoni" 

topping[10] ist vom Typ char während "pepperoni" ist ein char-Array, das auf const char* abklingt. Sie können diese beiden Typen nicht vergleichen.

Wenn Sie wollen Zeichenfolge innerhalb Topping mit „pepperoni“ vergleichen, dann sollten Sie verwenden, zB:

if (strcmp(pizza.topping, "pepperoni") == 0) {} 

in C++ Sie std::string verwenden sollen, die Ihr Leben viel leichter machen werden.

btw. Wie in den Kommentaren angegeben, topping[10] ist außerhalb der Grenzen, die Undefined Behaiour ist, ist die schlimmste Sache als Kompilierungsfehler. Stellen Sie außerdem sicher, dass eine beliebige Zeichenfolge in der Spitze mit '\ 0' endet.

+4

Nicht nur ist 'toppings [10]' ein einzelnes 'char', es ist auch aus den Bergen. –