Thread: Muti-diemsional array problem

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Muti-diemsional array problem

    Hi Guys

    im getting an error with my code.

    Code:
    main.cpp invalid conversion from `char' to `const char*' 
    initializing argument 2 of `int strcmp(const char*, const char*)'
    Im trying to read from a 2-dimensional array and Im not too good with strcmp so maybe thats my issue. Originally i was going to use this format:

    Code:
    if ( pc == list[0][0] )
    {
       // do somthing
    }
    But I got a casting char pointer error.

    Here is my code

    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    
    // function prototypes
    void itemSelection (  char[][ 80 ] );
    void calculatePayment (  char[][ 80 ], char[], unsigned short& );
    
    // main - begins program execution /////////////////////////////////////////////
    //
    int main(int argc, char *argv[])
    {
       std::cout << std::setprecision( 2 ) << std::fixed;
    
       // array of 20 strings each can
       // hold length of 80 characters
       char itemList[ 20 ][ 80 ] =
       {
          "0120", "Baked Beans",
          "0121", "Fish Cakes",
          "0122", "Bread",
          "0123", "Apple Pie",
          "0124", "Milk",
          "0125", "Tin Of Peas",
          "0126", "Breakfast Cereal",
          "0127", "Pactet Of Biscuits",
          "0128", "Tea Bags",
          "0129", "Meat"
       };
    
       itemSelection ( itemList );
    
       std::cin.get(); // freeze console output window
       std::cin.ignore();
    
       return 0; // return value from main to OS
    }
    
    // function to make a selection from the item list
    void itemSelection ( char list[][ 80 ] )
    {
       int i;
       unsigned short itemQuant;
    
       char productCode[ 80 ];
    
       std::cout << "Enter product code: ";
       std::cin >> productCode;
    
       for ( i = 0; i < 20; i += 2 )
       {
          if ( !strcmp ( productCode, list[ i ] ))
          {
             std::cout << "\nItem is: " << list[ i + 1 ]
                       << "\nPlease enter the quantity of this item: ";
             std::cin >> itemQuant;
    
             if (( itemQuant <= 0 ) || ( itemQuant > 100 ))
             {
                std::cout << "\nThe entered amount is negative ot exceeds the\n"
                          << "The limit allowed." << std::endl;
             }
    
             calculatePayment ( list, productCode, itemQuant );
             break;
          }
       }
    
       if ( i == 10 )
       {
          std::cout << "\nItem not found!" << std::endl;
       }
    }
    
    // function to calculate the payment
    void calculatePayment ( char list[][ 80 ], char pc[], unsigned short &rQuant )
    {
       if ( strcmp ( pc, list[0][0] ))  // ERROR LINE
       {
          std::cout << "\n\nWho said that!\n";
       }
    }
    Any help appreiciated, I know its somthing small thats the error but im having trouble correcting it.
    Double Helix STL

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Stop messing with char arrays in your C++ program would be my first thought.

    list is a pointer to an array of chars
    list[0] is an array of chars (what you want)
    list[0][0] is the first char of the first array.
    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.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Thanks Salem. I only used char arrays as i have been told to by the the book. I don't like mixing C-style code in C++ really.
    Double Helix STL

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps it's time for a new book then?
    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.

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I changed it around a bit and this seemed to work ok

    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    
    void selectItem ( std::string[][ 2 ] );
    
    // main - begins program execution /////////////////////////////////////////////
    //
    int main(int argc, char *argv[])
    {
       std::cout << std::setprecision( 2 ) << std::fixed;
    
       const int MAX_ROWS = 5;
       const int MAX_COLS = 2;
    
       std::string itemList[ MAX_ROWS ][ MAX_COLS ] =
       {
          { "0111", "BAKED BEANS" },
          { "0112", "TIN OF PEAS" },
          { "0113", "FISH CAKES" },
          { "0114", "TEABAGS" },
          { "0115", "SOUP" }
       };
    
       selectItem ( itemList );
    
       std::cin.get(); // freeze console output window
    
       return 0; // return value from main to OS
    }
    
    // function to select an item from the list
    void selectItem ( std::string list[][ 2 ] )
    {
       std::string productCode;
    
       std::cout << "Enter product code: ";
       std::cin >> productCode;
    
       if ( productCode == list[0][0] )
       {
          std::cout << list[0][1];
       }
    }
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Dynamically Increasing Array of Integers
    By laserlight in forum C++ Programming
    Replies: 30
    Last Post: 07-04-2008, 07:27 AM
  2. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM