C++ Assignment Help

C++ Programming Task – Pointers/Arrays

#include <iostream>
#include <string>

using namespace std;

int main()
{
int numItem;
string nameItem;

cout << “\nHow many items would you like to enter? “;
cin >> numItem;
cin.ignore();

string* groceryItem = new string[numItem]; // create dynamic array

for (int i = 0; i < numItem; i++)
{
cout << i + 1 << “. “;
getline(cin, nameItem);
groceryItem[i] = nameItem;
}

delete[] groceryItem; // delete dynamically created array

return 0;
}

 

#include <iostream>
#include <string>

using namespace std;

int main()
{
string item;

cout << “\nEnter five items.\n”;

string strItem[5];

for (int i = 0; i < 5; i++)
{
cout << “Item ” << i + 1 << “: “;
getline(cin, item);
strItem[i] = item;
}

cout << “Reverse string.\n”;
for (int i = 0; i < 5; i++)
{
int len = strItem[i].length();

cout << “Item ” << i + 1 << “: “;
for (int j = len-1; j >= 0; j–)
{
cout << strItem[i][j];
}

cout << endl;
}

cout << endl;

return 0;
}