Question

I'm getting a segmentation fault for this function, this is for a c++ program. Is there...

I'm getting a segmentation fault for this function, this is for a c++ program. Is there anything that you see that is obviously wrong? Am i using cin.get wrong? Please help if you can.

number is my variable, and NUMBER is the const int limit

void questions::addQuestion()
{
   int i = 0;

   cout << "Enter the question number: " << endl; //after this line is where it seg faults
   cin.get(number,NUMBER, '\n');
   cin.ignore(100, '\n');

   cout << "Enter the subject of the question: " << endl;
   cin.get(subject, strlen(subject), '\n');
   cin.ignore(100, '\n');
   for(int i = 0; i < strlen(subject); ++i)
       subject[i] = toupper(subject[i]);

   cout <<"Enter the text of the question: " << endl;
   cin.get(text,TEXT);
   cin.ignore(100, '\n');

   cout <<"Enter the date of completion: " << endl;
   cin.get(date, DATE);
   cin.ignore(100, '\n');
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Your doing wrong at the declaration of the variable therefore the compiler is not able to access your memory location and returns the seg fault.

As you have not passed any variable named number to addQuestion function and it has not been declared locally in the addQuestion function.

Therefore the problem lies in the declaration of the varialble and I suggest you to delcare all the varialbes in the function locally

as you know that cin.get takes string variable and you have to mention the size of the variable which you are taking at the runtime.correct usage of the cin.get(char *buffer, int length,'\n');

As you have mentioned the line where you are getting the seg fault i have modified upto that and I suggest you to please declare all the variable before their usage.(A small mistake every programmer does not declaring the variables).

code correction as per your mentioned line

#include <iostream>

using namespace std;

//Global declaration for NUMBER size

class questions{

public:

char number[100];//THe problem might lie here
void addQuestion();

};

void questions::addQuestion()
{
int i = 0;
const int NUMBER = 50;
cout << "Enter the question number: " << endl; //after this line is where it seg faults
cin.get(number,NUMBER, '\n');
cout<<number;
}


int main()
{
questions Q;//Object declaration for the questions class named Q
Q.addQuestion();//Calling the function addQuestion member function
return 0;
}

You might not variables for the class for question class .
Above i have shown how to declare the variable in the class.

follow as per above you might execute your code.

DeSKLUP #include <iostream> using namespace std; class questions{ public: char number[100]; void addQuestion(); void question

dsfsumanth@sumanth-Lenovo-ideapad-Y700-Touch-15ISK:-/Desktop$ g++ CPP.CPP sumanth@sumanth-Lenovo-ideapad-Y700-Touch-15ISK:-/D

Add a comment
Know the answer?
Add Answer to:
I'm getting a segmentation fault for this function, this is for a c++ program. Is there...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is for a C++ program: I'm almost done with this program, I just need to...

    This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...

  • (Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the...

    (Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the date of birth.            **fall.sort(compare_DOB); //Here lies your error** #include <fstream> #include <iostream> #include <string> #include <list> #include <cctype> using namespace std; const char THIS_TERM[] = "fall.txt"; const string HEADER = "\nWhat do you want to do ?\n\n" "\t. Add a new friend name and DOB ? \n" "\t. Edit/Erase a friend record ? \n" "\t. Sort a Friend's record ? \n"...

  • When I run this code use ./main How to fix it when program reminds segmentation fault...

    When I run this code use ./main How to fix it when program reminds segmentation fault (core dumped)? #include <iostream> void set_num(int* i_prt, int num) { *i_prt = num; } int main(int argc, char** argv) { int x; set_num(&x, 13); std::cout << "x: " << x << std::endl; int* y; set_num(y, 4); std::cout << "*y:" << *y << std::endl; }

  • Hello, I am trying to write this program and have received a "Segmentation Fault" error but...

    Hello, I am trying to write this program and have received a "Segmentation Fault" error but cannot seem to figure out where. I haven't been able to find it and have been looking for quite a while. The goal is to basically create a version of Conway's Game of Life. I am able to generate a table if the input values for rows and columns are equal, but if they are not I receive a segmentation fault and cannot continue...

  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • The following program is in c++ ; I am trying to read in games from a...

    The following program is in c++ ; I am trying to read in games from a file and when doing so I am only reading in parts of the file. It is giving me a segmentation fault error I am going to post my code below if anyone is able to help me debug the program I would greatly appreciate it. The program is too large to be submitted as a single question on here :( --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void Business::addGamesFromFile() {...

  • Write a C++ program that uses a structure to store the following inventory information in a...

    Write a C++ program that uses a structure to store the following inventory information in a file: ⦁   Item description, Quantity on hand, Wholesale cost, Retail cost, and Date added to inventory. ⦁   Use a char array for item description and date. ⦁   The program should have a menu that allows the user to perform the following tasks: i.   Add a new record at the end of the file. ii.   Display any record in the file. iii.   Change any record...

  • Can you fix this program and run an output for me. I'm using C++ #include using...

    Can you fix this program and run an output for me. I'm using C++ #include using namespace std; //function to calculate number of unique digit in a number and retun it int countUniqueDigit(int input) {    int uniqueDigitCount = 0;    int storeDigit = 0;    int digit = 0;    while (input > 0) {        digit = 1 << (input % 10);        if (!(storeDigit & digit)) {            storeDigit |= digit;       ...

  • Overload the output stream operator << and the assignment operator =. Any time cout << task;...

    Overload the output stream operator << and the assignment operator =. Any time cout << task; is used the code should output a task, any time task1 = task2 is copied there must be a deep copy if there are pointers. Use the operators in the following code as suggested above. DO NOT simply overload them! Do so on the following code: //Project 4 Main function #include "functions.h" #include <stdlib.h> #include <stdio.h> //main int main(){    TaskList column("tasks.txt");    char...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT