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"
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
}
elseif (opType == "*")
{
a_big_int1.multiply(a_big_int2); //invokes mutiplication if * sign is read
}
}
}
cout << endl;
return 0;
}
//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
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:
#ifndef BIGINT_H
#define BIGINT_H
#include <string>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <iostream>
usingnamespace 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
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