By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,613 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,613 IT Pros & Developers. It's quick & easy.

Struck inside a struct

P: 16
Pls. give a hand. I have a simple mistake.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct User{
  5.      string read;
  6.      string write;
  7.      string execute;
  8. };
  9.  
  10. struct File
  11. {
  12.     string filename;
  13.     User accessrights;
  14.     File*next;
  15. };
  16.  
  17. bool isEmpty(File *head);
  18. char menu();
  19. void insertAsFirstElement(File *&head, File *&last, string filename, User accessrights);
  20. void insert(File *&head, File *&last, string filename, User accessrights);
  21. void remove(File *&head, File *&last);
  22. void showList(File *current);
  23.  
  24.  
  25. bool isEmpty(File *head)
  26. {
  27.     if(head == NULL)
  28.     return true;
  29.     else 
  30.     return false;
  31. }
  32. char menu()
  33. {
  34.  
  35.     char choice;
  36.  
  37.     cout<<"Menu\n";
  38.     cout<<"1.Add an item \n";
  39.     cout<<"2.Remove an item \n";
  40.     cout<<"3.Show the list \n";
  41.     cout<<"4.Exit \n";
  42.  
  43.     cin>>choice;
  44.  
  45.     return choice;
  46. }
  47. void insertAsFirstElement(File *&head, File *&last, string filename1, User accessrights1)
  48. {
  49.     File *temp = new File;
  50.     temp->filename = filename1;
  51.     temp->accessrights=accessrights1;
  52.     temp -> next = NULL;
  53.     head = temp;
  54.     last = temp;    
  55. }
  56. void insert(File *&head, File *&last, string filename2, User accessrights2)
  57. {
  58.     if(isEmpty(head))
  59.     {
  60.         insertAsFirstElement(head, last, filename2, accessrights2);
  61.  
  62.     }
  63.     else
  64.     {
  65.         File* temp = new File;
  66.         temp -> filename = filename2;
  67.         temp -> accessrights=accessrights2;
  68.         temp -> next = NULL;
  69.         last->next =temp;
  70.         last = temp;
  71.     }
  72. }
  73. void remove(File *&head, File *&last)
  74. {
  75.     if(isEmpty(head))
  76.     cout<<"The list is alreay empty.\n";
  77.     else if(head==last)
  78.     {
  79.         delete head;
  80.         head==NULL;
  81.         last = NULL;
  82.  
  83.     } else
  84.     {
  85.         File*temp = head;
  86.         head = head->next;
  87.         delete temp;
  88.     }
  89. }
  90.  
  91. void showList(File *current)
  92. {
  93.  
  94.   if(isEmpty(current))
  95.   {
  96.       cout<<"The list is Empty\n";
  97.   }
  98.   else
  99.   {
  100.       cout<<"The list contains : \n";
  101.       while(current !=NULL)
  102.       {
  103.           cout<<current->filename<<endl;
  104.           cout<<current->accessrights.read<<current->accessrights.write<<current->accessrights.execute;
  105.           cout<<"--------------------"<<endl;
  106.           current = current ->next;
  107.       }
  108.   }
  109.  
  110. }
  111.  
  112.  
  113. int main()
  114. {
  115.     File *head = NULL;
  116.     File*last = NULL;
  117.     char choice;
  118.     string filenamE;
  119.     User accessrightS;
  120.  
  121.  
  122.     do{
  123.  
  124.  
  125.         choice = menu();
  126.  
  127.         switch(choice)
  128.         {
  129.  
  130.          cout<<"Enter your choice :";
  131.            cin>>choice;
  132.  
  133.          case '1' : cout<<"Enter file name :";
  134.                      cin>>filenamE;
  135.                      cout<<"Grant Access rights :";
  136.                      cin>>User.read>>User.write>>User.execute;
  137.                      insert(head, last, filenamE, accessrightS);
  138.                      break;
  139.  
  140.          case '2' : remove(head,last);
  141.                      break;
  142.  
  143.          case '3' : showList(head);
  144.                      break;
  145.          default: cout <<cout<<"System exit\n";
  146.         }
  147.  
  148.     }while(choice!='4');
  149.  
  150. }
  151.  
  152.  

I have mistake in the line in Case 1 where I need to get three inputs from the user.
cout<<"Grant Access rights :";
cin>>User.read>>User.write>>User.execute;
insert(head, last, filenamE, accessrightS);

I get the error
------------------
D:\SEMESTER_F\FavQuotes\Source & Exe\FMSFinal.cpp In function 'int main()':
137 19 D:\SEMESTER_F\FavQuotes\Source & Exe\FMSFinal.cpp [Error] expected primary-expression before '.' token
137 30 D:\SEMESTER_F\FavQuotes\Source & Exe\FMSFinal.cpp [Error] expected primary-expression before '.' token
137 42 D:\SEMESTER_F\FavQuotes\Source & Exe\FMSFinal.cpp [Error] expected primary-expression before '.' token
Nov 2 '16 #1
Share this Question
Share on Google+
10 Replies


P: 16
hey guys I 'm now able to do the prog. by myself
:) :)
Nov 2 '16 #2

P: 16
Thanks for viewing my problem although!!
Nov 2 '16 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Too many errors for me to even begin advising you.

It also looks like you are trying to do a linked list without using nodes.

Would you please explain more fully your code?
Nov 3 '16 #4

P: 16
Hi weaknessforcats,
Thank you for the reply. I am trying to get what is show in the attached linkedlist structure
https://www.dropbox.com/s/cr5jxz9pph...truct.jpg?dl=0
Nov 4 '16 #5

P: 16
Hi guys don't just view give a help!
Nov 5 '16 #6

weaknessforcats
Expert Mod 5K+
P: 9,197
OK. I understand.

What you have here is a linked list of files where each file is a linked list of users. Sometimes this is called a two dimensional linked list.

What you try to do this write a linked list that can be used for both files and users. That means writing the linked list when you don't know the data that's contained within the list.

The common design is a cursor-based linked list. In this design there is a list of nodes where each node is one of your data elements. Here you have two structs. The struct List for the list itself and a struct Node for the nodes in the list:

Expand|Select|Wrap|Line Numbers
  1. struct LinkedList
  2. {
  3.                 int   type; //1 = file 2 = user
  4.         Node* start;
  5.         Node* end;
  6.                 Node* current;
  7. };
This LinkedList struct s called a cursor. All it knows is where the list starts, where it ends, and where you are currently positioned, and the type of data in the list.

The Node pointers (start, end, current) are 0 when the list is empty.

The int type is called a discriminator. If it is a 1 the linked list is for a file. If it is 2 the linked list contains users. type


The Node struct looks like:

Expand|Select|Wrap|Line Numbers
  1. struct Node
  2. {
  3.     void* data;
  4.  
  5.     Node* prev;
  6.     Node* next;
  7. };

You try to write the code so that the user in main() calls only functions with LinkedList pointers as arguments. These functions should access only the members of the LinkedList struct. If they are to insert data in the list then they call a function with a Node* argument to work with the actua nodes.

So here goes:

Expand|Select|Wrap|Line Numbers
  1. int main();
  2.  
  3. {
  4. LinkedList* myfiles = CreateLinkedList(1);     //create the list for files
  5. }
  6.  
  7.  
where

Expand|Select|Wrap|Line Numbers
  1. LinkedList* CreateLinkedList(int arg)
  2. {
  3.    LinkedList* thelist = new LinkedList;
  4.  
  5.    thelist->start = 0;
  6.    theList->end = 0;
  7.    theList->current = 0;
  8.    theList->type = arg;
  9.  
  10.  
  11.    return theklist;
  12. }
Does this make sense to you so far?
Nov 5 '16 #7

P: 16
Thanks for the reply :) Ok . will try like the way you explained
Nov 6 '16 #8

weaknessforcats
Expert Mod 5K+
P: 9,197
Ok. But let me go a little further. Let's add to the end of a linked list:

Expand|Select|Wrap|Line Numbers
  1. int main();
  2.  
  3. {
  4.     LinkedList* myfiles = CreateLinkedList(1);     //create the list for files
  5.  
  6.         //Create the data
  7.         File* ptr  = new File;
  8.  
  9.         AddToEnd(myfiles, ptr);
  10.  
  11.  
  12. }
where:

Expand|Select|Wrap|Line Numbers
  1. void  AddToEnd(LinkedList* thelist, File* thedata)
  2. {
  3.         //Create the node
  4.        Node* n = CreateNode(thedata);
  5.  
  6.       //See if the list is empty
  7.        if (thelist->start == 0)
  8.        {
  9.           thelist->start = n;
  10.           thelist->end = n;
  11.           return;
  12.        {
  13.        //Otherwise insert the second argument after the first argument
  14.  
  15.         InsertAfter(thelist->end, n); 
  16. {
This requires a function to insert one node after another:

Expand|Select|Wrap|Line Numbers
  1. void* InsertAfter(Node* first, Node* second)
  2. {
  3.  
  4.        //Is first the last node?
  5.        if (first->next == 0)
  6.        {
  7.        first->next = second;
  8.        second->prev = first; 
  9.        second->next = 0;
  10.         return;
  11.        }
  12.        //Otherwise it's in the middle somewhere 
  13.        second->next = first->next;
  14.        second->prev = first;
  15.        first->next->prev  = second;
  16.        first->next = second;
  17. }
Notice that all the pointer work with the nodes is inside InsertAfter. This is an example of encapsulation.
You do not want node pointers scattered all over the code.

I didn't compile this so let me know how it works.
Nov 6 '16 #9

P: 16
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4.     struct Node
  5.     {
  6.         void* data;
  7.  
  8.         Node* prev;
  9.         Node* next;
  10.     };
  11.  
  12.     struct LinkedList
  13.     {
  14.                     int   type; //1 = file 2 = user
  15.             Node* start;
  16.             Node* end;
  17.                     Node* current;
  18.     };
  19.     LinkedList* CreateLinkedList(int arg)
  20.     {
  21.        LinkedList* thelist = new LinkedList;
  22.  
  23.        thelist->start = 0;
  24.        thelist->end = 0;
  25.        thelist->current = 0;
  26.        thelist->type = arg;
  27.  
  28.  
  29.        return thelist;
  30.     }
  31.  
  32.  
  33.  
  34.  
  35.     void* InsertAfter(Node* first, Node* second)
  36.     {
  37.  
  38.            //Is first the last node?
  39.            if (first->next == 0)
  40.            {
  41.            first->next = second;
  42.            second->prev = first; 
  43.            second->next = 0;
  44.             return first;
  45.            }
  46.            //Otherwise it's in the middle somewhere 
  47.            second->next = first->next;
  48.            second->prev = first;
  49.            first->next->prev  = second;
  50.            first->next = second;
  51.     }
  52.  
  53.  
  54.     void  AddToEnd(LinkedList* thelist, Node* thedata)
  55.     {
  56.             //Create the node
  57.            Node* n = new Node;
  58.  
  59.           //See if the list is empty
  60.            if (thelist->start == 0)
  61.            {
  62.               thelist->start = n;
  63.               thelist->end = n;
  64.               return;
  65.            {
  66.            //Otherwise insert the second argument after the first argument
  67.  
  68.             InsertAfter(thelist->end, n); 
  69.     {
  70.  
  71.  
  72. int main()
  73.  
  74. {
  75.  
  76. LinkedList* myfiles = CreateLinkedList(1);     //create the list for files
  77. //Create the data
  78.         Node* ptr  = new Node;
  79.  
  80.         AddToEnd(myfiles, ptr);      
  81.  
  82.         return 0;
  83. }
  84.  


https://www.dropbox.com/s/4z4om39ftn...error.jpg?dl=0
Nov 7 '16 #10

weaknessforcats
Expert Mod 5K+
P: 9,197
I made a typo in the if statement inside AddToEnd. It has two {. Change the second one to }.


I would not have a Node* in main(). The user in main does not know about nodes. The user knows about lists. So you create your data and pass the address of the data to a function that also has a pointer to the list and it is this function (because it is a list function) that knows about nodes. So it is ere you create your node and then call a node function that knows how to connect nodes.

By having the Node* in main() you have exposed how the list is constructed. Should you redesign the list main() will have to be changed along with the redesigned list code. This type of coding is called "spaghetti" because the logic threads go all over the place.

It takes a little practice to compartmentalize your thinking into functional blocks but it is well worth the effort.
Nov 7 '16 #11

Post your reply

Sign in to post your reply or Sign up for a free account.