0

I've looked everywhere, but I cannot find a solution to exactly why this happens in my situation.

I'm making a simple string function that asks for a string, and prints out the length.

However, I get an "Invalid Null Pointer" assertion error when I run the compiled version. I have had no errors when compiling, but the error comes up when I run it.

This is the function causing the problem:

string getString() 
{
    string wordInput;

    cout << "Enter a word that has AT LEAST four (4) letters!  ";
    getline(cin, wordInput);

    while (wordInput.length() <= 3)
{
    cout << "Enter a word that has AT LEAST four (4) letters!  ";
    getline(cin, wordInput);
}

return 0;
} 

The while loop isn't a problem. I commented it out and I still got the same error. How is initializing word input, cout, and getline giving me the error?

Here is my whole code so far (not finished). I tried running the string by itself too, the getKeyLetter function isn't a problem.

#include <iostream>
#include <string>
#include <cassert>

using namespace std;
char getKeyLetter()
{
    char keyLetter;
    string convertFromString;
    cout << "Enter a SINGLE character!  ";
    getline(cin, convertFromString);

while (convertFromString.length() > 1)
{
    cout << "Enter a SINGLE character!  ";
    getline(cin, convertFromString);
}

assert(convertFromString.size() == 1);
keyLetter = convertFromString[0];
return 0;
}

string getString()
{
string wordInput;

cout << "Enter a word that has AT LEAST four (4) letters!  ";
getline(cin, wordInput);

while (wordInput.length() <= 3)
{
    cout << "Enter a word that has AT LEAST four (4) letters!  ";
    getline(cin, wordInput);
}

return 0;
} 

int main()
{
getKeyLetter();
getString();
return 0;
}
  • *Where* do the assert happen? Where in *your* code does it happen? Have you tried to catch it in a debugger? What are the values of all involved variables then? Perhaps you should take some time to read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) by Eric Lippert. – Some programmer dude Jul 17 '17 at 06:15
  • In addition to the good advice from some programmer dude, you should always strive to provide an [mcve]. If you see the same error without the getKeyLetter function, then it should not be part of your example. That also makes rubber duck debugging much easier. – MikeMB Jul 17 '17 at 06:20
  • 4
    Here's a hint: when a function return type is a `string` or `char`, do not end the function with a `return 0`. – Eran Jul 17 '17 at 06:24

1 Answers1

0

First, in your GetKeyChar() function, writing:

char ch;
cout << "Enter a single character: ";
cin >> ch;

will give you the first character the person types into the command prompt. So, typing "check" will have ch = c.

Second, as eran said, at the end of your functions, you have

return 0;

Unless you want both functions to return a char and string respectively, make them void GetKeyLetter() and void GetString(). Or, if you do want to return something, have them return ch (from my example) and return wordInput.

Only int main(), per standard, needs return 0, to show you that it exited correctly. the variable type you put in front of your functions is what variable you plan on returning. 0 is an int, so that's what it returns based on convention. As was pointed out, a return is not necessary in main. If you want your functions to return values, do this in your main.

string str;
char ch;
ch = GetKeyLetter();
str = GetString();
return 0;

And have your functions return the char and string value you want them to.

  • *Only int main() needs return 0* - And even that is [not required in ISO C++](https://stackoverflow.com/a/276814/577603). – ComicSansMS Jul 17 '17 at 06:57
  • Hahaha, I knew someone would point that out. He didn't know what returns were for, so I figured I'd tell him why there's a return in main, (because that's where he got the idea that you need a return 0 at the end of functions). Thanks for your comment! – Zach Hammond Jul 17 '17 at 07:09