473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Struck inside a struct

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
10 1474
hey guys I 'm now able to do the prog. by myself
:) :)
Nov 2 '16 #2
Thanks for viewing my problem although!!
Nov 2 '16 #3
weaknessforcats
9,208 Expert Mod 8TB
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
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
Hi guys don't just view give a help!
Nov 5 '16 #6
weaknessforcats
9,208 Expert Mod 8TB
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
Thanks for the reply :) Ok . will try like the way you explained
Nov 6 '16 #8
weaknessforcats
9,208 Expert Mod 8TB
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
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
9,208 Expert Mod 8TB
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

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

Similar topics

1
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
5
by: Jason | last post by:
How do you access an int pointer inside a struct. This is how it seems like it would work to me, but it doesn't... struct maze { int num; int * roomnum; }; void setmaze(struct maze * maze)...
19
by: Geetesh | last post by:
Recently i saw a code in which there was a structer defination similar as bellow: struct foo { int dummy1; int dummy2; int last }; In application the above array is always allocated at...
1
by: Jón Sveinsson | last post by:
Hello everone I have code from c which I need to convert to c#, the c code is following, as you can see there is a struct inside struct typedef struct { some variables....
12
by: djhong | last post by:
Following is a snippet of a header file. Here, #define are inside struct evConn{} Any advantage of putting them inside struct block? Looks like there is no diff in scoping of each...
0
by: PauloD | last post by:
public struct Character_Items { public short Armor; public short Helm; public short WeaponL; public short WeaponR; public short Pants; public short Boots; public short Gloves; public short...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.