简体   繁体   中英

Why this code keeps crashing

Why this code gives error:

#include<iostream>
#include<vector>
using namespace std; 
int main()
{
    char x='\0',i=0;
    vector<char> text;
    do
    {
        cout<< "Enter a char" << endl;
        cin>>x;
        if(x<65||(x>90&&x<97)||x>123)
        {
            cout<<"Out of Range-Please Enter Letters"<<endl;
            continue;
        }
    text.push_back(x);  
    if(x=='n'||x=='N'){
            text.clear();
            cout<<"Vector Cleared"<<endl;
            continue;
                       }    
    cout<<text.size()<<endl;
    cout<<"Vector now holds\n";
    for(i=0;i< signed (text.size() ) ;i++)
        {
        cout<< text[i] << endl;
        }
    }while(x!='y');

    cout << "Vector size is " << text.size() << " Vector Capacity is " << text.capacity() << endl;
    vector<char>::iterator it = text.begin();
    for (; it != text.end() ; it++)
        cout << *it << endl;
    cout << "Enter Position to delete: " << endl;

    cin >> i;
    text.erase(text.begin() + i - 1);
    it = text.begin() ;
    for (; it != text.end() ; it++)
        cout << *it << endl;


}

Debug assertion failure, expression vector iterator + offset out of range.

It will crash because i is a character, therefore cin will read a character. For example if someone enters a value 8 , the value of i will be 38 (if you platform is using ASCII encoding).

Plus testing characters for their values is very bad practice use isalpha() from cctype header instead.

Part of your problem is you are using char as an array index:

cout<< text[i] << endl;

You also should consider prefix (not postfix) operators for non-primitive types to improve your performance at these lines:

for (; it != text.end() ; it++)

and

for (; it != text.end() ; it++)

Suggestions:

  1. Use std::string instead of std::vector<char> .
  2. Use character constants instead of their ASCII decimal number ('A' instead of 65).
  3. Prefer library functions instead of comparing character ranges (see isprint, isdigit, isalpha, etc.)
  4. Convert text and characters to upper or lower case before comparing. Use tolower or toupper .
  5. Prefer one variable declaration per line.
  6. Add spaces around operators, improves readability.

I converted your example to use std::string , character functions and added space around operators. Compare to your style for simplicity and ease of reading:

#include <string>
#include <iostream>
#include <cstdlib>

using namespace std; 

int main()
{
    char x='\0';
    unsigned int i=0;
    string text;
    do
    {
        cout<< "Enter a char" << endl;
        cin>>x;
//        if(x<65||(x>90&&x<97)||x>123)
        if (! isprint(x))
        {
            cout<<"Out of Range-Please Enter Letters"<<endl;
            continue;
        }
        text += x;  
        if(toupper(x) == 'N')
        {
            text.clear();
            cout<<"String Cleared"<<endl;
            continue;
        }    
        cout << text.length() << endl;
        cout<<"String now holds\n";
        cout << text << endl;
    }while(tolower(x) != 'y');

    cout << "String length is: " << text.length() << ", capacity: " << text.capacity() << endl;
    cout << text << endl;
    cout << "Enter Position to delete: " << endl;

    cin >> i;
    text.erase(text.begin() + i - 1);
    cout << "After erase: " << text << endl;
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM