user5808331
user5808331

Reputation:

Why won't std::cin print and instead causes an error?

This program gives me an odd error that I can simply not figure out, it says some things about an out of range object. When I run this program without the std::cin >> firstLast;, and instead I just hard-code the string firstLast into something else, it works. I haven't seen this anywhere and have been searching for days on why it doesn't work.

#include "stdafx.h"
#include <string> 
#include <iostream>

int main()
{
    //Declaring firstLast string
    std::string firstLast;

    //Asking for input for last and first name (same string)
    std::cout << "Enter your first and last name below." << "\n>>";

    //Getting firstLast value from user
    std::cin >> firstLast;

    //This finds the space in the string so I can seperate the first and last name into different strings
    int index = firstLast.find(' ');

    /*
    This makes a substring. The substring starts at index (the beginning of the surname) and goes on for the size of the surname (which is equal to the length of firstLast - first name length).

    Ex: Name - "John Hopkins"

    Length of "John" = 4
    Length of " Hopkins" = firstLast.length() - 4
    */
    std::string lastName = firstLast.substr(index, firstLast.length() - index);

    //Printing the found surname
    std::cout << "Your surname is " << lastName << "." << std::endl;

    int rnd; std::cin >> rnd; return 0;
}

I am really not sure why this does not work, if I hard-code the firstLast string, it works, but when I use cin to get the string, it crashes and gives me ERROR:

Unhandled exception at 0x7626D928 in Test.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0018F374.

Upvotes: 1

Views: 96

Answers (3)

lcs
lcs

Reputation: 4255

std::cin by default will read up until it sees whitespace.

So in your case, if you enter Joe Smith, firstLast will contain the word Joe, and Smith will be sitting in the stream waiting to be extracted.

This means std::string::find is going to return std::string::npos, which means that your call to std::string::substr is going to be wrong.

To avoid this, you can either perform two std::cin calls, to get the first and last names seperately, or use getline to retrieve the entire line.

Upvotes: 1

Sudipta Kumar Sahoo
Sudipta Kumar Sahoo

Reputation: 1147

Here the problem is you are using cin to take the first and last name with space. But the property of cin is that it differentiate between arguments based on the space character in console.

std::cin >> firstLast;

after this line, firstLastwill only have "First Name". Sp after this if you try to extract Last name, it will cause error in your program in below line.

std::string lastName = firstLast.substr(index, firstLast.length() - index);

Example: if you are entering ""Hello world", it will only take "Hello".

Instead of this, you can take First name and Last name separately.

Hope this helps.

Upvotes: 0

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

int index = firstLast.find(' ');

Would never find a whitespace in your string, since

std::cin >> firstLast;

already reads only up to the next whitespace.

To read a string containing whitespaces use

std::getline(cin,firstLast);

instead.


If you use the std::string::find() function always check the result against string::npos before using it as an indexing value.

Upvotes: 3

Related Questions