Click here to Skip to main content
14,865,476 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,

I'm new to C++ and am trying to make a basic program that displays some info about computers.
It is just for learning it isn't very practical.

I have a problem.
When adding more than one of the data types/class that I make, it causes the program to freeze on compile, yet when I remove it it works fine with one?

What am I doing wrong, assuming my method for displaying this data is the correct way.

Thanks in advance.


#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

class Computer{
      public:
      string AssignCompName(string compname);
      string AssignIP(string IP);
      string AssignDescription(string description);
      string AccessCompName();
      string AccessIP();
      string AccessDescription();
      private:
      string ComputerName,IPAddress,Description;
      
      };
      
string Computer::AssignCompName(string compname){
       ComputerName = compname;
       }
string Computer::AssignIP(string IP){
       IPAddress = IP;
       }
string Computer::AssignDescription(string description){
       Description = description;
       }
       
string Computer::AccessCompName(){
       return ComputerName;
       }
string Computer::AccessIP(){
       return IPAddress;
       }
string Computer::AccessDescription(){
       return Description;
       };

int main(int argc, char *argv[])
{
    Computer comp1;
    Computer comp2; // If I remove the second instance of my class it works fine but it seems to be this line that causes me troubles
    comp1.AssignCompName("Johnson");
    comp1.AssignIP("192.168.200.200");
    comp1.AssignDescription("Description");
    comp2.AssignCompName("Johnson");
    comp2.AssignIP("192.168.200.200");
    comp2.AssignDescription("Description");   
    cout << comp1.AccessCompName() << "\t";
    cout << comp1.AccessIP()<< "\t";
    cout << comp1.AccessDescription()<< "\t";
    
    
    cout << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}


Thanks for any help.

[edit]Code block fixed - OriginalGriff[/edit]

[edit2]< and > characteres fixed - OriginalGriff[/edit2]
Posted
Updated 28-Mar-11 5:40am
v4
Comments
Dalek Dave 28-Mar-11 11:40am
   
Edited for Grammar and Readability.

1 solution

In my opinion that code shouldn't compile at all.

C++
string Computer::AssignCompName(string compname){
       ComputerName = compname;
       }
string Computer::AssignIP(string IP){
       IPAddress = IP;
       }
string Computer::AssignDescription(string description){
       Description = description;
       }


They should look more like this imho:

C++
void Computer::AssignCompName(string compname){
       ComputerName = compname;
       }
void Computer::AssignIP(string IP){
       IPAddress = IP;
       }
void Computer::AssignDescription(string description){
       Description = description;
}


Note that you'll need to change the return type to void in the declaration as well.

If your program behaves weirdly you should try to step through it with the debugger, it will let you execute each code line in a step-by-step kind of manner. Very useful.
As for your code, after making the changes I've suggested above it runs fine on my box, I'm using Visual Studio 2010, what compiler or IDE are you using to build you program?

/Fredrik
   
v2
Comments
Dalek Dave 28-Mar-11 11:40am
   
Good catch.
BigHeadedGuitarist 28-Mar-11 18:36pm
   
Hello thanks for the reply.

Since those methods are there just to set a private variable, what should I declare as the return value?

And no the compile doesn't freeze that was bad wording, the website/browser glitched and I had to type it all again and wrote it incorrectly.

My code compiles, and seems successful, but when I run the application it appears for a brief moment then crashes. It does not record any error though, it simply disappears. That probably doesn't count as a crash.

Thanks again.
Fredrik Bornander 29-Mar-11 2:42am
   
I've updated my answer with new suggestions.
BigHeadedGuitarist 29-Mar-11 4:28am
   
Aah, so simple, I tried that before you made the second post, but forgot to change the declaration.

Brilliant, and I also forgot to write I am using Dev-C++.

Does VS 2010 use the same standard header files or does it have its own for C++?

I remember reading a tutorial then looking at VS Express 2008, and the header files were all different, which led me to use Dev C++.

Thanks for the answer, it works now that I updated the declaration.
Fredrik Bornander 29-Mar-11 4:37am
   
I'm glad it worked out, VS C++ is standard compliant, so you can use it with the same header files.
Accept the answer if it fixed your issue so that the question can be closed.
BigHeadedGuitarist 29-Mar-11 5:40am
   
Thanks, and will do.

Thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900