Hi! I need C++ program, please. Here are reqirements. This program should be wit
ID: 3607525 • Letter: H
Question
Hi! I need C++ program, please. Here are reqirements. This program should be without Global variables. All Variables should be in Main function. During declaration of Functions provide type of data which should be returned. setUp (int) for example.
Thank you!
Write a program containing the following functions, in this order:
main - calls the other functions; otherwise does almost nothing
getSize - which asks the user how many strings they want
getSpace - which gets an array in the heap of the size requested by the user
inputData - which allows the user to input the strings and stores them in the array
printData - which prints all the strings, one string per line
destroy - which returns all the space to the heap
All of these functions, except main, shall have a return type of void.
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
void getSize(int &size);
void getSpace(string **str,int size);
void inputData(string *sPtr,int size);
void printData(string *str, int size);
void destroy(string *ptr);
int main()
{
string *strArray = NULL;
int n;
//get how many strings user wants to enter
getSize(n);
//get space of string array
getSpace(&strArray, n);
//ask user to enter strings
inputData(strArray, n);
//print data
printData(strArray,n);
destroy(strArray);
}
void getSize(int &size)
{
cout << "Enter the number of strings : ";
cin >> size;
}
void getSpace(string **str, int size)
{
*str = new string[size];
}
void inputData(string sPtr[], int size)
{
for (int i = 0; i < size; i++)
{
cout << "Enter string" << i + 1 << " : ";
cin >> sPtr[i];
}
}
void printData(string str[], int size)
{
for (int i = 0; i < size; i++)
{
cout << "String" << i + 1 << " : " << str[i] << endl;
}
}
void destroy(string *ptr)
{
delete[]ptr;
}
/*
Enter the number of strings : 4
Enter string1 : John
Enter string2 : Diya
Enter string3 : Smith
Enter string4 : Rita
String1 : John
String2 : Diya
String3 : Smith
String4 : Rita
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.