954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

string problem

do{
			
	chatLike("Enter student's full name: ");
	getline(cin, name, '\n');

        //I have my try-catch here

	}while(error==true);


chatLike is a function that i made that produces the cout character per character.
program immediately crashes when input is:

a)input is space only or a single space
b)input ends with one or multiple spaces

error is "debug assertion failed (then a path here pointing to xstring line:1441) string subscript out of range".

i've been googling for 2 days now it's getting frustrating. been trying to find a way to try and manipulate the input before storing it in name in a way that it will not accept case a and b. :-/ but i also thing that would not work.

prog_newb
Newbie Poster
3 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Can you post more of your code? There is really not enough here to troubleshoot your issue. Also, when exactly do you get the assertion failure? After you type some characters and press [ENTER]? Before that?

hag++
Junior Poster
197 posts since Jan 2010
Reputation Points: 34
Solved Threads: 31
 

sorry took some time to reply. i made the try catch a function. and yes, immediately crashes after pressing enter(provided that you did input either the case a or b)

do{
			
			chatLike("Enter student's full name: ");
			getline(cin, name, '\n');

			error=nameChecker(name);			
	
			}while(error==true);


here's the function

bool nameChecker(string str)
{
	int periodCtr=0, spaceCtr=0,alphaCtr=0,length;
	bool error=false;

	
	try{
			length=str.length();
			if(length<5)//name must atleast be 5 something like: su ly (chinese name and stuff)
			{
				throw "\aInvalid Name: too short!\n";
			}

			else
			{
				for(int x=0 ; x<length ; x++)
				{
					if(isalpha(str[x]))//checks if x'th entry is in the alphabet
					{
						alphaCtr++;//counts the number of alphabet characters
							if(alphaCtr<4 && x+1==length)//forbids the numer of characters to be less that 4(i think chinese names would be the only ones with 4 characters)
							{
								throw "\aInvalid Name: must be atleast 4 alphabet characters!\n";
							}
						continue;
					}
					switch(str[x])
					{
						case '.' :
						{
							periodCtr++;//counts the periods
								if(x+1!=length)//checks if the x'th member is the last member of the string
								{
									if(str[x+1]=='.')//if not checks if the next members is another period
										{
											throw "\aInvalid Name: consecutive periods!\n";
										}
								}
								if(periodCtr>2)//period cannot be more than 2 for as far as i know middle initials usually contains up to 2 periods
									{
										throw "\aInvalid Name: too many periods!\n";
									}
								if(periodCtr=0 && x+1==length)//checks if there is 0 occurances of periods in the string
								{
									throw "\aInvalid Name: atleast 1 period!\n";
								}
							break;
						}

						case ' ':
						{
							spaceCtr++;//counts the spaces
								if(x+1!=length)//checks if the x'th member is the last string member
								{
									if(str[x+1]==' ')//if not checks if the next member is also a space
										{
											throw "\aInvalid Name: consecutive spaces!\n";
										}
								}
							
								if(spaceCtr=0 && x+1==length)//counts the occurances of spaces in the string, must be atleast 1
									{
										throw "\aInvalid Name: atleast 1 space!\n";
									}
							break;
						}

						default ://symbols and numbers f
						{
							throw "\aInvalid Name: alphabet characters only!\n";
							break;
						}
					}

				}
			}
	}

	catch(string err)
	{
		chatLike(err);
		error=true;
	}
			
	
	return error;		
}
prog_newb
Newbie Poster
3 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

anyone here? :(

prog_newb
Newbie Poster
3 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 
anyone here?


Please have some patience, the person who ultimately answers you question may be sleeping now.

Have you run this function through a debugger to see exactly when it crashes?

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: