Thread: Error CC296 and C2297

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    42

    Error CC296 and C2297

    I can't figure out how to solve this error, please help.

    Code:
    double price[itemNumber] = {10.50, 13.00, 10.50, 13.00, 8.50, 10.00, 11.50, 13.50};
    double quantity[itemNumber];
    double amountDue;
    
    amountDue = quantity * price;
    I keep getting error C2296 '*': illegal, left operand has type 'double [8]' and error C2297 '*': illegal, right operand has type 'double [8]'. How do I solve this?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    C++ does not support vector operations. You have to manually multiply and add each element. And btw, quantity is not initialized, so you'd get garbage results anyway right now.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You could do something like this, to cheat, if you were so inclined:
    Code:
    template<typename T>
    std::vector<T> operator*(const std::vector<T>& lhs, const std::vector<T>& rhs)
    {
      std::vector<T> retVal(lhs.size());
      for (auto itLeft = lhs.begin(), itRight = rhs.begin(), itRes = retVal.begin(); itLeft != lhs.end() && itRight != rhs.end(); itLeft++, itRight++, itRes++)
      {
        *itRes = *itLeft * *itRight;
      }
    
      return retVal;
    }
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by nadeera View Post
    I can't figure out how to solve this error, please help.

    Code:
    double price[itemNumber] = {10.50, 13.00, 10.50, 13.00, 8.50, 10.00, 11.50, 13.50};
    double quantity[itemNumber];
    double amountDue;
    
    amountDue = quantity * price;
    I keep getting error C2296 '*': illegal, left operand has type 'double [8]' and error C2297 '*': illegal, right operand has type 'double [8]'. How do I solve this?
    std::vector is not a mathematical vector. It serves a purpose of a container storing its elements sequentially in a contiguous chunk of memory (it is a dynamically grown array).
    There are some pretty good libraries out there which can help you deal with math stuff, for example, you might want to check out Eigen.

  5. #5
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68
    Why didn't you define the quantity elements like you did the price? Also weren't they arrays? You're not specifying which element in the arrays your multiplying.
    UPDATE! As of 10/6/2014

    https://www.dropbox.com/s/2sj6qwpfbb...t%201.zip?dl=0
    Just find the application file and double click it. Controls are (Arrow keys) (Z) (Z + arrow key) (Spacebar)
    Don't play this crappy update. Wait for the newest one which is far more impressive.

    Official Sonic character poll hosted by some guy at Sega..... Sega of America. Vote for blaze because she OP.
    http://blogs.sega.com/2015/08/28/the...down-heats-up/

  6. #6
    Registered User
    Join Date
    Oct 2015
    Posts
    42
    This is basically what I want to do. If I initialize quantity[itemNumber] =0, I get error C2440: 'initializing': cannot convert from 'int' to 'double[8]'.

    Code:
    const int itemNumber = 8;
    	const string coffeeFlavours[itemNumber]= { "CappucinoR", "CappucinoL" "LatteR", "LatteL", "LongBlackR", "LongBlackL", " IceCoffeeR", "IceCoffeeL"};
    	double price[itemNumber] = { 10.50, 13.00, 10.50, 13.00, 8.50, 10.00, 11.50, 13.50 };
    	double quantity[itemNumber] = 0;
    	char pitz; 
    	double amountDue = 0, amountDue1, amountDue2,amountDue3,amountDue4, amountReceived , totalDue, change; 
    	int customerID, option;
    	float sum = 0, n, choice;
        cout << "\n\n\n\t\t\t!!MENU";
    	cout << "\n\n\nInsert Customer Order Number : ";
    	cin >> customerID;
    	cout << "\n\nCustomer No:" << customerID << endl;
    
    
    
    
    
    	cout << "\n\n            \t\t\t MENU\n" << endl;
    	
    	cout << "   ==| COFFEE FLAVOURS |==| REGULAR |==| LARGE |==|EXTRA CHARGE|==========";
    	cout << "\n   | 1-Cappucino           RM 10.50    RM 13.00  RM 2.00 for Extra Shot |";
    	cout << "\n   | 2-Latte               RM 10.50    RM 13.00                         |";
    	cout << "\n   | 3-Long Black          RM 8.50     RM 10.00                         |";
    	cout << "\n   | 4-Ice Coffee          RM 11.50    RM 13.50  RM 1.00 for Ice Cream  |\n";
    	cout << "\n  =======================================================================";
    	cout << endl;
    
    
    	do {
    		cout << "\nWhat do you want to order?";
    		cin >> option;
    
    		switch (option) {
    
    		case 1:
    		A :
    		  
    		  cout << "Cappucino" << endl ;
    		  cout << "How many?";
    		  cin >> quantity[itemNumber]; 
    		  cout << "\n\nDo you prefer :";
    		  cout << "\n\n1-Regular or 2-Large :";
    		  cin >> choice;
    		  if (choice == 1)
    		  {
    			  cout << "\n\tPrice: RM" << price[0]<< endl;
    			  amountDue1 = price[0] * quantity[itemNumber];
    			  cout << "\n\tAmount Due: RM" << amountDue << endl;
    		  }
    		  else if (choice == 2)
    		  {
    			  cout << "\n\tPrice: RM" << price[1] << endl;
    			  amountDue1 = price[1] * quantity[itemNumber];
    			  cout << "\n\tAmount Due: RM" << amountDue << endl;
    		  }
    		  else
    		  {
    			  cout << "Please enter 1 or 2 only";
    			  goto A;
    		  }
    		break;
    		case 2:
    		B :
    		  
    		  cout << "Latte" << endl ;
    		  cout << "How many?";
    		  cin >> quantity[itemNumber]; 
    		  cout << "\n\nDo you prefer :";
    		  cout << "\n\n1-Regular or 2-Large :";
    		  cin >> choice;
    		  if (choice == 1)
    		  {
    			  cout << "\n\tPrice: RM" << price[2]<< endl;
    			  amountDue2 = price[2] * quantity[itemNumber];
    			  cout << "\n\tAmount Due: RM" << amountDue << endl;
    		  }
    		  else if (choice == 2)
    		  {
    			  cout << "\n\tPrice: RM" << price[3] << endl;
    			  amountDue2 = price[3] * quantity[itemNumber];
    			  cout << "\n\tAmount Due: RM" << amountDue << endl;
    		  }
    		  else
    		  {
    			  cout << "Please enter 1 or 2 only";
    			  goto B;
    		  }
    		break;
    		case 3:
    		C :
    		  
    		  cout << "Long Black" << endl ;
    		  cout << "How many?";
    		  cin >> quantity[itemNumber]; 
    		  cout << "\n\nDo you prefer :";
    		  cout << "\n\n1-Regular or 2-Large :";
    		  cin >> choice;
    		  if (choice == 1)
    		  {
    			  cout << "\n\tPrice: RM" << price[4]<< endl;
    			  amountDue3 = price[4] * quantity[itemNumber];
    			  cout << "\n\tAmount Due: RM" << amountDue << endl;
    		  }
    		  else if (choice == 2)
    		  {
    			  cout << "\n\tPrice: RM" << price[5] << endl;
    			  amountDue3 = price[5] * quantity[itemNumber];
    			  cout << "\n\tAmount Due: RM" << amountDue << endl;
    		  }
    		  else
    		  {
    			  cout << "Please enter 1 or 2 only";
    			  goto C;
    		  }
    		break;
    		case 4:
    		D :
    		  
    		  cout << "Iced Coffee" << endl ;
    		  cout << "How many?";
    		  cin >> quantity[itemNumber]; 
    		  cout << "\n\nDo you prefer :";
    		  cout << "\n\n1-Regular or 2-Large :";
    		  cin >> choice;
    		  if (choice == 1)
    		  {
    			  cout << "\n\tPrice: RM" << price[6]<< endl;
    			  amountDue4 = price[6] * quantity[itemNumber];
    			  cout << "\n\tAmount Due: RM" << amountDue << endl;
    		  }
    		  else if (choice == 2)
    		  {
    			  cout << "\n\tPrice: RM" << price[7] << endl;
    			  amountDue4 = price[7] * quantity[itemNumber];
    			  cout << "\n\tAmount Due: RM" << amountDue << endl;
    		  }
    		  else
    		  {
    			  cout << "Please enter 1 or 2 only";
    			  goto D;
    		  }
    break;
    		default:
    			break;
    
    		}
    		cout << "\nDo you want to order another (y/n)? ";
    		cin >> pitz;
    	} while ((pitz == 'y') || (pitz == 'Y'));
    
    	amountDue = amountDue1 + amountDue2 + amountDue3 + amountDue4;
    	cout << "\nAmount to be paid: RM" << amountDue;
    
    	do {
    		cout << "\n\nYour cash is   : RM";
    		cin >> amountReceived;
    		if (amountReceived <amountDue){
    			cout << "\a\n\t     Your money is not enough! " << endl;
    		}
    	}
    
    	while (amountReceived <amountDue);
    
    	change = amountReceived- amountDue;
    	cout << "\nYour Balance  : RM" << change << "\n\n" << endl << endl;
    
    	cout << "\t\t  For your receipt: ";
    	system("pause");
    	system("cls");

  7. #7
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    If you want to initialize all the array elements to zero the syntax is:

    Code:
     doublequantity[itemNumber] = { 0 };
    If you want to assign the 8th element in the array the value 0 you need to declare the array first then try to address the 8th element.

    Code:
     doublequantity[itemNumber] = { 0 };
      
      quantity[itemNumber - 1] = 0;
    Last edited by Aslaville; 04-02-2016 at 07:14 AM. Reason: Address Jim's comments!

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If you want to assign the 8th element in the array the value 0 you need to declare the array first then try to address the 8th element.
    But that is only if your array has 8 or more elements. Since your array was defined with a size of itemNumber trying to access quantity[itemNumber] would be accessing your array out of bounds. Remember arrays start at zero and end at size - 1.


    Jim

  9. #9
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68
    Quote Originally Posted by nadeera View Post
    This is basically what I want to do. If I initialize quantity[itemNumber] =0, I get error C2440: 'initializing': cannot convert from 'int' to 'double[8]'.

    Code:
    const int itemNumber = 8;
        const string coffeeFlavours[itemNumber]= { "CappucinoR", "CappucinoL" "LatteR", "LatteL", "LongBlackR", "LongBlackL", " IceCoffeeR", "IceCoffeeL"};
        double price[itemNumber] = { 10.50, 13.00, 10.50, 13.00, 8.50, 10.00, 11.50, 13.50 };
        double quantity[itemNumber] = 0;
        char pitz; 
        double amountDue = 0, amountDue1, amountDue2,amountDue3,amountDue4, amountReceived , totalDue, change; 
        int customerID, option;
        float sum = 0, n, choice;
        cout << "\n\n\n\t\t\t!!MENU";
        cout << "\n\n\nInsert Customer Order Number : ";
        cin >> customerID;
        cout << "\n\nCustomer No:" << customerID << endl;
    
    
    
    
    
        cout << "\n\n            \t\t\t MENU\n" << endl;
        
        cout << "   ==| COFFEE FLAVOURS |==| REGULAR |==| LARGE |==|EXTRA CHARGE|==========";
        cout << "\n   | 1-Cappucino           RM 10.50    RM 13.00  RM 2.00 for Extra Shot |";
        cout << "\n   | 2-Latte               RM 10.50    RM 13.00                         |";
        cout << "\n   | 3-Long Black          RM 8.50     RM 10.00                         |";
        cout << "\n   | 4-Ice Coffee          RM 11.50    RM 13.50  RM 1.00 for Ice Cream  |\n";
        cout << "\n  =======================================================================";
        cout << endl;
    
    
        do {
            cout << "\nWhat do you want to order?";
            cin >> option;
    
            switch (option) {
    
            case 1:
            A :
              
              cout << "Cappucino" << endl ;
              cout << "How many?";
              cin >> quantity[itemNumber]; 
              cout << "\n\nDo you prefer :";
              cout << "\n\n1-Regular or 2-Large :";
              cin >> choice;
              if (choice == 1)
              {
                  cout << "\n\tPrice: RM" << price[0]<< endl;
                  amountDue1 = price[0] * quantity[itemNumber];
                  cout << "\n\tAmount Due: RM" << amountDue << endl;
              }
              else if (choice == 2)
              {
                  cout << "\n\tPrice: RM" << price[1] << endl;
                  amountDue1 = price[1] * quantity[itemNumber];
                  cout << "\n\tAmount Due: RM" << amountDue << endl;
              }
              else
              {
                  cout << "Please enter 1 or 2 only";
                  goto A;
              }
            break;
            case 2:
            B :
              
              cout << "Latte" << endl ;
              cout << "How many?";
              cin >> quantity[itemNumber]; 
              cout << "\n\nDo you prefer :";
              cout << "\n\n1-Regular or 2-Large :";
              cin >> choice;
              if (choice == 1)
              {
                  cout << "\n\tPrice: RM" << price[2]<< endl;
                  amountDue2 = price[2] * quantity[itemNumber];
                  cout << "\n\tAmount Due: RM" << amountDue << endl;
              }
              else if (choice == 2)
              {
                  cout << "\n\tPrice: RM" << price[3] << endl;
                  amountDue2 = price[3] * quantity[itemNumber];
                  cout << "\n\tAmount Due: RM" << amountDue << endl;
              }
              else
              {
                  cout << "Please enter 1 or 2 only";
                  goto B;
              }
            break;
            case 3:
            C :
              
              cout << "Long Black" << endl ;
              cout << "How many?";
              cin >> quantity[itemNumber]; 
              cout << "\n\nDo you prefer :";
              cout << "\n\n1-Regular or 2-Large :";
              cin >> choice;
              if (choice == 1)
              {
                  cout << "\n\tPrice: RM" << price[4]<< endl;
                  amountDue3 = price[4] * quantity[itemNumber];
                  cout << "\n\tAmount Due: RM" << amountDue << endl;
              }
              else if (choice == 2)
              {
                  cout << "\n\tPrice: RM" << price[5] << endl;
                  amountDue3 = price[5] * quantity[itemNumber];
                  cout << "\n\tAmount Due: RM" << amountDue << endl;
              }
              else
              {
                  cout << "Please enter 1 or 2 only";
                  goto C;
              }
            break;
            case 4:
            D :
              
              cout << "Iced Coffee" << endl ;
              cout << "How many?";
              cin >> quantity[itemNumber]; 
              cout << "\n\nDo you prefer :";
              cout << "\n\n1-Regular or 2-Large :";
              cin >> choice;
              if (choice == 1)
              {
                  cout << "\n\tPrice: RM" << price[6]<< endl;
                  amountDue4 = price[6] * quantity[itemNumber];
                  cout << "\n\tAmount Due: RM" << amountDue << endl;
              }
              else if (choice == 2)
              {
                  cout << "\n\tPrice: RM" << price[7] << endl;
                  amountDue4 = price[7] * quantity[itemNumber];
                  cout << "\n\tAmount Due: RM" << amountDue << endl;
              }
              else
              {
                  cout << "Please enter 1 or 2 only";
                  goto D;
              }
    break;
            default:
                break;
    
            }
            cout << "\nDo you want to order another (y/n)? ";
            cin >> pitz;
        } while ((pitz == 'y') || (pitz == 'Y'));
    
        amountDue = amountDue1 + amountDue2 + amountDue3 + amountDue4;
        cout << "\nAmount to be paid: RM" << amountDue;
    
        do {
            cout << "\n\nYour cash is   : RM";
            cin >> amountReceived;
            if (amountReceived <amountDue){
                cout << "\a\n\t     Your money is not enough! " << endl;
            }
        }
    
        while (amountReceived <amountDue);
    
        change = amountReceived- amountDue;
        cout << "\nYour Balance  : RM" << change << "\n\n" << endl << endl;
    
        cout << "\t\t  For your receipt: ";
        system("pause");
        system("cls");



    Code:
    const int itemNumber = 8;
    
    cin >> quantity[itemNumber
    What you're telling the computer to do is access the 9 th element of the array, but the array only has 8 members. Array start their count at 0. This means the 8 element is your array is 7. There is a mysterious 9 element but you would go out of bounds. I'm not a good coder but this would make it work and keep you in bounds.

    Code:
    const int itemNumber = 8;
    
    cin >> quantity[itemNumber - 1]
    UPDATE! As of 10/6/2014

    https://www.dropbox.com/s/2sj6qwpfbb...t%201.zip?dl=0
    Just find the application file and double click it. Controls are (Arrow keys) (Z) (Z + arrow key) (Spacebar)
    Don't play this crappy update. Wait for the newest one which is far more impressive.

    Official Sonic character poll hosted by some guy at Sega..... Sega of America. Vote for blaze because she OP.
    http://blogs.sega.com/2015/08/28/the...down-heats-up/

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You should replace all those backward jumping goto statements with an appropriate loop structure.

    You should also move some code into other functions, say
    Code:
        cout << "\n   | 1-Cappucino           RM 10.50    RM 13.00  RM 2.00 for Extra Shot |";
        cout << "\n   | 2-Latte               RM 10.50    RM 13.00                         |";
        cout << "\n   | 3-Long Black          RM 8.50     RM 10.00                         |";
        cout << "\n   | 4-Ice Coffee          RM 11.50    RM 13.50  RM 1.00 for Ice Cream  |\n";
        cout << "\n  =======================================================================";
        cout << endl;
     
        do {
            cout << "\nWhat do you want to order?";
            cin >> option;
     
            switch (option) {
            case 1:
                doCappuchino();
                break;
            case 2:
                doLatte();
                break;
            // etc
            }
    
            cout << "\nDo you want to order another (y/n)? ";
            cin >> pitz;
        } while ((pitz == 'y') || (pitz == 'Y'));
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Oct 2015
    Posts
    42
    It's returning garbage values and I can't figure out why. It seems like there is something very simple I missed.

    Code:
    amountDue = amountDue1 + amountDue2 + amountDue3 + amountDue4;

  12. #12
    Guest
    Guest
    Did you fix the problems others pointed out, like line 4, line 40 etc.?

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Aslaville View Post
    If you want to initialize all the array elements to zero the syntax is:

    Code:
     doublequantity[itemNumber] = { 0 };
    Actually, it's more appropriate to say

    Code:
    double quantity[ItemNumber] = {};
    You don't have to initialize any elements of the array in C++. All elements which are not explicitly initialized as initialized to 0.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by Aslaville View Post
    If you want to initialize all the array elements to zero the syntax is:
    Code:
     doublequantity[itemNumber] = { 0 };
    This solution is misleading, because one could deduce from it that changing 0 to 1
    Code:
    doublequantity[itemNumber] = { 1 };
    would initialize all elements to 1, which is wrong, because it would only initialize the first one (the rest would still be zero).

  15. #15
    Registered User
    Join Date
    Oct 2015
    Posts
    42
    I have no idea what's wrong with this. It shows no error when it compiles but when I run it, it goes a little crazy.
    Error CC296 and C2297-capture-png

    Code:
    #include <iostream>#include <string>
    using namespace std;
    
    
    struct coffeeOrder {
       char name[50];
       char size[5];
       int noOfExtra;
       int noOfSugar;
    }order[3];
    
    
    void printOrder (coffeeOrder coffee); 
    
    
    int main() 
    {
    	int n;
        
    	for (n=0; n<3; n++) 
    	{
        cout << "Enter coffee order: " << endl;
    	cin.get(order[n].name, 50);
        cout << "Enter the size of the coffee: " <<endl;
    	cin.get(order[n].size, 5);
    	cout << "Enter extra item: " << endl;
        cin >> order[n].noOfExtra;
    	cout << "Enter no of sugar(s)" << endl;
    	cin >> order[n].noOfSugar;
    	}
    
    
    	cout << "\nDisplaying Information." << endl;
    	for (n=0; n<3; n++)   
    		printOrder (order[n]); 
    	return 0;
    } 
    
    
    void printOrder (coffeeOrder coffee) 
    { 
        cout << "Coffee Order: " << coffee.name << endl;
    	cout << "Size: " << coffee.size << endl;
        cout <<"Extra: " << coffee.noOfExtra << endl;
    	cout <<"No of Sugar: " << coffee.noOfSugar << endl;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-20-2016, 10:31 AM
  2. Replies: 2
    Last Post: 07-31-2015, 02:11 PM
  3. Compile error? Logic error? Syntax Error? Please help
    By Khody Afkhami in forum C Programming
    Replies: 4
    Last Post: 10-11-2014, 01:36 AM
  4. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  5. Replies: 3
    Last Post: 10-02-2007, 09:12 PM