Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] C++, Abnormal Termination Error

Status
Not open for further replies.

_Treant

Newbie level 1
Newbie level 1
Joined
Oct 10, 2011
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,289
I got this code to find the nth character of a string:


#include <iostream>
#include <string>
using namespace std;

int main ()
{
char ans;
repeat:

string str;
char c;
int n;

cout<<"input: & number";
getline(cin,str);
cin>>n;

c = str.at(n-1);

cout<<"char = "<<c;

n=n <= 0 && n < str.length();



{

cout << "Do you want to try again(y/n)? ";
cin>>ans;

if (ans == 'y')
goto repeat;

else if (ans == 'n')
exit(1);
else
cout<<"INVALID CHOICE"<<endl;

}

return 0;
}

The program's working fine on the first try but when you enter y to try again, input the string, it shows the "Abnormal Program Termination" error. Please help...
 

First of all this is the worst code I have seen in my life, In C++ never use goto as they create a lot of problems and debugging them will be quite difficult, but in this case the problem is with formatting the console input stream...but you also have some other errors...

Code:
char ans;
repeat:
string str;
char c;
int n;
In this code when goto jumps to repeat the str,c and n variables are redefined you just define them once

Code:
getline(cin,str);
Here is your main problem, when using getline with cin you have to remove any white space from the stream.

Code:
n=n <= 0 && n < str.length();
What for this is used in the code, I have no idea i.e.. not needed as per the logic of the program goes

Here is your code corrected
Code:
#include <iostream>
#include <string>
using namespace std;

int main()
{

char ans;
string str;
char c;
int n;

repeat:

cout<<"input: & number\n";
cin>>ws;// stream out any whitespace 
getline(cin,str);
cin>>n;
cout<<str<<endl;
c = str.at(n-1);

cout<<"char = "<<c;

cout << "Do you want to try again(y/n)? ";
cin>>ans;

if (ans == 'y')
goto repeat; 

else if (ans == 'n')
exit(1);
else
cout<<"INVALID CHOICE"<<endl;

return 0;
}

And a bit more good way i.e.. in proper C++ style. Its identical to the above logic
Code:
#include <iostream>
#include <string>
using namespace std;

int main()
{
char ans = 0;
string str = "";
char c = 0;
int n = 0;


while(1)
{
cout<<"Input String : ";
cin>>ws;// stream out any whitespace 
getline(cin,str);
cout<<"Input the nth number : ";
cin>>n;

//Check if that n is not equals to 0 or 
//smaller than that,otherwise you will corrupt str variable
if(n<=0)
{
	cout<<"The nth number too small\n";
}
else
{
c = str.at(n-1);
cout<<"The nth char is : "<<c<<endl;
}

while(1)
{
cout << "Do you want to try again(y/n)? \n";
cin>>ans;
if (ans == 'y')
{
    break;
}
if (ans == 'n')
{
    exit(0);
}
else
{
  cout<<"INVALID CHOICE"<<endl;
  continue;
}
}
}
return 0;
}

Good Luck
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top