Please help with struct

This is a discussion on Please help with struct within the C++ Programming forums, part of the General Programming Boards category; I have to construct a problem using struct and it's due pretty soon. I had 2 finals yesterday and didn't ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    9

    Please help with struct

    I have to construct a problem using struct and it's due pretty soon. I had 2 finals yesterday and didn't realize this one was due so soon. I know this is completely unethical but I'm pretty desperate.

    Thanks to anyone who even looks at this. Please help if you can! Thanks so much guys.


    • There are 50 employees in an organization
    • Create a “struct” called “employee” to hold the following employee records
    o Employee ID (0 to 49)
    o Employee Salary (2500 to 3500)
    o Employee Status (F or P)
    • struct will create a user-defined data-type for employee information
    • All the 50 employee’s record should have the same data-type of “struct employee”
    o Hint: Create a employee array of type “employee”
    • Randomly fill the salary and status values for all employees (using looping)

    • Ask user to enter a number between 0 to 49, say X
    • Print the employee X’s information

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Its nice to be polite and all, but it is still not a good idea to post a question and expect the solution. If you are that desperate, search for sites that offer this service, i.e. "hire a coder"/programmer or whatever its called. Of course those services cost money.

    If you want help, at least on this site, you will have to post an attempt. Start with one of these lines and work on it Start by creating the "employee" struct with the given fields (this is hopefully very easy to do). After that we can help you with the actual coding. All of the requirements are pretty straightforward, too. Your probably just overwhelmed by all of the text, but it is pretty clear as to what it requires if you take it one step at a time.

    If you dont want to post an attempt, then I wouldnt bother with this assignment, as exams are usually worth much more, so just focus on those instead.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    9
    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;

    struct employee
    {
    int eid [50];
    int esal [100];
    char estat[2];
    };

    void main ()
    {

    employee 1 = {
    1,
    2700,
    P
    };

    system ("pause");
    return 0;
    }

    So far I have this but I'm not sure if this is the right way to start and also I'm not sure how to set
    the char struct as far as limiting it to F and P. I'm willing to try to do this and I am trying but I just really desperately need some help at least. Thanks for the reply and any help you can provide

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Post all of your code in [CODE ] tags in the future, otherwise we will not read it (see the "CODE" button in "Advanced view" when you post). (Your only warning from me!)

    o Employee Status (F or P)
    This one is pretty vague, but we can assume its a char. You have used char[2], but you dont need it to be an array, just use "char" without the array.

    o Hint: Create a employee array of type “employee”
    Similar to how you create a char array, it tells you you have to create an array of struct employee, size of the array is 50.

    • Randomly fill the salary and status values for all employees (using looping)
    So for each of the 3 fields you have to create random numbers. See http://faq.cprogramming.com/cgi-bin/...&id=1043284385. Part of that article contains a custom function "GetRand" or whatever. If you use it word-for-word in your assignment, I imagine you should either state you have done so, or at least fully understand what its doing. Otherwise look for basic c++ random number examples online, it is very easy to find and also easy to use.

    When you generate the random numbers you will give a minimum and maximum value. You are also told what these min and max values are. For example:
    o Employee Salary (2500 to 3500)
    So you need to generate a random number from 2500 to 3500 and store it in that field.

    • Ask user to enter a number between 0 to 49, say X
    • Print the employee X’s information
    Read a number from the user (also a FAQ for this on this website), using scanf. Then use basic printfs to print the employee.

    When you try this out, post your code or any errors you get so we can help. If you dont understand, explain exactly what the problem is.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    9
    OK I'll take your advice and work on it and post back in a little bit. Thank you so much

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    9
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <stdlib.h>
    
    using namespace std;
    
    struct employee
    {
    
    int eid [50];
    int GetRand(int min, int max)
    {
      static int Init = 0;
      int esal;
      
      if (Init == 0)
      {
        srand(time(NULL));
        Init = 1;
      }
      esal = (rand() % (3500 - 2500 + 1) + 2500);
      
      return (esal);
    }
    
    char estat;
    };
    
    void main () 
    {
    	employee 1 = {
    	1,
    	2700,
    	P
    };
    
    	cout << "Enter a customer number between 0 and 49: " << endl;
    	cin >> eid >> endl;
    	cout << employee.eid employee.esal employee.estat << endl;
    
    system ("pause");
    return 0;
    }
    This is what I have so far. I have yet to get it to run correctly. I'm still trying to figure out how I'm going to store all the info for 50 employees and I know that the last c out statement to display the employee info is probably wrong, but I'm still working on it. Please let me know if you can help in any way. Thanks again.

  7. #7
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    last c out statement to display the employee info is probably wrong
    You know how to use cout<< "enter...49: << endl;
    You used << for each element.
    But your last line you try to << more elements than you included directions for.
    Asking a question you already know the answer to might teach you something you did not know...

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Move the function youve declared in the struct to outside the struct, like:
    Code:
    struct employee
    {
    
    int eid [50];
    char estat;
    };
    
    int GetRand(int min, int max)
    {
      static int Init = 0;
      int esal;
      
      if (Init == 0)
      {
        srand(time(NULL));
        Init = 1;
      }
      esal = (rand() % (3500 - 2500 + 1) + 2500);
      
      return (esal);
    }
    Then you also need the third field in the struct, so add the "salary" field. Also, as I said above, you should understand what the code is doing, which apparently you dont. You need to use the "min" and "max" arguments instead of the constants "3500" and "2500".

    You then need an array of 50 employees, like
    Code:
    struct employee myEmployees[50];
    and you need to use a for loop, as it says, from 0 to 49 to randomly fill in the 3 fields for each employee. So you will have 3 random numbers to generate inside the for loop.

    Try these changes and let us know any problem.

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    9
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <stdlib.h>
    
    using namespace std;
    
    struct employee 
    {
    int eid [50]; 
    char estat;
    };
    
    int GetRand(int min, int max)
    {
      static int Init = 0;
      int esal;
      
      if (Init == 0)
      {
        srand(time(NULL));
        Init = 1;
      }
      esal = (rand() % (3500 - 2500 + 1) + 2500);
      
      return (esal);
    }
    
    void main () 
    {
    	int i,n;
    	employee 1[50], zero = {0,0,""},
    	myemployees[50]={
    	{1,esal,P}
    	{2,esal,P}
    	{3,esal,F}
    	//etc.
    };
    
    	n=50;
      for(i=0; i<n; i++)
    	  a[i]=myemployees[i];      //each element of the array is a structure
    
      for(i=0; i<n; i++)
    	cout << "Enter a customer number between 0 and 49: " << endl;
    	cin >> eid >> endl;
    	cout << employee.eid << employee.esal << employee.estat << endl;
    
    system ("pause");
    return 0;
    }
    This is what I have right now... but it's getting pretty confusing. I'm not sure how to get the information stored and displayed. I'm looking for code resources online and in my book but I just can't seem to grasp it. I'm not sure what to do now.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    You're not just new to C++, you're new to procedural programming as well because you're doing silly things that someone who has programmed in just about any language wouldn't do. It's most unfortunate that you've somehow made it this far.

    I strongly believe that in a fair world you should fail this assignment.
    It's purpose is for you to demonstrate the knowledge you have gained, and you clearly are out of your depth, and can not possibly gain the required knowledge in the time available. Therefore it would be misleading for you to hand in a well-written and functioning program for this.

    Just do your best and hand in whatever mess you can make, even if it doesn't compile. Failure can be good - just as long as it teaches you something!

    One last word: Learn how to at least do one part of this properly. It's better to demonstrate a good knowledge of one or two parts and not be able to do much of the rest, than it is to demonstrate that you can scrape by with just enough code to make it look like everything is there even though the whole thing is utter gibberish.
    Last edited by iMalc; 12-05-2009 at 09:16 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Dec 2009
    Posts
    9
    I'm not a C Major I'm going for a logistics major and its just a requirement and I honestly have never understood the language and I've tried learning it.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Is this even compiling for you? Anyways...

    I noticed something wrong I overlooked in your code
    Code:
    struct employee 
    {
    int eid [50]; 
    char estat;
    };
    'eid' should be a single int not an array.

    Also change
    Code:
      for(i=0; i<n; i++)
    	  a[i]=myemployees[i];      //each element of the array is a structure
    to
    Code:
      for(i=0; i<50; i++)
        {
    	  myemployees[i].eid = i;
              myemployees[i].salary = // generate a number in the required range here;      //each element of the array is a structure
             // note that you didnt listen to what i said above, you need to add this third "salary" field to the struct.
             // next generate and save a random number with 2 possible values (i.e. from 0 to 1, 1 to 2, whatever)
             // have an if statement so that if it is, say, 0, assign this employe's estat to 'f', otherwise assign it 's', or whatever the values are required to be.
    Also change
    Code:
      for(i=0; i<n; i++)
    	cout << "Enter a customer number between 0 and 49: " << endl;
    	cin >> eid >> endl;
    	cout << employee.eid << employee.esal << employee.estat << endl;
    to
    Code:
    	cout << "Enter a customer number between 0 and 49: " << endl;
    	cin >> eid >> endl;
    	cout << myemployees[eid].eid << myemployees[eid].salary << myemployees.estat << endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me please.
    By yann in forum C Programming
    Replies: 15
    Last Post: 09-29-2009, 09:04 PM
  2. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  3. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  4. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  5. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21