#include <iostream>
#include <limits>
//#include<cmath> // <--- Not needed right now with the code that you have.
usingnamespace std;
int main()
{
int length{};
do
{
std::cout << "\n How many numbers would you like to use?: "; // <--- Added.
cin >> length;
if (!length || !std::cin)
{
if (!std::cin)
{
std::cerr << "\n Invalid Input! Must be a number\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
}
elseif (!length) // <--- Or (length < 1). You do not want a (0) or a negative number.
{
std::cerr << "\n Invalid length! Must be greater than (0).\n";
}
}
} while (!length);
int* myArray = newint[length];
std::cout << "\n Enter " << length << " numbers as ( 1 2 3 4 ...): "; // <--- Added.
for (int idx = 0; idx < length; idx++)
{
cin >> myArray[idx]; //1112321332
}
std::cout << "\n Numbers entered are: "; // <--- Used for testing. Comment or remove when finished.
for (int idx = 0; idx < length; idx++) // <--- Used for testing. Comment or remove when finished.
{
std::cout << myArray[idx] << ' ';
}
delete[] myArray; // <--- Must have a delete when using "new".
return 0;
}
Your use of blank lines is nice, but you have to many where they are not needed and not enough where they are needed. The first goal, first to your-self and then to others, is to make the code as easy to read as possible. Another advantage to this is that problems or errors tend to stick out and are easier to find.
Something I have a problem with is a "cin" without a prompt. I hate to stare at a blank screen wondering what to do and what should be entered. So unless you intend to provide the source code or written instructions on what to do and how to enter what is needed a good prompt helps.
#include <Windows.h> //<~not sure if this is needed or not.
#include <vector>
usingnamespace std;
int main() {
string s{};
cin>>s;
vector<int> i{};
for (auto m : s) {
i.push_back(m - 48);
}
for (auto m : i) { //for testing
cout << m;
}
}