Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7308475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:33:45+00:00 2026-05-28T23:33:45+00:00

Hi this is a very simple piece of code I just write for C++

  • 0

Hi this is a very simple piece of code I just write for C++ file i/o practice. But I got a Segmentation Fault (core dumped) exception when I am running this.
Here is my code below:

# include <iostream>
# include <string>
# include <cmath>
# include <fstream>
using namespace std;

int main()
{
double num, rad, i, angle, x, y;
char * filename;
ofstream file;
// prompt  ask for number
cout << "Enter the number of sample points: ";
cin >> num;

// prompt for circle radius.
cout << "Enter the circle radius: ";
cin >> rad;

// prompt for output file name.
cout << "Enter the output filename: ";
cin >> filename;

file.open (filename, fstream :: in | fstream :: trunc);
if(!file.is_open())
{
    cout << "Error opening file " << filename << endl;
    cout << "Exiting..." << endl;
    return 0;
}
for(i = 1; i <= num; i ++)
{
    //angle = 2 * M_PI * (i/num);
    //x = rad * cos(angle);
    //y = rad * sin(angle);
    //file << "\t" << x << "\t" << y << endl;
    file << "this is " << i << endl;
}
cout << "finished";
file.close();
return 0;
}

I am not sure where the problem is, but the error message “seg fault (core dumped)”shows up after Enter the output file name.

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:33:45+00:00Added an answer on May 28, 2026 at 11:33 pm

    cin >> filename will be undefined behaviour, because filename is an uninitialized pointer.

    If you want to store characters, you need to allocate space for them. So you could do:

    char filename[150] = {0};
    cin >> filename; // OK, you provide space for 149 characters. Will still break
                     // if more characters are provided by the user.
    

    or:

    #include <string>
    std::string filename; // overloads operators >> and << with streams
                          // automatically performs memory management
    // std::cin >> filename; /* Would stop at first space */
    std::getline(std::cin, filename); // better: will stop at any carriage return
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to compile this very simple piece of code class myList {
I was making this very simple lex program (just an introductory program). But on
I have a very simple piece of code (just for testing): import smtplib import
I have this very simple piece of code; #include <deque> #include <vector> using namespace
This very simple code gives me tons of errors: #include <iostream> #include <string> int
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout
I've written this very simple function to replace a file extension using LINQ in
I just want to design this very simple website. Basically there are multiple pages
This should be simple, very simple, but I'm having a hard time with it.
This is likely a very simple question with a straightforward answer but I'm something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.