Solved

Reading in an character array from user, segmentation error?

Posted on 2003-11-24
4
596 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
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
n_fortynine earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hey thanks for the help.  I finally figured it out.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now