Question:
help me to find a mistake in my C++ code?
XXX
2009-04-15 23:38:48 UTC
include
#include
using namespace std;
struct Box
{char maker[40];
float height;
float width;
float length;
float volume;
};
void displayAllBoxes(Box*,int);
int main()
{
char maker[40];
float height;
float width;
float length;
float volume;
cout<<"_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_"<cout<<"How many boxes will you enter? ";
int numb;
cin>>numb;
cin.ignore();
Box*ptr=new Box[numb];
for(int i=0;i{cout<getline(cin, ptr+i)->`maker);
cout<getline(cin,(ptr+i)->height);
cout<getline(cin,(ptr+i)->width);
cout<getline(cin,(ptr+i)->length);
cout<<"Volume: "<displayAllBoxes(ptr,numb);

}


system("Pause");
return 0;
}
void displayAllBoxes(Box*allbxs, int numbbxs)
{cout<<"Here is some info about boxes you've entered"< for(int i=0;i {cout<<(allbxs+i)->maker<<": "<<(allbxs+i)->height<<": "<<(allbxs+i)->width<<": "<<(allbxs+i)->length<<";it's volume is "<<(allbxs+i)->volume< return;
}

}
Three answers:
cja
2009-04-16 06:38:39 UTC
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
?
2009-04-15 23:56:39 UTC
include

#include

using namespace std;



typedef struct Box{

char maker[40];

float height;

loat width;

float length;

float volume;

};



void displayAllBoxes(Box*,int);



int main(){

char maker[40];

float height;

float width;

float length;

float volume;

cout<<"_-_-_-_-_-_-_-_-_-_-_";

cout<<"How many boxes will you enter? ";



int numb;

cin>>numb;

cin.ignore();



Box *ptr=new Box[numb];



for(int i=0;i
cout<
getline(cin, ptr+i)->`maker);

cout<
getline(cin,(ptr+i)->height);

cout<
getline(cin,(ptr+i)->width);

cout<
getline(cin,(ptr+i)->length);

cout<<"Volume: "<
displayAllBoxes(ptr,numb);



}



getch();

return 0;

}



void displayAllBoxes(Box *allbxs, int numbbxs){

cout<<"Here is some info about boxes you've entered"<
for(int i=0;i
cout<<(allbxs+i)->maker<<": "<<(allbxs+i)->height<<": "<<(allbxs+i)->width<<": "<<(allbxs+i)->length<<";it's volume is "<<(allbxs+i)->volume<
}

}
Yahoo! Answers Sucks
2009-04-16 00:01:11 UTC
Why yes I can find many mistakes...



1. using namespace std;

DO NOT EVER EVER EVER DO THAT!



2. Spacing is so horribly bad in this code.



3. Missing # in first include.



4. Box is not 1 byte large, but you are not accounting for this in displayAllBoxes().



5. Extra closing bracket.



6. Again, Box is not 1 byte large, but you are not accounting for this in main().


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...