Not working

Hello,
I'm not sure if it's the compiler or my code, but the cstrings limits can be exceeded char response[5]. Also, this cin.getline(response, 4); only extracts three characters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
bool restart()  //returns false if the user wants to restart the program
{
    char response[5]; //holds the users response
    string answer;
    stringstream convert;

    cout<<"Restart the program (y/n)?  ";

    while(true)
    {
        cin.getline(response, 4);

        //convert response to lowercase
        for(int i=0; i<strlen(response); i++)
        {
            response[i]=tolower(response[i]);
        }

        convert.str("");    //clear 'convert'
        convert<<response;  //write to 'convert'

        answer=convert.str();

        if(answer=="y" || answer=="yes")    //if yes
        {
            clr();  //clear the screen
            return false;
        }
        else if(answer=="n" || answer=="no")    //if no
        {
            return true;
        }
        else
        {
            cout<<"\aInvalid, enter (y/n) then press ENTER.  ";
        }
    }
}

I do understand it would be easier to accomplish this function with the string type rather than cstring, I just wanted to see if I could do it. As always, thanks for your help.
Last edited on
I'm not sure if it's the compiler or my code
It's probably your code.

cstrings limits can be exceeded
Correct. C strings are merely arrays, and C++ performs no bounds checking.

Also, this cin.getline(response, 4); only extracts three characters.
Correct. See
http://www.cplusplus.com/reference/iostream/istream/getline/

By the way, you don't need an std::stringstream to assign a C string to an std::string:
1
2
3
char s1[]=/*...*/;
std::string s2;
s2=s1;
See http://www.cplusplus.com/reference/iostream/istream/getline/ to understand why using 4 gets you 3 characters.

Not sure what you mean with "cstrings limits can be exceeded".

Also, it is completely counter-intuitive to return false when the user wants to restart from a function called 'restart'. To make the point more clear, write it within an IF statement: if (restart()) //The function returned true; plain English would interpret as YES, restart.
First, to answer your second statement (you didn't word your topic in questions):

If you take a look at the definition of getline (http://www.cplusplus.com/reference/iostream/istream/getline/)
you will see that only n-1 characters are extracted where n is the second parameter.

the nth character is written as the null terminator ( '\0' )

So cin.getline(response, 4)
says that response is a char array with a length of four, only read 3 chars into it and write the 4th char as 0.

if you want the function to do what you intend, use the length of response:
1
2
3
int const size; // initialized
char response [size];
cin.getline(response, size);


To answer your second statement would require the intense ability to read your mind.
I have no idea what you are attempting to ask.

You said the cstrings limits can be exceeded and referenced your definition of the response array.

How does this show that the char array's (cstring's) limits are being exceeded?

If the cstring ( assuming you mean char[ ] ) does not end in 0 or the null terminator '/0', when strlen is used it will keep reading values until 0 is reached.. if that's what you mean by the cstrings limits being exceeded.

However, if you tried to write to the array past it's size you would get a runtime error.
Thanks guys. When I wrote the return statements, I looked at it as though true meant terminate the program. But I see what you mean.
Topic archived. No new replies allowed.