Solved

Reading in an character array from user, segmentation error?

Posted on 2003-11-24
4
601 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 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');
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

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

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…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

688 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