C++ Segmentation Fault

Get help on programming - C++, Java, Delphi, etc.
Post Reply
DarkRanger
Registered User
Posts: 8346
Joined: 10 May 2006, 02:00
Processor: Intel i5-3750
Motherboard: Gigabyte
Graphics card: nVidia GTX 550Ti
Memory: 8GB Jetram
Contact:

C++ Segmentation Fault

Post by DarkRanger »

Hi guys,

Here is the code for my program:

Code: Select all

#include <iostream>
using namespace std;

struct vehicle
{
	string name;
	double chassis_id;
	string colour;
	int wheels;
};

void printVehicle(vehicle v)
{
	string type;
	if(v.wheels == 1)
		type = "vehicle is a unicycle.";
	else if(v.wheels == 2)
		type = "vehicle is a motorcycle.";
	else if(v.wheels == 3)
		type = "vehicle is a trike.";
	else if(v.wheels == 4)
		type = "vehicle is a car.";
	else if(v.wheels < 4)
		type = "vehicle is a truck.";
	cout<<v.name<<v.chassis_id<<" "+v.colour+" "<<type<<endl;
}

vehicle getVehicle()
{
	vehicle v;
	cout<<"Name: ";
	cin>>v.name;
	cout<<"Chassis ID: ";
	cin>>v.chassis_id;
	cout<<"Colour: ";
	cin>>v.colour;
	cout<<"Number of Wheels: ";
	cin>>v.wheels;
	printVehicle(v);
}

int main()
{
	getVehicle();
}
And this is the output:

Code: Select all

Name: BMW_M3
Chassis ID: 221141414
Colour: blue
Number of Wheels: 4
BMW_M32.21141e+08 bluevehicle is a car.
Segmentation fault
I honestly have no idea what a segmentation fault is or why it throws the error.

Can someone please help me?

Thanks

DR
Image
Richard_
Registered User
Posts: 2295
Joined: 18 May 2003, 02:00
Location: Durban, South Africa

Re: C++ Segmentation Fault

Post by Richard_ »

What happens when you add this to the main method:

Code: Select all

int main()
{
	getVehicle();
	return 0;
}
The fact that you're declaring main() to return an integer type, but aren't returning anything seems a bit suspect. The default case is to return 0 to the OS.
DarkRanger
Registered User
Posts: 8346
Joined: 10 May 2006, 02:00
Processor: Intel i5-3750
Motherboard: Gigabyte
Graphics card: nVidia GTX 550Ti
Memory: 8GB Jetram
Contact:

Re: C++ Segmentation Fault

Post by DarkRanger »

Same error.

It does nothing different and also doesn't output anything new.
Image
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Re: C++ Segmentation Fault

Post by rustypup »

you have a number of issues here, but the segmentation occurs because your getVehicle() method declares a return type of vehicle... as it stands, you return nothing...

also,

Code: Select all

if(v.wheels < 4)
      type = "vehicle is a truck.";
is not going to achieve what you want

within the same code block, we can break your logic by inputting any value greater than 4 or less than zero... always assume the user is an ape...

get to know enum as well as switch...

enum because it makes for readable code - enums are indexed from 0, (with each succeeding entry being 1+prior index), and will always return type int, so they can be used in switch statements..

Code: Select all

enum V_TYPE {UNICYCLE, BICYCLE, MOTORCYCLE, CAR, BUS, TRUCK, UNKNOWN}; 

switch(v.type){ //assuming type is of instance type V_TYPE...
    case UNICYCLE: 
            type = "unicycle";
            break;
    case BICYCLE:
            type = "bicycle";
            break;
    default: 
            type = "unknown";
            break;
}
switch, because chaining if/else statements beyond 3 levels is asking for bugs and broken logic...

while you may get away with omitting the return statment from your main() method, (most compilers will include it for you), it is good practise not to depend on the compiler and do so yourself. this habit is hard to break and should you ever ship your source for others to build, now you're relying on their compiler to perform the same magic...
Most people would sooner die than think; in fact, they do so - Bertrand Russel
DarkRanger
Registered User
Posts: 8346
Joined: 10 May 2006, 02:00
Processor: Intel i5-3750
Motherboard: Gigabyte
Graphics card: nVidia GTX 550Ti
Memory: 8GB Jetram
Contact:

Re: C++ Segmentation Fault

Post by DarkRanger »

that return vehicle statement fixed it.

What I did was:

Code: Select all

vehicle getVehicle()
{
   vehicle v;
   cout<<"Name: ";
   cin>>v.name;
   cout<<"Chassis ID: ";
   cin>>v.chassis_id;
   cout<<"Colour: ";
   cin>>v.colour;
   cout<<"Number of Wheels: ";
   cin>>v.wheels;
   return v;
}

int main()
{
   vehicle car = getVehicle();
   printVehicle(car);
   return 0;
}
Image
Richard_
Registered User
Posts: 2295
Joined: 18 May 2003, 02:00
Location: Durban, South Africa

Re: C++ Segmentation Fault

Post by Richard_ »

My bad, that was indeed the obvious mistake :P
DarkRanger
Registered User
Posts: 8346
Joined: 10 May 2006, 02:00
Processor: Intel i5-3750
Motherboard: Gigabyte
Graphics card: nVidia GTX 550Ti
Memory: 8GB Jetram
Contact:

Re: C++ Segmentation Fault

Post by DarkRanger »

Richard_ wrote:My bad, that was indeed the obvious mistake :P
+1 haha...
Image
Post Reply