Need some help with a program

This is a discussion on Need some help with a program within the C++ Programming forums, part of the General Programming Boards category; Hi. I am trying to write a program to manage a computer lab with 2 types of computer, PC and ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    4

    Need some help with a program

    Hi. I am trying to write a program to manage a computer lab with 2 types of computer, PC and Sun. The program takes the input of computer name, ip, hd size, and admin pass. The Sun has an additional parameter of server type.The program needs to be able to add a computer, modify a computer, delete a computer, display the info for a computer, and be able to search by name and ip. I wrote the code for the add and delete functions just to test to see if I was on the right track.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class PC{
    	int x;
    public:
    	PC();
    	void addcomp(int);
    	void deletecomp(int);
    private:
    	int name[10];
    	int ip[10];
    	int hd[10];
    	int adpass[10];
    };
    void PC::addcomp(int x)
    {
    	cout << "Input name of computer: ";
    	cin >> name[x];
    	cout << "Input IP address: ";
    	cin >> ip[x];
    	cout << "Input size of hard disk (in GB): ";
    	cin >> hd[x];
    	cout << "Input Admin password: ";
    	cin >> adpass[x];
    }
    void PC::deletecomp(int x)
    {
    	name[x]=0;
    	ip[x]=0;
    	hd[x]=0;
    	adpass[x]=0;
    }
    int main()
    {
    	PC p;
    	int a,x;
    	cout << "::Select an operation::\n"<< "1) Add a Computer\n" << "2) Delete a Conputer\n" << "3) Modify a Computer\n" << "4) Search by Computer Name\n" << "5) Search by IP\n" << "6) Display Computer Information\n";
    	cin >> a;
    	cout << "Enter computer number: ";
    	cin >> x;
    	x=x-1;
    	if(a == 1)
    	{
    		p.addcomp(x);
    	}
    	if (a == 2)
    	{
    		p.deletecomp(x);
    	}
    
    	return 0;
    }
    These are the errors that I am getting from Visual C++
    Code:
    comuter.obj : error LNK2001: unresolved external symbol "public: __thiscall PC::PC(void)" (??0PC@@QAE@XZ)
    Debug/computer.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.
    I'm just starting out with classes so I'm not sure I have mine setup the way they should be. Also, should I simply write zeros to the arrays to clear them or is there a better way to do it? Any advice on my errors or how I should implement classes would be appriciated.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    It looks like a link error.
    comuter.obj : error LNK2001: unresolved external symbol "public: __thiscall PC::PC(void)" (??0PC@@QAE@XZ)
    Debug/computer.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.
    The linker is complaining because it cannot find the object code for the PC constructor, because you haven't written it yet. When you do, it will link, but for now you can only compile.

    If you ever get stuck with an error you can always search the web for the error code. (In this case searching for "LNK2001: unresolved external".) You'll find documents relating to your problem.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    4
    Ok, I figured out that problem. I now have a different issue. For example, I select addcomp, which then goes to through the addcomp function. After inputing everything, I want to return to the original menu at the beginning of the main function. I've been told that using goto is not the ideal way to do this. What should I do?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    4
    Here's another problem I am having. Here is a snippet of code
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    class PC{
    	int i,x;
    public:
    	void addcomp(int);
    	void deletecomp(int);
    	void modifycomp(int);
    	void searchname(string);
    	void searchip(string);
    	void display(int);
    private:
    	string name[50];
    	string ip[50];
    	float hd[50];
    	string adpass[50];
    };
    void PC::addcomp(int x)
    {
    	cout << "Input name of computer: ";
    	cin >> name[x];
    	cout << "Input IP address: ";
    	cin >> ip[x];
    	cout << "Input size of hard disk (in GB): ";
    	cin >> hd[x];
    	cout << "Input Admin password: ";
    	cin >> adpass[x];
    	cout << name[0] << ip[0] << hd[0] << adpass[0] << endl;
    	cout << name[1] << ip[1] << hd[1] << adpass[1] << endl;
    }
    I run through the addcomp function twice, storing the data first in [0] and then another set in [1]. This is what I expected to see (ex input a,a,2,a)
    Code:
    aa2a
    aa2a
    Instead I see
    Code:
      2 
    aa2a
    Why are the string values in the first array being deleted?

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    You've done some odd things with your PC object that are related to your problem.

    private:
    string name[50];
    string ip[50];
    float hd[50];
    string adpass[50];

    Some of these are arrays of strings. So now when you assign name[x] with cin or whatever, you're accessing an array item, rather than part of the name string. Since operator >> works differently for strings than it does for characters, that is probably the difference in outcomes. I think you wanted string variables, not string arrays. Incorporate that change and see if that fixes it.

    Keep in mind that you'll have to reserve space in your strings before you access any characters, like this:

    name.reserve(50);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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