started a new thread because naru got my interest. please explain.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
void reverseword(char * reverse) {
    size_t size = strlen(reverse);              //get the size of the char *
    for(size_t i = 1; i <= size; i++)     
    {
        cout << reverse[size - i];                  //output the word
    }
}
void reverseword (string reverse) {
    size_t size = reverse.size();
    string::reverse_iterator walkword;
    for(walkword = reverse.rbegin(); walkword < reverse.rend(); walkword++)
    {
        cout << *walkword;
    }
}
int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Method one for string reversal" << endl;
    cout << "==============================" << endl;
    cout << "== Using Char * and cin.getline" << endl;
    cout << "== using for statement to itterate" << endl;
    cout << "==============================" << endl;
 
    char * getwords = new char();       //will hold the words
    cout << "Please enter the words to reverse" << endl << endl << endl;
    cin.getline(getwords,100);           //get the word(s) to reverse
    reverseword(getwords);      //reverse the words
    cin.sync();//clean up buffer for next example
 
    cout << endl << endl << endl;
    cout << "Method Two for string reversal" << endl;
    cout << "==============================" << endl;
    cout << "== Using std::basic_string cin.getline" << endl;
    cout << "== using build in itterator members" << endl;
    cout << "==============================" << endl;
 
    string getwordsagain;
    cout << "Please enter the words to reverse" << endl << endl << endl;
    getline(cin, getwordsagain);
    reverseword(getwordsagain);
    cin.sync();                 //clean the buffah
    return 0;
}

cheers!

Recommended Answers

All 7 Replies

>size_t size = strlen(reverse);
For starters, you need to include <cstring> to use the C-style string functions.

>char * getwords = new char();
This is the cause of your undefined behavior. You allocated one character. Only one. That means you can only store one character, ever. But look here:

>cin.getline(getwords,100);
You're telling getline that it can store up to 100 characters in getwords. This is called buffer overflow, and it's undefined. That's a very bad thing.

>cin.sync();//clean up buffer for next example
sync isn't required to do what you think it does. An extremely knowledgeable person explained why here. ;)

I'm not a huge fan of stdafx.h either, but I can let that slide.

>size_t size = strlen(reverse);
For starters, you need to include <cstring> to use the C-style string functions.

>char * getwords = new char();
This is the cause of your undefined behavior. You allocated one character. Only one. That means you can only store one character, ever. But look here:

thanks, i had wondered how to allocate more instead of using a constant char foo[100]; i thought maybe that was the solution, if not i will ask around :)

>cin.getline(getwords,100);
You're telling getline that it can store up to 100 characters in getwords. This is called buffer overflow, and it's undefined. That's a very bad thing.

very much so :)

>cin.sync();//clean up buffer for next example
sync isn't required to do what you think it does. An extremely knowledgeable person explained why here. ;)

will read, i remember someone posting about using cin.flush to remove any characters from the buffer in case a user entered in more than was allocated but i tried that and got errors :(

I'm not a huge fan of stdafx.h either, but I can let that slide.

yeah i didnt want to include it, i checked the box to remove pre-compiled headers but i guess i have no say in that when using the express version of VC++.

oh and by the way, when i was researching this in google a few links to daniweb popped up, and of course you were the one responding and bringing up the issue of undefined and implementation-defined behavior in C thread.

>i had wondered how to allocate more instead of using a constant char foo[100];
It's great, you can use a similar syntax to allocate more than one:

char *p = new char[100];

This creates 100 characters and points p to the first of them.

>i remember someone posting about using cin.flush to remove any characters from the buffer
They were probably wrong. I say probably because flush is a valid operation on an ouput stream. It forces any characters in the stream buffer to be written to the destination.

commented: + cool points for being so informative +6

>i had wondered how to allocate more instead of using a constant char foo[100];
It's great, you can use a similar syntax to allocate more than one:

char *p = new char[100];

This creates 100 characters and points p to the first of them.

>i remember someone posting about using cin.flush to remove any characters from the buffer
They were probably wrong. I say probably because flush is a valid operation on an ouput stream. It forces any characters in the stream buffer to be written to the destination.

cool, all makes sense to me now :)

You're telling getline that it can store up to 100 characters in getwords. This is called buffer overflow, and it's undefined. That's a very bad thing.

how much buffer or say... space in memory...? does that variable may take up to?

>how much buffer or say... space in memory...? does that variable may take up to?
You'll have to rephrase your question. I'm not sure what answer you're looking for.

>how much buffer or say... space in memory...? does that variable may take up to?
You'll have to rephrase your question. I'm not sure what answer you're looking for.

i think he/she may be asking what is the maximum ammount of information a char * can take.

or what is the max buffer size for the getline statement

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.