Reputation: 1
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void printpart1(int length, string *printpart1[18]) {
int dummy;
for (int i = 0; i < length; i++)
cout << (printpart1 + i) << endl;
cin >> dummy;
}
int main() {
const int ARRAY_SIZE = 18;
int dummy;
string part1[ARRAY_SIZE] = { "An example of a career associated with computer studies is a Software Engineer. To become a",
"Software Engineer, a minimum of a Bachelor’s Degree in Software Engineering is required which ",
"could be obtained by first going into Engineering in post-secondary and choosing Software ",
"Engineering as your major. Alternatively you could get a degree in Computer Sciences or another ",
"closely related field. While a Bachelor’s Degree is enough to get some jobs, some employment ",
"opportunities may require a Master’s Degree. Tuition for Engineering at the University of Western ",
"Ontario for Canadian students is around $6000 and requires four years for a Bachelor’s degree. ",
"This means with tuition alone it will cost around $24000 for the minimum qualifications. This price is ",
"much higher factoring in books, living, food etc. An employee in this field makes an average of ",
"$80000 yearly and could get a variety of benefits depending on the company employing them. An ",
"average day for a software engineer varies by company but generally seems to begin with a few ",
"hours of good work, followed by a break to walk around and talk to coworkers about either personal ",
"things or work related affairs followed by more coding. Some days there are interviews with clients ",
"where a software engineer and the client communicate and discuss details of the project. The ",
"majority of the average workday of a Software Engineer is spent programming. ",
"https://study.com/articles/Become_a_Computer_Software_Engineer_Education_and_Career_Roadmap.html",
"https://www.univcan.ca/universities/facts-and-stats/tuition-fees-by-university/ ",
"https://www.coderhood.com/a-day-in-the-life-of-a-software-engineer/"
};
string *part1PTR = part1;
printpart1(ARRAY_SIZE, &part1PTR);
}
The printing must be done in a function with the pointer to the array as an argument. I've been at this for ages trying to figure out how to make this work. Any help would be appreciated.
Upvotes: 0
Views: 64
Reputation: 37227
You forgot to dereference the pointer:
cout << *(printpart1 + i) << endl;
^
Also, you're declaring the parameter as an array of pointers, you should drop the array part:
printpart1(int length, string *printpart1)
^
... and change the function call to
printpart1(ARRAY_SIZE, part1PTR);
^
Upvotes: 2
Reputation: 84551
It looks like you are confused between how to declare and use an array of string
and array of pointers to string
.
string part1[ARRAY_SIZE] = { "...", "...", ...
You declare part1
as an array of string. But then there seems to be some confusion in how to pass that array to printpart1()
. You provide the declaration as:
void printpart1(..., string *printpart1[18]) {
Where printpart1
specifies the parameter printpart1
will be an array of pointers to string. You then call printpart1()
as:
printpart1(ARRAY_SIZE, &part1PTR);
Where you have declared part1PTR
as a pointer to string. By passing the address of part1PTR
, you are providing printpart1()
with a pointer to pointer to string
.
In order to get the actual string for printing you first have to dereference printpart1
(e.g. *printpart1
) to obtain a pointer to string before applying any offset and dereference, e.g. *(*printpart1 + i)
.
For example, the following will provide the wanted string output:
void printpart1(int length, string *printpart1[18]) {
int dummy;
for (int i = 0; i < length; i++)
cout << *(*printpart1 + i) << endl;
cin >> dummy;
}
(note: *(*printpart1 + 1)
is equivalent to (*printpart1)[i]
. Whatever makes more sense to you. Generally the second form is used)
Simply Pass The Array
Now all of this over-complicates what should be as simple as passing the array itself to printpart1()
, e.g. if you change your function to:
void printpart1(int length, string *printpart1) {
int dummy;
for (int i = 0; i < length; i++)
cout << printpart1[i] << endl;
cin >> dummy;
}
You no longer need part1PTR
and can simply call your function with:
printpart1 (ARRAY_SIZE, part1);
Look things over, think through it and let me know if you have further questions.
Upvotes: 0