-2

While learning C++ Classes - Basic Inheritance - my program returned an error saying: "C++ forbids comparison between pointer and integer and C++ forbids comparison between pointer and integer". Where did I go wrong? Thanks for your help! :-)

#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.";}
 }
חִידָה
  • 259
  • 1
  • 2
  • 14
  • It would help if you formatted your code a little better. – PaulMcKenzie Aug 05 '16 at 19:23
  • 1
    Where's the inheritance? – juanchopanza Aug 05 '16 at 19:23
  • `cin >> pizza.topping[10];` -- What were you expecting this to do? They didn't cover `std::string` in what you've been taught? – PaulMcKenzie Aug 05 '16 at 19:24
  • Use strings instead of char arrays. And by the way can you tell us where is inheritance in your code? – ani627 Aug 05 '16 at 19:26
  • 1
    It seems you need to [re-read a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) concentrate on the chapter on arrays. Then never use arrays again and use `std::string` for strings, `std::vector` for dynamic arrays, and possibly `std::array` for fixed-size arrays. – Some programmer dude Aug 05 '16 at 19:27

2 Answers2

0

Here:

 pizza.topping[10] == "pepperoni"

topping[10] is of type char while "pepperoni" is a char array which decays to const char*. You cannot compare those two types.

If you wanted to compare string inside topping with "pepperoni" then you should use for example:

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

in c++ you should use std::string that will make your life a lot easier.

btw. as stated in comments, topping[10] is out of bound which is Undefined Behaiour, way worst thing than compilation error. Also make sure any string in topping ends with '\0' character

marcinj
  • 48,511
  • 9
  • 79
  • 100
-1

Thanks for all the responses :-) PROGRAM works!

// 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.";}
}
חִידָה
  • 259
  • 1
  • 2
  • 14