GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Aug-2008, 01:54
vagandhi vagandhi is offline
New Member
 
Join Date: Aug 2008
Posts: 1
vagandhi is an unknown quantity at this point

Help me to solve my problem in C++


I am having problem while running below program.

If anyone can help me.

CPP / C++ / C Code:
// create class car
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
// class declaration
class car
{
//data member
private:
char *c_nm;
char *color;

int model;
float price;

//member function
public:
void getcar(int n,char *nm,float p)
{
model=n;
c_nm=nm;
color=c;
price=p;
}
void putcar()
{
cout<<endl<<"model :="<<model;
cout<<endl<<"name :="<<c_nm;
cout<<endl<<"color :="<<color;
cout<<endl<<"price :="<<price;
}
};
void main()
{

int i;
char *nm1;
char *c1;
float p1;
clrscr();

car c11; //object of class
cout<<"enter model:=";
cin>>i;
cout<<"enter name:=";
cin>>nm1;

cout<<"enter color:=";
cin>>c1;
cout<<"enter price:=";
cin>>p1;

c11.getcar(i,nm1,p1);
c11.putcar();
getch();
}
Last edited by admin : 04-Aug-2008 at 04:09. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 04-Aug-2008, 02:55
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: help me to solve my problem in C++


Quote:
Originally Posted by vagandhi
I am having problem while running below program.
Numerous comments:
  • When posting code, use the [cpp] tag before & [/cpp] afterwards as this will both preserve indentation & highlight keywords. Given that you are asking others to debug your code, the least you can do is make the code as readable as possible.
  • Providing an explanation of what you intend to achieve with your application is never a bad thing. We can't read your mind.
  • A glaring problem with your code is that you are attempting into read in a string without allocating any memory for it. ie:
    CPP / C++ / C Code:
    char *nm1;
    ...
    cin>>nm1;
    You are attempting to read data to an uninitialized address. My guess is that you are either getting a segmentation fault upon execution or some other application crash. At a bare minimum, allocate memory before trying to write data into it. eg.
    CPP / C++ / C Code:
    char nm1[40];
    ...
    cin >> nm1;
  • The following member function:
    CPP / C++ / C Code:
    void getcar(int n,char *nm,float p)
    {
    model=n;
    c_nm=nm;
    color=c;
    price=p;
    }
    ...is also horribly wrong as you are just copying whatever address is passed about in nm to the member variable c_nm. Only if you are reading into nm1 back in main() once will this work. You should declare space for c_nm in a similar manner as already discussed, & use some string operation such as strcpy() to copy from one string into the other.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help me to solve C++ problem fahad C++ Forum 1 01-Nov-2007 05:43
Problem Description: Graph’s Degree.....can help me to solve this out? thefinalyy C++ Forum 0 24-Aug-2007 06:42
I have a problem to solve summation in c++ logieen C++ Forum 4 22-May-2007 08:25
A problem using streams. (I've spent a month trying to solve it) Mararia C++ Forum 8 14-Dec-2006 17:52
Please help to solve me a problem with multiline texrbox and checkbox mithila MS Visual C++ / MFC Forum 0 08-Sep-2004 23:08

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:12.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.