Reading in an character array from user, segmentation error?

Posted on 2003-11-24
Last Modified: 2006-11-17
For class we've been assigned to take a file of football players and read them into an array or structures.  Everything is read in correctly, and we are to give the user the option to search through the array of structures to find a player, that is if they are in the list.  

Here's what I have so far.
When this code runs, it says-
Enter the exact name to search for: Segmention Fault
and exits.
Ive been using g++.
int searchName(qbInfo qbs[], int numQBs, int x)
int index = 0;
bool found = false;
char userinput[23];
int count = 0;
char ch;

cout << "Enter the exact name to search for: ";
cin.get(ch); //this is where the error is coming in
while(ch!= '\n') // read in until user hits enter
userinput[count]= ch;
cin.get(ch); //going to cause the error again?
What can I do to read in the entire name, Ive tried cin >>userinput; but that is only going to read up until say the name is "Trent Green", it will only read in "Trent" and hit the space and stop.

Question by:Jhoe-Oendus
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Accepted Solution

n_fortynine earned 125 total points
ID: 9814855
Jhoe-Oendus, >> will ignore spaces and end of lines as its default behavior. Looking at your function I see no logical error (are you missing a } to end your function?).
You might want to look into the use of getline.
cin.getline(userinput, 23, '\n');

Author Comment

ID: 9815015
Well I tried that, and noticed that's not what I wanted to do, I tried switching it over to a string and using cin on that, and Im still getting the Segmentation Fault.  And I checked for missing } and everything seems to be closed.  Any other suggestions?

This was the new code with the string implemented.
int index = 0;
bool found = false;
string userinputx;
char userinput[23];
int count = 0;
char ch;

 cout << "Enter the exact name to search for: ";
 cin >> userinputx;
 cout << "You entered ";
 cout << userinputx;


Expert Comment

ID: 9815549
can you post the entire file here? Since from what I see I can't tell that any of these could be giving you a segmentation fault error.

Author Comment

ID: 9815819
Hey thanks for the help.  I finally figured it out.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
thread-safe code in c++ 2 122
computer science syllabus 3 100
Issues with C++ Class 19 107
max float value 3 59
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question