Using getline properly

The following code works for half the time when getting user input. However, sometimes if the user presses enter without entering anything, the program crashes. Sometimes, after typing a short command, and then a longer one the next time, the program again crashes. Other times the program crashes without pattern.

I have no idea what is causing this undefined behavior.


1
2
3
4
5
6
7
8
9
do{
		cout << endl << "What do I do next?" << endl;
		getline(cin,in2,'\n');
			//Convert all letters to lowercase for ease on the processor.
		for(int indx=0; indx<101; indx++){
			if(isupper(in2[indx]))	in2[indx]=tolower(in2[indx]);}
ProcCheckList(in2);
	}while(play);
          in2 = "";
Last edited on
for(int indx=0; indx<101; indx++){

try this
for(int indx=0; indx< in2.length(); indx++){
I implemented that, and I really thought that was the solution since I never noticed that...

But the problem still persists...

New code:
1
2
3
4
5
6
7
8
9
10
	do{
		cout << endl << "What do I do next?" << endl;
		getline(cin,in2,'\n');
			//Convert all letters to lowercase for ease on the processor.
		for(int indx=0; indx<in2.length(); indx++){
			if(isupper(in2[indx]))	in2[indx]=tolower(in2[indx]);}
		ProcCheckList(in2);
		in2 = "";
	}while(play);
Maybe there is a problem elsewhere in the program?
I'd like to think that, but I secluded this portion of code in main. I even commented out the call to "ProcCheckList."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <stdlib.h>
#include <conio.h>
#include <cctype>
//#include "redirect_V2.h"
	
using namespace std;


int main(int argc, char *argv[]){
	bool play=true;
	string in2;

//	Clean();
	cout << "Ugh... I feel so groggy. Where am I? I think I need some 'help' here..." << endl;

	do{
		cout << endl << "What do I do next?" << endl;
		getline(cin,in2,'\n');
			//Convert all letters to lowercase for ease on the processor.
		for(int indx=0; indx<in2.length(); indx++){
			if(isupper(in2[indx]))	in2[indx]=tolower(in2[indx]);}
//		ProcCheckList(in2);
	}while(play);
	
//	Clean();
	getch();
	return 0;
}


Oh, and please disregard my use of conio.h. This is an incomplete, old program I'm trying to fix up. I want to focus on this current problem first.
Last edited on
Taken from http://stackoverflow.com/questions/313970/stl-string-to-lower-case

Make sure you #include <algorithm>

21
22
23
24
25
//Convert all letters to lowercase for ease on the processor.
for(int indx=0; indx<in2.length(); indx++){
	if(isupper(in2[indx]))	in2[indx]=tolower(in2[indx]);}
transform(in2.begin(), in2.end(), in2.begin(), ::tolower);
//ProcCheckList(in2); 
http://ideone.com/XZJHET

Try cout << in2 << endl; to see if it works for you.
Last edited on
Wow, that worked like a charm. Thanks!

Though that does raise a question... What are the problems with "tolower" and "toupper?"
I don't know of any problems other than not being able to use a custom locale. If you scroll down that stackoverflow page there is a similar example that uses locales.
Topic archived. No new replies allowed.