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

  • SEARCH
  • Home
  • 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 7783819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:57:10+00:00 2026-06-01T19:57:10+00:00

As a c++ beginner I’ve written the following code: int main(void){ struct car{ char

  • 0

As a c++ beginner I’ve written the following code:

int main(void){
struct car{
    char * make[200];
    int manfYear;
};

int num=0;

cout << "How many cars do you wish to catalogue? ";
cin >> num;
car * Cars = new car [num]; 

for (int i=1;i<=num;i++){
    cout << "Car #" << i << ":" << endl << "Please enter the make: ";
    cin.getline(*Cars->make,200);
    cout << "Please enter the year made: ";
    cin >> Cars->manfYear;
}

My problem is that I can’t get my head round a problem where I get a segfault when running the program at the point I need to enter the model of the car. Can someone please explain what I’m doing wrong?

As far as I understand it I’m passing a pointer to the array “make” which should make it work. Is my understanding way off?

Thanks in advance
Dan

  • 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-06-01T19:57:12+00:00Added an answer on June 1, 2026 at 7:57 pm

    Four issues I see right away:

    Issue 1

    In your struct, you have:

    char * make[200];

    In English, this is saying, “create an array of 200 pointers to character”, when I think you want to say, “create an array of 200 characters.” So you should have instead:

    char make[200].

    Issue 2

    You are looping by starting at 1. This will skip the first car in the array – remember arrays are zero-indexed. So you should have instead:

    for (int i = 0 ; i < num ; i++)

    and for display purposes, you could say:

    cout << "Car #" << (i+1) << ":" << endl << "Please enter the make: ";

    Issue 3

    Where you say:

    cin.getline(*Cars->make,200);

    and

    cin >> Cars->manfYear;

    Where in these lines are you specifying which car the user is populating? Nowhere. If you are looping with i, then you need to actually mention i. These should work:

    cin.getline(Cars[i].make,200);

    and

    cin >> Cars[i].manfYear;

    Notice that we are using ., not ->. This is because the items in the Cars array are actual instances, not pointers. The Cars array is itself a pointer, but not its contents.

    Issue 4

    All credit to @Ben C who pointed this out first: mixing the >> operator with getline() function on cin can lead to strange behavior, with leftover CR‘s from >> going into the getline() call. You could use either all >> (disadvantage: you don’t have the 200 limit enforced when reading the make) or all cin.getline() (disadvantage: you will have to use string buffers and then convert them for number of cars and year), or put cin.ignore() after each invocation of >>, like so:

    cin >> num;
    cin.ignore();
    

    and

    cin >> Cars[i].manfYear;
    cin.ignore();
    

    Again, all credit to @Ben C for noticing this first.

    Last But Not Least

    By convention, classes/structs have capital names, and variables have lowercase / camelcase names. Consider renaming the struct from car to Car, and the array from Cars to cars. In other words, the reverse of the capitalization you have right now.

    Finally, I concur with all the other posters here: you should consider using string instead of char arrays.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

iOS beginner here. I'm using the following code to save my facebook accessToken and
another beginner problem. Why isn't the following code with an asp.net page not working?
A beginner's question, probably a trivial one. Here's the XUL code snippet: <window xmlns=http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul>
Beginner question here. I'm going to make a Jquery function that is used to
Im beginner in android development. I ve just created a button in main.xml file
A beginner question: I don't know how best to structure this bit of code,
Python beginner here. I am using the matplotlib library to make graphs from tab
Beginner android question. Ok, I've successfully written files. E.g. // get the file name
Beginner question. How do I substitute: $_SESSION['personID'] for {personID} in the following: public static
Beginner question: I want to debug some code that compiles. I pressed debug, it

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.