Vectors giving me trouble (string and double)

Hello! I am working on vectors and my program is giving me problems. This is the specification:
A manufacturer needs a program to determine the total cost of producing some number of different types of cylindrical machine parts. The cost for one machine part is the cost of one square centimeter of the materials times the number of square centimeters in the total surface area of the container. The machine parts are open on one end, so the total surface area is the surface area of the base plus the surface area of the cylinder. Each part type has the following data to describe it:
• the name of the part
• the cost of the materials required
• the radius of the base
• the height of the cylinder
Your program should define four vectors to hold the data for several parts (name, cost, radius, and height). These vectors will be parallel structures which together hold all of the information for multiple parts.


I don't have much code
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
    vector <string> name ( Short123, Medium234, Tall345 );
    vector <double> cost ( 1.25, 4.79, 5.29);
    vector <double> radius (2.0, 3.14, 5.19);
    vector <double> height (2.0, 4.1, 7.5);
 
}
Last edited on
there are several ways to construct a vector.
vector <double> height {2.0, 4.1, 7.5}; is what you want here, but I recommend studying the others. Another one is: vector<double> x(10); //10 locations exist. There are also different way to set up the memory, ... reserve keeps the vector empty but allocates space for it, for example.
Last edited on
what about for the string vector? the error that that is giving me is :
identifier "Short123" is unidentified
Last edited on
you don't have a variable Short123.
did you mean "Short123"?
it works the same way..
{"one","two","three"}
Last edited on
Ok thank you! I understand it now.

The next part of what I have to do is Loop and input data for each part until a user indicates that all part data is entered, storing the data for each part in the vectors


I have my updated code but the cin in the while loop is giving me an error
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
    vector <string> name{ "Short123", "Medium234", "Tall345" };
    vector <double> cost{1.25, 4.79, 5.29};
    vector <double> radius{ 2.0, 3.14, 5.19 };
    vector <double> height{ 2.0, 4.1, 7.5 };
    bool MoreParts = true;
    int YesorNo;
while (MoreParts = true)
    {
        cout << "Enter the name, cost, radius, and height for a part:" << endl;
        cin >> name;
        cout << "Are there more parts to enter? Enter 1 for yes." << endl;
        cin >> YesorNo;
        if (YesorNo == 1)
        {
            MoreParts = true;
        }
    }

}

line 14 is an assignment. turn on your compiler warnings, and read them, it will help deal with these kinds of typos.

name is a vector. you can't cin a vector. you can cin a string, though.
string tmp;
while()
cin >> tmp;
name.push_back(tmp);

moreparts is redundant.
just say something like:
int yesorno{0};
do
{..}
while (yesorno != 1); //the boolean serves no purpose here.
your current logic has no way to set it to false and actually stop! This is part of the trouble caused by have 2 variables to represent the same thing; its easy to overlook an issue.
Last edited on
Macbeth, act iv.

I just came here because of your awesome title.


https://www.poetryfoundation.org/poems/43189/song-of-the-witches-double-double-toil-and-trouble
Topic archived. No new replies allowed.