Reversing sentence order using char arrays and pointers(College Assignment)

Noob here. Ok so what I'm suppose to do is take short sentences from the user and output the reverse order of the user input to the user. IE:
I like blue
I hate red
Hello

Hello
I hate red
I like blue

Here's my code for reference:

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
39
40
#include <iostream>
#include <string>

using namespace std;

// Declare Constants
const int SIZE = 500;
const int N = 30;

int main()
{
	// Declare Arrays
	char *input[SIZE];
	char *inputPtr = new char [SIZE] ();
	char temp [N];
	
	// Declare Variables
	int count = 0;
	int length = 0;
	do
	{
		// Get sentence from user and save into the area
		cout <<"Please enter a short sentence or press 0 to quit:" << endl;	
		cin.getline(temp + '\0', N);
		if(*temp == '0')
		{
			break;
		}
		strcat(inputPtr, temp);
		input[count++] = &inputPtr[length];
		length += strlen(temp);
	}while(*temp != '0');

	for(int i = count - 1; i >=0; i--)
	{
		cout << input[i] << endl;
	}
	system("PAUSE");
	return 0;
}


Now so far it appears to work. I can input
hi
hello
ok

and my output is this:
ok
hellook
hihellook

What I need is to have my output look like this:
ok
hello
hi

Friendly advice welcomed
a string array has index numbers right? well just make a cout array function and have another function that calls out the index in any order you choose, im gona have a pop at that now myself for fun, if its got to be dynamic, use vectors :)
youve over complicated it,your using a reference to the address of the array that is input and that [length] is going to increase in size, you want it to be the same length as the word you want and start at the right point and end at the right point...dude...vectors, less lines less overhead and more readible

Last edited on
Sadly the teacher wants us to use char arrays
"The Problem
In this assignment you will allow the user to type in a number of short, one line sentences. Each sentence is added to a buffer of 500 characters. Be sure that each sentence in the buffer is null terminated. As each sentence is added to this buffer, store a pointer to the sentence in an array of char pointers. When the user enters a zero, stop getting input from the user and display the sentences in the buffer in reverse order. Note, the sentences are in reverse order, not the words in the sentences. For example, if the user typed

I like the color red.
Blue is also a pretty color,
But I don't like orange.
0

your program would display

But I don't like orange.
Blue is also a pretty color.
I like the color red."
http://www.cplusplus.com/forum/beginner/92887/ ¿same class?
> Be sure that each sentence in the buffer is null terminated


Also, explain me these lines
1
2
3
char *inputPtr = new char [SIZE] ();
cin.getline(temp + '\0', N);
system("PAUSE");


By the way, you don't need `input' (it shouldn't be so big either)
Last edited on
I think your problem is on line 29. You concatenate the strings before putting it into the array. If you wanted to do that you would need to increase inputPtr by length, but you don't have length (it is still 0), so it would be much easier to do this:

Try deleting line 29, and change line 30 to:

input[count++] = *temp;

I haven't tested that, just going on the idea of:

1
2
3
//get input
//assign to array
//print array in reverse 


You can do simple debugging by placing extra cout statements to see what the value of different variables are at different points. Otherwise learn to use the debugger program in your IDE, which is easier in the long run. You can have a watch list of variables, see how they change as you step through the code 1 line at a time. See where they go wrong and deduce the problem.

HTH
Last edited on
> input[count++] = *temp;
That makes no sense (it's good that it does not compile)

Also, it's a requirement to have all the sentences in a buffer (all contiguous in memory)
input[count++] = *temp;
That makes no sense (it's good that it does not compile)


temp is the input, and we need to assign it to the array. I should have used temp not *temp. My bad.
Yeah me and mkteagle have been collaborating back and forth with our class forum trying to work this out together.
> temp is the input, and we need to assign it to the array. I should have used temp not *temp.
They are not `std::string', you can't assign like that.
`strcpy' would be wrong too.
You aren't really doing what the assignment requires. Here's a straightforward implementation:

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
39
40
41
42
43
44
45
46
47
#include <iostream>

const unsigned bufferCapacity = 500 ;
const unsigned maxStrings = 20 ;
const char nul = '\0' ;

int main ()
{
    char buffer[bufferCapacity] ;
    char* const bufferEnd = buffer + bufferCapacity ;

    char* strings[maxStrings] ;
    unsigned nStrings = 0 ;

    char * bufPos = buffer ;


    std::cout << "Enter one line sentences.  0 to quit.\n" ;

    bool notDone = true ;
    while ( notDone )
    {
        std::cin.getline(bufPos, bufferEnd-bufPos) ;
        if ( bufPos[0] != '0' )
        {
            strings[nStrings++] = bufPos ;          // store the address of the string.

            while ( *bufPos++ != nul )              // advance past the just-stored string.
                ;             

            if ( bufPos >= bufferEnd || nStrings == maxStrings )    // sanity check.
                notDone = false ;
        }
        else
            notDone = false ;
    }

    std::cout << '\n' ;

    if ( nStrings )
    {
        do
        {
            std::cout << strings[--nStrings] << '\n' ;
        } while ( nStrings ) ;
    }
}


If you've questions, please ask. Pointers are tricky things.
Topic archived. No new replies allowed.