Hi i am trying to write a program where the user inputs a list of space separated words (more than ten are required); the program then reverses and prints the order of the words.
Example: input:“Extra Credit Programs”; output: “Programs Credit Extra”
but my program s far is not behaving correctly. can anyone help me please.

Attachments
#include<iostream.h>
#include<string>
using namespace std;

main() {
     std::cout << "Please enter a sequence of words: ";
     std::string y;  // read words and write them one per line
sort (y.begin(), y.end());   
void reverseArrayElement( string y[], int begin, int end); // reverse the elements of a string array
 while(cin >> y) {
    std::cout << y << endl;}
    


//    begin is the index of the first element in the range to be reversed, and end is the index after the last element in the range

system("Pause");

}

Please, just post the code inside code blocks, as in

// This is C++ code

Asking us to download data and then analyze it is inconvenient at best, and a vector for malware at worst. :-(

OKEY HERE it is:

#include<iostream.h>
#include<string>
using namespace std;

main() {
     std::cout << "Please enter a sequence of words: ";
     std::string y;  // read words and write them one per line
sort (y.begin(), y.end());   
void reverseArrayElement( string y[], int begin, int end); // reverse the elements of a string array
 while(cin >> y) {
    std::cout << y << endl;}
    


//    begin is the index of the first element in the range to be reversed, and end is the index after the last element in the range

system("Pause");

}

Edited 5 Years Ago by WaltP: Added CODE Tags -- please use them

You posted Code that makes no sense at all.

1) Output a message
2) define a string variable
3) Sort nothing since all you did was declare a variable
4) declare a prototype for a non-existant function
5) start a loop the 
   a) inputs a string
   b) outputs the string

You need to think through what you want to do and put the steps in their proper order.

some hints/psuedocode

1) create a std::stack of strings
2) Either read words one by one, ending with a sentential value or read the whole sentence
3) If reading words one by one, just add it to the stack else if reading the whole sentence you will have to do some parsing, google "C++ split" or something similar, most likely you will use stringstream and the add that to the stack.
4) Now just pop the stack and output the value popped

Edited 5 Years Ago by firstPerson: n/a

You posted Code that makes no sense at all.

1) Output a message
2) define a string variable
3) Sort nothing since all you did was declare a variable
4) declare a prototype for a non-existant function
5) start a loop the 
   a) inputs a string
   b) outputs the string

You need to think through what you want to do and put the steps in their proper order.

Thanks but i am new to C++; therefore, it is a little hard for me to grasp. But thanks for your insight

A stack is a virtual container that behaves something like an array but has restricted, instead of random access, to whats inside.

The first thing you need to be able to do is be able to identify separate words (also known as tokens or substrings or whatever). Using the enter key and accepting input with the >> operator is one way to get single words, though input isn't necessarily restricted to one word at a time. For example, you can also read in an entire line consisting of multiple words separated by a space (using getline())and then find the spaces to separate the individual words. Finding the spaces can be done several diffent ways, depending on what you know how to do.
1) You could evaluate each char in the input line one at a time. If it isn't a space you add it to a new char array (word)and when you find a space you add a null terminating char to the end of the new char array and then go on to a new char array (word).
2) You could use one of the find() method of the STL string class if you know about STL strings.
3) You could use strtok().
4) You could use a stringstream.
And there may well be other ways to find individual words in a line of input, too.

The point is we don't know what tools you have available. Use what your education has provided you so far. If you are supposed to strike out on your own and find what you can find to do the trick, then pick and choose from one of the above and look them up in your favorite reference material, posting specific questions about a specific process if you have to.

Comments
like

Okey Thanks I made some changes but the program is still not running . Please help :(

#include<iostream.h>
#include<string>
using namespace std;

main() {
     std::cout << "Please enter a sequence of words: ";
     std::string y;  // read words and write them one per line
   
void reverseArrayElement( string data[], int begin, int end); // reverse the elements of a srting array

 while(cin >> y) {
    std::cout << y<< endl;}
  int words;  
 for ( words = 0; words==y ; words--) {
     
        cout << y[words] << endl;
    }


//    begin is the index of the first element in the range to be reversed, and end is the index after the last element in the range

system("Pause");

}

Edited 5 Years Ago by WaltP: Added CODE Tags again -- you've been asked...

This article has been dead for over six months. Start a new discussion instead.