1

I want to check to see if the user added a ? to the end of the buffer. If not, I want the program to add one automatically. This is what I have so far. I dont know what to do next.

First I check to see if the buffer is not blank.
Then, if the last item is not a ?, add the question mark automatically to the buffer and then copy the content to the current data node.

if ( strlen(buffer) != 0)
{
   if (buffer[strlen(buffer)-1] != '?')
   {

           //what do i put here to add the ? if theres non?    
   }

strcpy(current->data,buffer);

}
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
user1896464
  • 345
  • 1
  • 4
  • 11
  • 1
    this is a C question, in C++ you would not manipulate buffers directly – TemplateRex May 02 '13 at 21:01
  • Do you really need to add it to the buffer itself, or do you just need the question mark to go on the end of current->data? – Egor May 02 '13 at 21:02
  • @rhalbersma - while this is a C question, the second part of your comment is not necessarily true. The poster may be writing C-style code within a larger C++ application, or intending to feed it to a C++ compiler. – Chris Stratton May 02 '13 at 21:04
  • @RichardTeviotdale C also has `operator->` – TemplateRex May 02 '13 at 21:07
  • @ChrisStratton this is a strict C program, with no reference to any C++ construct, using C-style code and library functions (`strlen`, `strcpy`). – TemplateRex May 02 '13 at 21:12
  • 1
    its a C++ program. i just need the question mark to go on the end of current->data and i was thinking it would be easier to modify the buffer. – user1896464 May 02 '13 at 21:13
  • so what is the c++ function of (strlen, strcpy) – user1896464 May 02 '13 at 21:14
  • @rhalbersma - this *excerpt* is C style, and something a C compiler would accept, and so a C style solution is apparently wanted. But only the poster knows what the rest of the program is, or what compiler will need to be willing to accept it. – Chris Stratton May 02 '13 at 21:14
  • I am adding at least the C tag since those SO members are much more likely to generate answers. – TemplateRex May 02 '13 at 21:14
  • That I can agree with. – Chris Stratton May 02 '13 at 21:15
  • What is your data type? If it is `char` array, you will have to allocate a new `char` array of buffer+1 in length, `strcpy` the original buffer into the new buffer, then set the next to the last character as `'?'` and the final spot as `0`. If it is a `string` data class, then you can use any of its append functions, such as `buffer += "?";` – StarPilot May 02 '13 at 21:16
  • @ChrisStratton I find it very strange that outside context not directly mentioned the OP would determine the correct tagging. This whole excerpt could be wrapped inside a shell script being fed to a continuous integration server or what not. That would not justify tagging this question with `bash` or `jenkins`. – TemplateRex May 02 '13 at 21:23
  • @rhalbersma the context was explicitly mentioned by the poster's use of the C++ tag. And it *is* potentially relevant, because not quite everything that will compile if the file extension is .c will do so if the extension is .cpp and the compiler has not been explicitly told that the code is plain C. – Chris Stratton May 02 '13 at 21:25
  • @ChrisStratton ipse dixit does not make it true. – TemplateRex May 02 '13 at 21:29
  • my program is in C++, the reason why im using the (strlen, strcpy) is because im my data is a char array. and i dont know the simplest way to copy the content of the array other than using a for loop. thats why i decided to use the (strlen, strcpy). it works fine in Vstudio. i just get a warning message. – user1896464 May 02 '13 at 22:29

2 Answers2

7

From what I can see, you don't gain anything from modifying buffer in this way. You can simply add the ? to current->data if it is needed.

int len = strlen(buffer);
strcpy(current->data, buffer);
if (len && buffer[len-1] != '?') {
    current->data[len] = '?';
    current->data[len+1] = '\0';
}

If it is an option, you should consider changing your code to use std::string instead.

std::string buffer = input();
if (!buffer.empty() && buffer.back() != '?') buffer += '?';
std::copy(buffer.begin(), buffer.end(), current->data);
current->data[buffer.size()] = '\0';

If you don't have a C++11 compiler, use *buffer.rbegin() instead of buffer.back().

jxh
  • 69,070
  • 8
  • 110
  • 193
  • tiny nitpick: `buffer.size()` could be written as the slightly more idiomatic `!buffer.empty()` – TemplateRex May 02 '13 at 21:39
  • @rhalbersma: Done. I was thinking it would be better to maintain the same "logic" of the original code, but `!empty()` is more clear. – jxh May 02 '13 at 21:45
  • the logic hasn't changed, only the syntax, and I agree it's clearer now, so +1 :-) – TemplateRex May 02 '13 at 21:51
0

Why not create a function that checks whether or not the last character is a question mark before you concatenate the question mark?

//Create function that returns a bool
bool isQuestionMark(char * buffer)
{  
    //Create pointer to buffer    
    char * pointer = buffer;

    //Go to the null character
    while(*pointer != '\0')
        pointer++;

    //Get to the last character
    pointer--;

    //Check to see if last character is a question mark
    if(*pointer == '?')
        return true;
    else
        return false;
}

Then you want to call that function to see if you need to concatenate a question mark.

if(isQuestionMark(buffer) == true)
    strcat(buffer, "?");
else
    //Do nothing
MrPickle5
  • 522
  • 4
  • 9
  • 31