7 Replies - 382 Views - Last Post: 19 November 2012 - 11:12 AM Rate Topic: -----

#1 techdreamer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 15-August 12

getting an error of segmentation fault(code dump)

Posted 19 November 2012 - 08:21 AM

The following programme i just need to read from a file about choclate information for a shop. Then if i want to update the stocks i have to do it in a programme. But when I compile it comes segmentation fault(code dump).
1. I need to know why i got this error.
2. I have 20 choclate stock information. I don't know how to read and update the information.
It would be grat pleasure if you can help me for this programme.

Here is the code:
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

struct Supplier
{
	char sname[50];
	char saddress[50];
	char sphone[50];
};

struct Chocolate
{
	char cname[100];
	char ctype[100];
	char qty[100];
	char ppp[100];
	Supplier supply;
}choco[20];

int main()
{
	int num;
	ifstream infile;
	infile.open("choc.txt");

	
    if(!infile.eof())
	{
		infile>>num;
			for(int i=0; i<num; i++)
			{
				
				infile.getline(choco[i].cname,50,'\n');
				infile.ignore();
				infile.getline(choco[i].ctype,50,'\n');
				infile.ignore();
				infile.getline(choco[i].qty,50,'\n');
				infile.ignore();
				infile.getline(choco[i].ppp,50,'\n');
				infile.ignore();
				infile.getline(choco[i].supply.sname,50,'\n');
				infile.ignore();
				infile.getline(choco[i].supply.saddress,50,'\n');
				infile.ignore();
			

				cout<<choco[i].cname<<endl;
				cout<<choco[i].ctype<<endl;
				cout<<choco[i].qty<<endl;
				cout<<choco[i].ppp<<endl;
				cout<<choco[i].supply.sname<<endl;
				cout<<choco[i].supply.saddress<<endl;
				cout<<choco[i].supply.sphone<<endl;
			}
			
	}
	
			
			
	return 0;
}

This post has been edited by JackOfAllTrades: 19 November 2012 - 08:35 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: getting an error of segmentation fault(code dump)

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: getting an error of segmentation fault(code dump)

Posted 19 November 2012 - 08:36 AM

Come ON! You've been around here long enough now to know to use CODE TAGS!!!

:code:

Quote

when I compile it comes segmentation fault(code dump)


When you COMPILE it, it dumps core, or when you RUN it?
Was This Post Helpful? 0
  • +
  • -

#3 techdreamer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 15-August 12

Re: getting an error of segmentation fault(code dump)

Posted 19 November 2012 - 08:48 AM

View PostJackOfAllTrades, on 19 November 2012 - 08:36 AM, said:

Come ON! You've been around here long enough now to know to use CODE TAGS!!!

:code:/>

Quote

when I compile it comes segmentation fault(code dump)


When you COMPILE it, it dumps core, or when you RUN it?

When I RUN the programme
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,564
  • Joined: 25-December 09

Re: getting an error of segmentation fault(code dump)

Posted 19 November 2012 - 08:57 AM

Please post the contents of your input file.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 techdreamer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 15-August 12

Re: getting an error of segmentation fault(code dump)

Posted 19 November 2012 - 09:20 AM

View Postjimblumberg, on 19 November 2012 - 08:57 AM, said:

Please post the contents of your input file.

Jim

2
Hershey
White chocolate
100
5.30
Alderfer
123 Wrong Street Selangor
012-3765567
Van Houten
Dark Chocolate with Almond
50
4.80
New Supplier
19 Petaling Street Kuala Lumpur
019-6783345

(If it is 3 choclate means instead of two we have to write 3 and wite 3 details. In this file i wrote 2)

View Posttechdreamer, on 19 November 2012 - 09:19 AM, said:

View Postjimblumberg, on 19 November 2012 - 08:57 AM, said:

Please post the contents of your input file.

Jim

2
Hershey
White chocolate
100
5.30
Alderfer
123 Wrong Street Selangor
012-3765567
Van Houten
Dark Chocolate with Almond
50
4.80
New Supplier
19 Petaling Street Kuala Lumpur
019-6783345

(If it is 3 choclate means instead of two we have to write 3 and wite 3 details. In this file i wrote 2)

Each chocolate’s information should be kept in a struct containing the following information:

– chocolate name
– description
– quantity
– price per piece
– supplier information (kept in another struct containing name, address, and contact number)
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,564
  • Joined: 25-December 09

Re: getting an error of segmentation fault(code dump)

Posted 19 November 2012 - 09:55 AM

You need to check that your file opened correctly. Your program runs successfully for me, no segmentation fault.


Jim
Was This Post Helpful? 1
  • +
  • -

#7 techdreamer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 15-August 12

Re: getting an error of segmentation fault(code dump)

Posted 19 November 2012 - 10:06 AM

View Postjimblumberg, on 19 November 2012 - 09:55 AM, said:

You need to check that your file opened correctly. Your program runs successfully for me, no segmentation fault.


Jim

What do u mean by open correctly.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,564
  • Joined: 25-December 09

Re: getting an error of segmentation fault(code dump)

Posted 19 November 2012 - 11:12 AM

You are trying to open a file for input. If the file doesn't exist then this opening fails. You need to insure that your file opened correctly.

ifstream fin("YourDataFile");
// Check to insure the file opened correctly.
if(!fin)
{
   // The file failed to open.
   // Do something.
}


Jim

This post has been edited by jimblumberg: 19 November 2012 - 11:12 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1