?
Solved

Reading in an character array from user, segmentation error?

Posted on 2003-11-24
4
Medium Priority
?
602 Views
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;
count++;
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.

Thanks
0
Comment
Question by:Jhoe-Oendus
[X]
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
4 Comments
 
LVL 4

Accepted Solution

by:
n_fortynine earned 500 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');
0
 

Author Comment

by:Jhoe-Oendus
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;

Thanks
0
 
LVL 4

Expert Comment

by:n_fortynine
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.
0
 

Author Comment

by:Jhoe-Oendus
ID: 9815819
Hey thanks for the help.  I finally figured it out.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

801 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