Thread: Indexes problem

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Indexes problem


    Hi to all! I made the following code for an array of linked lists, the compiler doesn't show me any wrongs, but the code doesnt run at all.. any suggestions?
    Code:
    #include <iostream>  
    #include <stdio.h>   
    #include <conio.h>   
    #define size 5 
    using namespace std;    
    struct product {          
      char code[10];        
      char name[25];   
      char discription[25];               
      struct product *next;    };   
    
     int main() {    
    int ctr;  
    char productforsearch[25];  
    struct product *var[size];         
    for(ctr=0;ctr<size;ctr++) {    
     cout << "Please enter for " << ctr+1 << "product:";              cout<<"\nCode:\t";              
    cin >> var[ctr]->code;              
    cout<<"\nName:\t";              
    cin >> var[ctr]->name;              
    cout<<"Discription:\t";              
    cin >> var[ctr]->discription;                                  
    var[ctr]->next=NULL;    }        
    cout << "\nPlease enter product for search:\n";      
    cin >> productforsearch;    
    for(ctr=0;ctr<size;ctr++) {         
      if(var[ctr]->name==productforsearch)     
    cout << var[ctr]->code;  }    
    getch();  
    return 0;    } //end main
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,366
    Rep Power
    1870
    1. Your indentation needs work.
    Code:
    #include <iostream>
    #include <stdio.h>
    //!!#include <conio.h>
    #define size 5
    using namespace std;
    struct product {
      char code[10];
      char name[25];
      char discription[25];
      struct product *next;
    };
    
    int main()
    {
      int ctr;
      char productforsearch[25];
      struct product *var[size];
      for (ctr = 0; ctr < size; ctr++) {
        var[ctr] = new product;
        cout << "Please enter for " << ctr + 1 << "product:";
        cout << "\nCode:\t";
        cin >> var[ctr]->code;
        cout << "\nName:\t";
        cin >> var[ctr]->name;
        cout << "Discription:\t";
        cin >> var[ctr]->discription;
        var[ctr]->next = NULL;
      }
      cout << "\nPlease enter product for search:\n";
      cin >> productforsearch;
      for (ctr = 0; ctr < size; ctr++) {
        if (var[ctr]->name == productforsearch)
          cout << var[ctr]->code;
      }
    //!!  getch();
      return 0;
    }                               //end main
    2. You created an array of pointers, but didn't initialise those pointers.
    Here, I used
    var[ctr] = new product;

    3. Don't mix and match your header files. Stick to C++ header files, if you're writing C++. So if you really need something from C stdio, then you should have
    #include <cstdio>

    > if (var[ctr]->name == productforsearch)
    4. To compare C-style char array strings, you need strcmp()

    5. Stop using C-style char arrays for strings, and use std::string instead. Then == does what you expect.
    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

IMN logo majestic logo threadwatch logo seochat tools logo