Here are the compile errors I'm getting:
foo.cpp: In function `int main()':
foo.cpp:27: error: no matching function for call to `getline(std::istream&, Box*)'
foo.cpp:27: error: stray '`' in program
foo.cpp:27: error: expected `;' before ')' token
foo.cpp:29: error: no matching function for call to `getline(std::istream&, float&)'
foo.cpp:31: error: no matching function for call to `getline(std::istream&, float&)'
foo.cpp:33: error: no matching function for call to `getline(std::istream&, float&)'
foo.cpp:14: warning: unused variable 'maker'
foo.cpp:18: warning: unused variable 'volume'
One error is that you have a stray grave accent before maker, on the line after you prompt for Maker. In general, you can help yourself to find errors in your code by formatting it in a more readable manner. That's always a good idea.
Where you declare a Box, you either need to specify 'struct Box', or typedef the Box struct definition. I prefer the latter.
My repaired version of your code is below. As you can see, I'm recommending many changes, not the least of which is adding a good way to get input, and checking its validity. Actually, that may be the most interesting part of this program.
#include
#include
#include
using namespace std;
template
void getInput(const string *prompt,
T *n1, T *n2 = NULL,
T *n3 = NULL, T *n4 = NULL);
typedef struct Box {
string maker;
float height;
float width;
float length;
float volume;
};
void displayAllBoxes(Box*,size_t);
const string dimensionPrompts[] = {
string("Height: "),
string("Width: "),
string("Length: ")
};
const string makerPrompt =
string("Maker: ");
const string numPrompt =
string("How many boxes will you enter? : ");
int main(int argc, char *argv[]) {
size_t numb;
cout << "_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_" << endl;
getInput(&numPrompt, &numb);
Box *boxArray = new Box[numb];
for (size_t i = 0; i < numb; i++) {
getInput(&makerPrompt, &boxArray[i].maker);
getInput(dimensionPrompts,
&boxArray[i].height,
&boxArray[i].width,
&boxArray[i].length);
boxArray[i].volume =
boxArray[i].length *
boxArray[i].width *
boxArray[i].height;
cout << "Volume: " << boxArray[i].volume << endl;
}
displayAllBoxes(boxArray,numb);
delete [] boxArray;
return 0;
}
void displayAllBoxes(Box *allbxs, size_t numbbxs) {
cout << "Here is some info about boxes you've entered:" << endl;
for (size_t i = 0; i < numbbxs; i++) {
cout << (allbxs+i)->maker << ": " << (allbxs+i)->height;
cout << ": " << (allbxs+i)->width << ": ";
cout << (allbxs+i)->length << "; its volume is ";
cout << (allbxs+i)->volume << endl;
}
}
template
void getInput(const string *prompt,
T *n1, T *n2 = NULL,
T *n3 = NULL, T *n4 = NULL) {
stringstream *ss;
string line;
bool inputOk = false;
T *N[] = {n1, n2, n3, n4, NULL};
T *n = N[0];
int i = 0;
while (n != NULL) {
cout << prompt[i];
do {
getline(cin,line);
ss = new stringstream(line);
if ((!((*ss) >> (*n))) || (ss->good())) {
cout << "invalid input, try again" << endl;
cout << "> ";
} else {
inputOk = true;
}
delete ss;
} while (inputOk == false);
n = N[++i];
inputOk = false;
}
}
Sample run:
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
How many boxes will you enter? : 2
Maker: abc
Height: x
invalid input, try again
> 2
Width: 3 3
invalid input, try again
> 3
Length: 4
Volume: 24
Maker: xyz
Height: -
invalid input, try again
> 5
Width: 3
Length: 6
Volume: 90
Here is some info about boxes you've entered:
abc: 2: 3: 4; its volume is 24
xyz: 5: 3: 6; its volume is 90