Segmentation fault reading string on cin read

AI Thread Summary
A segmentation fault occurs when a program attempts to access memory that it shouldn't, often due to indexing an array out of bounds. The user encountered this error while trying to read a string from cin, specifically at the "cin >> strWord" statement. Suggestions include checking loop definitions to ensure they stay within string bounds and using a debugger to identify the issue. It's also noted that providing unexpected input, like a single word or no input, can lead to unpredictable results. Properly managing input and array indexing is crucial to avoid segmentation faults.
OSalcido
Messages
66
Reaction score
0

Homework Statement


I'm trying to read a string from cin. When it hits the "cin >> strWord" statement, I get the following error message: Segementation fault (core dumped). The input file is named "data4nine". I'm really not sure what a segmentation fault is and why I'm getting it


The Attempt at a Solution


Code:
	while (!cin.eof())
	{
		cin >> strWord;
		intWords++;
		cntVowsCons(strWord, intVowels, intConsonants);
		strFormatted = FormatWord(strWord);
		lenWords += strWord.length();
		lenFormatted += strFormatted.length();


		if(strFormatted.length() % 2 != 0) 
			SwapMidChar(strFormatted);
		if(istenth(intWords, tenth))
			cout << strFormatted << endl;
		else
			cout << strFormatted;
		
	}
 
Physics news on Phys.org
Here are my files
 

Attachments

Segmentation fault is generally a memory access violation. In this case, your program will attempt to access part of a string that doesn't exist (I know where this is in your code, but it should be left as an exercise to you). You may attempt to find it by scrutinizing your code or by using a debugger.
 
Hi OSalcido! :smile:

When an access violation (aka segmentation violation) occurs in a program, it may or may not crash with a core dump.
And if it does crash, it's often not a the point where the access violation occurred.

In any program an access violation always occurs in one of 2 forms.

Either you follow a pointer that does not point anywhere.
Since you're not using pointers, that won't be a problem.

Or you are indexing an array outside of its bounds.
Each of your for-loops should be defined such that you can always be sure your string is indexed inside its bounds.
Can you check if that is the case?
 
Yes! Thank you guys I did check the loops and fixed the issues. Thanks a bunch
 
Your welcome. ;)

Btw, if you use an input of just one word and a newline, or none at all, you may get some results you don't expect.
 
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

Replies
6
Views
3K
Replies
13
Views
2K
Replies
10
Views
3K
Replies
8
Views
4K
Replies
2
Views
2K
Back
Top