Solved

Reading in an character array from user, segmentation error?

Posted on 2003-11-24
4
597 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Writing a parser for java language 4 71
FMX StringGrid1->Canvas->FillRect Problem 3 128
thread-safe code in c++ 2 84
Installshield for Embarcadero EX 10.1 Berlin 4 20
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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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…

895 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

15 Experts available now in Live!

Get 1:1 Help Now