Home > Software engineering >  I am receiving an error in some code that I wrote and I am not sure why, if anyone could provide som
I am receiving an error in some code that I wrote and I am not sure why, if anyone could provide som

Time:01-30

#include <iostream>
using namespace std;
int main(){
    //set variables
    string name1;string name2;string name3;string n1;string n2;string n3;
    int age1; int age2; int age3;

    //get names and ages
    cout << "Input 3 Names Below:" <<endl;
    cout << "Who Is The First Student?"<< endl;
    cin >> name1; //name 1
    cout << "What is " << name1<< "'s Age?"<< endl;
    cin >> age1; // age 1
    cout << "Who Is The Second Student?"<< endl;
    cin >> name2; //name 2
    cout << "What is " << name2<< "'s Age?"<< endl;
    cin >> age2; //age 2
    cout << "Who Is The Third Student?"<< endl;
    cin >> name3; // name 3
    cout << "What is " << name3<< "'s Age?"<< endl;
    cin >> age3; //age 3

    // gets modified names
    n1 = name1.substr(2, name1.size()-3);
    n2 = name2.substr(2, name2.size()-3);
    n3 = name3.substr(2, name3.size()-3);

    // Output formatting
    cout << "Name             Age             Modified"<<endl;
    cout << name1<< "             "<<age1<<"             "<<n1<<endl;
    cout << name2<< "             "<<age2<<"             "<<n2<<endl;
    cout << name3<< "             "<<age3<<"             "<<n3<<endl;
    return 0;
}   

The output asks the first question which is for the names and then the name of the first student but it outputs an error and this:

Input 3 Names Below: Who Is The First Student? John Doe What is John's Age? Who Is The Second Student? What is 's Age? Who Is The Third Student? What is 's Age?

I am receiving the error :

libc abi: terminating with uncaught exception of type std::out_of_range: basic_string

Could anyone provide some guidance on what the error is?

CodePudding user response:

The std::string::substr() man page says this about the exception you are seeing:

Parameters
   pos   -  position of the first character to include
   count -  length of the substring

Exceptions
   [throws] std::out_of_range if pos > size()

CodePudding user response:

The main problem of the program is that the operator >> reads only one word.

That is for example in these statements

cout << "Who Is The First Student?"<< endl;
cin >> name1; //name 1

You entered a string that contains two words "John Doe". But the operator >> reads only the first word "Jphn" in the variable name1.

So in the next input statement

cout << "What is " << name1<< "'s Age?"<< endl;
cin >> age1; // age 1

an error occurred because instead of a number the input buffer contains the word "Doe".

So as a result the variables name1, name2, and name3 do not contain what you are expecting. And these statements

n1 = name1.substr(2, name1.size()-3);
n2 = name2.substr(2, name2.size()-3);
n3 = name3.substr(2, name3.size()-3);

produce the run-time error.

Instead of the operator >> you should use standard function std::getline.

Here is a demonstration program based on the code of your program that shows what is the reason of the error

#include <iostream>
#include <string>

int main()
{
    std::string name1, name2;
    int age1;

    //get names and ages
    std::cout << "Input 3 Names Below:" << std::endl;
    std::cout << "Who Is The First Student?" << std::endl;
    std::cin >> name1; //name 1
    std::cout << "What is " << name1 << "'s Age?" << std::endl;
    std::cin >> age1; // age 1
    std::cout << "Who Is The Second Student?" << std::endl;
    std::cin >> name2; //name 2

    std::cout << "name1 = " << name1 << '\n';
    std::cout << "name2 = " << name2 << '\n';
}

The program output is

Input 3 Names Below:
Who Is The First Student?
John Doe
What is John's Age?
Who Is The Second Student?
name1 = John
name2 =

As you can see due to the error of reading a data in the variable age1 (the buffer contains the string "Doe") the object name2 is empty. So if you will use it in this statement

n2 = name2.substr(2, name2.size()-3);

then the runtime error will occur.

  • Related