0

If I declare a string array in c++ such as

char name[10]

how would you error handle if the input is over the character limit?

Edit: My assignment says to use cstring rather than string. Input will be the person's full name.

2
  • What "input" do you mean? Commented Oct 29, 2014 at 19:48
  • Any reason not to use a dynamic allocation? Or you could use a cin.read(name, sizeof(name)); to only allow you to read at max it's size. This wouldn't include any null space assuming user inputs more than the variables size. Commented Oct 29, 2014 at 21:40

4 Answers 4

1

Here is an example where setName checks the size is OK before assigning the char[10] attribute.

Note char[10] can only store a 9-characters name, because you need one character to store the end-of-string.

Maybe that's what you want:

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

#define FIXED_SIZE 10

class Dummy
{
public:
    bool setName( const char* newName )
    {
        if ( strlen( newName ) + 1 > FIXED_SIZE )
            return false;

        strcpy( name, newName );
        return true;
    }
private:
    char name[FIXED_SIZE];
};

int main()
{
    Dummy foo;

    if ( foo.setName( "ok" ) )
        std::cout << "short works" << std::endl;
    if ( foo.setName( "012345678" ) )
        std::cout << "9 chars OK,leavs space for \0" << std::endl;
    if ( !foo.setName( "0123456789" ) )
        std::cout << "10 chars not OK, needs space for \0" << std::endl;
    if ( !foo.setName( "not ok because too long" ) )
        std::cout << "long does not work" << std::endl;

    // your code goes here
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

I'm piecing together that your instructions say to use <cstring> so you can use strlen to check the length of the string prior to "assigning" it to your name array.

so something like...

const int MAX_NAME_LEN = 10;
char name[MAX_NAME_LEN];
// ...
// ...
if (strlen(input)+1 >= MAX_NAME_LEN) {
// can't save it, too big to store w/ null char
}
else {
// good to go
}

2 Comments

strlen(input) returns the string size without EOS. So if you want to save it in a MAX_NAME_LEN array, isn't the condition to detect incapability to save the string strlen(input)+1 > MAX_NAME_LEN rather than strlen(input)-1 >= MAX_NAME_LEN?
Ah yes good catch, I believe the max we could store is 9 characters + \0 so that would be strlen(input)+1 >= MAX_NAME_LEN
0

First of all your question is not clear. Anyway I assume you want to ask for a way to ensure array index does not get out of bound.

Anything outside of that range causes undefined behavior. If the index was near the range, most probably you read your own program's memory. If the index was largely out of range, most probably your program will be killed by the operating system.

That means undefined behaviour could mean program crash, correct output etc.

Comments

0

Since others mentioned how to do this with a predefined input string, here's a solution which reads a c-string from input:

#include <iostream>

#define BUF_SIZE 10

using namespace std;
int main()
{
    char name[BUF_SIZE];
    cin.get(name, BUF_SIZE-1);
    if (cin) //No eof
        if (cin.get() != '\n')
            cerr << "Name may not exceed " << BUF_SIZE-1 << " characters";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.