0

The program needs to ask the user how many sides their shape has only being 3 correct options (3 sides, 4 sides, and 5 sides). Based off of their answer, the program then asks for the length of each of the sides, and then totals the sides in a loop to determine the perimeter of the designated shape. My code allows for the user to be told what shape they have, but I am not sure how to make an empty array and store the user input in it, then add the data in the array together. Here's my code:

#include <iostream>
using namespace std;
int main() {

    int sides = 0;
    int i = 0;

    //
    //

    do
    {
        cout << "How many sides does your shape have? " << endl; i++;
        cin >> sides;

        if (sides > 3 || sides < 5)
        {
            if (sides == 3)
                cout << "You have a triangle!" << endl;
            if (sides == 4)
                cout << "You have a quadrilateral!" << endl;
            if (sides == 5)
                cout << "You have a pentagon!" << endl;

            break;
        }
        else
        {
            cout << "That's not a valid shape here. Try again." << endl;
        }

    } while (1);
    do
    {
       //
       //

    } while (1);

    do
    {
       //
       //
    } while (1);

}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Which part of your C++ book that explains how to use arrays, and gives examples of them, you're unclear about? – Sam Varshavchik Jan 27 '20 at 04:10
  • I am unsure how to write them, and the code used in book does not match with the assignment @SamVarshavchik – charliecharlie Jan 27 '20 at 04:14
  • Both the book and your assignment require you to use arrays in the same way (I.e. declare them, initialize them, and use them), so even though, I'm sure, the examples in your C++ book are different, the general principles and the fundamental operation on the arrays are logically identical. stackoverflow.com is not a code-writing service, nobody will write code for you, but we can answer ***specific*** C++-related questions. – Sam Varshavchik Jan 27 '20 at 04:15
  • I understand, I didn't ask for someone to write my code. I am use to python, but in C++ is creating an empty array like "int x[];{}" ? – charliecharlie Jan 27 '20 at 04:17
  • Are you looking for https://en.cppreference.com/w/c/language/array_initialization ? - `int sides[3] = {0};`. You should clearly specify your question without the unrelated parts of the code. – tangy Jan 27 '20 at 04:24
  • 1
    In C++, arrays always have a fixed size. So if you want an array of three `int` values, the declaration would be `int x[3];`. If you need a variable-length arrays, this is what `std::vector` is for. For more information on how to use `std::vector`, see your C++ book. – Sam Varshavchik Jan 27 '20 at 04:31
  • Does this answer your question? [Create an array when the size is a variable not a constant](https://stackoverflow.com/questions/57367473/create-an-array-when-the-size-is-a-variable-not-a-constant) –  Jan 27 '20 at 05:50

1 Answers1

0

(Sorry, cannot comment as of yet)

C++ requires that the size of an array be known during initialization, and you can use either the runtime stack or the heap to do this. In my experience, the runtime stack is great for arrays whose sizes are known during compilation and the heap is great for arrays whose sizes need to be determined during runtime. Your question essentially looks like you are asking us to answer a hefty portion of your coding project, which is not what Stack Overflow is for, but if you have any specific questions we are happy to oblige.

Shaavin
  • 125
  • 8