spent hours and still do not know why!? sEGMENTATION FAULT

hey guys im write a program it has a class called BigInt which is on a separate header file. this program is far from finished but before i decided to go on with it any further, i just wanted to test the code. it was working well until i kept getting "segmentation fault" at this part. i've tried to put some really simple things like cout << "wee"; just to test it and i STILL GET SEGMENTATION FAULT, im so frustrated and do not why i keep getting it. heres my main code"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

int main()
{
	string filename;	//creates string called filename which will take the name of the file to be read from user input
	cout << "enter the full name of the file you wish the program to read from.\n";
	cout << "i.e myfile.txt or game.txt, enter now: \n";
	cin >> filename; //gets file name from user
	
	//creates an ifstream object that reads from the file name specified by the user
	ifstream inputname (filename.c_str());	
	
	//if the file name does not exist or access is denied, the program exists
	if (!inputname)
	{
		cout << "Error: no such file name or i cannot read from the file" << endl;
		cout << "this program will now terminate...\n";
		exit(1);
	}
		
	string opType, op1, op2; //create 3 string variables representing the operator and operands
	cout << "-------------------------------" << endl;
	cout << "Results from operations:" <<endl;
	cout << "-------------------------------" << endl;
	
	/*while filename can be read, the program reads the type of operation it 
		needs to perform and the two operands, then assigns their values to
	  opType, op1, and op2 respectively*/	
	while (inputname >> opType >> op1 >> op2) //might not work if file has more than 3 strings!!!
	{	 		
		//operations checker: if no multiplication or addition was assigned to opType, program informs user		
		if (opType != "+" && opType != "*")
		{
			cout<<"invalid operation on this line! i only perform multiplication or addition" << endl;			
		}
		
		else 
		{
			//creates two instances of BigInt, converting values contained in op1 and op2 into values of type BigInt
			BigInt a_big_int1(op1);
			BigInt a_big_int2(op2);				
			if (opType == "+")
			{							
				a_big_int1.plus(a_big_int2); //invokes addition if + sign is read	
			}	
			else if (opType == "*")
			{ 	
				a_big_int1.multiply(a_big_int2); //invokes mutiplication if * sign is read
			}			
		}			
		
	}		
	cout << endl;
	return 0;
}



and here is the implementation for the class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//constructor that takes the operand passed through it and assigns it to the string variable value WORKING!
BigInt::BigInt(string operand)
{
	value = operand;
}

string BigInt::multiply(BigInt passedv2)
{
	cout << "weee" << endl;
}

string BigInt::plus(BigInt passedv1)
{	
	cout << "plus" << endl;	
}

//outputs the value of a variable of type BigInt WORKING!
void BigInt::output()
{
	cout << value << endl;
}

//returns value of BigInt variable WORKING!
string BigInt::getValue()
{
	return value;
}


AND I KEEP GETTING THIS WHEN I READ FROM THE TEXT FILE !

-------------------------------
Results from operations:
-------------------------------
plus
Segmentation fault


PLEASE HELP ME
Where's the header file for BigInt, and is that _all_ the functions in BigInt?
If I had to venture a guess, I'd say there's a problem either with the constructor or the destructor.
the header file for big int is in another place, yes so far that is all the functions. like i say i am still developing it. heres the header file i commented out some functions because i was just testing them. @ helios, there should not be another wrong with myconstructor, it's just setting the value. i've even tested the constructor, it works. anyways here is the header please help me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef BIGINT_H
#define BIGINT_H
#include <string>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <iostream>

using namespace std;

/* declares a class called big int which allows for the addition and multiplication arbitrarily large numbers */

class BigInt
{
	private:	
	string value; //the value of the large numbers(operands) converted to a string
	string result; //result of operation 
	
	
	public:
	BigInt(string operand); //a constructor take the string containing the number of the operand, and stores it to value
	void output(); //prints out value of variable	
	string multiply(BigInt passedv2); //function for mutiplying one big int to another
	string plus(BigInt passedv1); //function for adding one big int to another	
	string getValue();
	
	
};
		
	
#endif 
Last edited on
ok i think i found out why, but im not 100% sure, i have to test it further. it has something to do with me declaring an function or type string, as is with the multiply and plus functions in my program. i guess you make a function with return type string. i've tested a string function by writing simple code and that gave me problems. so i guess the declaring the function type string, which does not work so that means im just trying to acess memory not allocated to my program

again im not 100% sure, can anyone confirm this?
Topic archived. No new replies allowed.