USING C Read 12 strings into an array then display them in r

USING C++

Read 12 strings into an array, then display them in reverse order. For example:

My code seems to work great but it removes the last line in the reverse, I am unsure how to resolve the issue.

Code:

#include
#include

#define LENGTH 12

using namespace std;

int main() {

string arr[LENGTH];

cout << \"Reading in: \" << endl;
for(int i=0;i {
getline(cin, arr[i]);
cout << \" \" << arr[i] << endl;
}

My code is cutting off \"Adam\" from the last lin

Solution

While printing reversed string ,you might have missed if array index i >= 0 in for loop ..

Below code prints all strings while reversing..

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

#define LENGTH 12
using namespace std;
int main()
{
   string arr[LENGTH];
   cout << \"Reading in: \" << endl;
   for(int i = 0; i < LENGTH; i++)
   {
       getline(cin, arr[i]);
       cout << \" \" << arr[i] << endl;
   }
   //reverse string of array
  
   cout<<\"Reversed: \"<<endl;
   for(int i = LENGTH-1; i >=0; i--)
   {
       cout<<arr[i]<<endl;
   }
}

USING C++ Read 12 strings into an array, then display them in reverse order. For example: My code seems to work great but it removes the last line in the revers

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site