-3

I am writing a code where I am asking user to assign value to an array and where user press enter key I want to assign a default value to array elements. Any idea how to proceed with this ?

I have tried using cin.get() method but it is not working. Here is my code :



#include<iostream>
#include<math.h>
#include<cmath>

using namespace std;

int main()
{
    int n;
    cout << "Enter array size: ";
    cin >> n;

    double y[n];
   
    string input;
    for(int i=0; i<n; ++i)
    {
        cout << "Enter Initial Velocity ( Default Value 0 ) : " ;
        y[i] = cin.get();
        if (input=="")
        {
            y[i]=0.0;
        }
        else
        {
            y[i]=stod(input);
        }
    }

}



  • 2
    `input` will always be an empty string since nothing ever changes it so the array value will always be overwritten with 0. – Retired Ninja Oct 28 '22 at 22:22
  • 2
    `double y[n];` - whichever C++ textbook showed you to do this -- you need to throw it away immediately, and get a different C++ textbook. If you copied that off some web site, don't visit that web site any more. If you saw this in some clown's Youtube video, unsubscribe from that channel, you're not learning proper C++. This is not standard C++, and many C++ compilers will refuse to compile this. – Sam Varshavchik Oct 28 '22 at 22:28
  • `y[i] = cin.get();` -> `std::getline(std::cin);`? – Jarod42 Oct 28 '22 at 22:28
  • Worse, the compilers that do allow arrays of variable length expect you to use them carefully. With `cin >> n;`, the user is allowed to control the size of the array and since the user's input is not checked or bound in any meaningful way, they could enter a large value and run the program out of Automatic Storage. Not much fun debugging the effects of that. They could input a zero or a negative number, and that won't be much fun to debug, either. – user4581301 Oct 28 '22 at 23:06

2 Answers2

0

try this

#include <iostream>
using namespace std;

int main()
  {
  cout << "Press the ENTER key";
  if (cin.get() == '\n')
    cout << "Good job.\n";
  else
    cout << "I meant ONLY the ENTER key... Oh well.\n";
  return 0;
  }
0

I've tried to change minimal things to make your code work as you've expected. Bunch of stuff can be improved, array creation (see this), not including useless files, reducing the scope of local variables, also don't use using namespace std - see this.

#include <math.h> //useless

#include <cmath> //useless
#include <iostream>
//#include <string> some compilers may require adding this


using namespace std; //bad practice

int main() {
    int n;
    cout << "Enter array size: ";
    cin >> n;

    double y[n]; //not a great way to create an array, fix this
    cin.ignore();

    //can be moved inside the loop
    string input;
    for (int i = 0; i < n; ++i) {
        cout << "Enter Initial Velocity ( Default Value 0 ) : ";
        getline(cin, input);
        if (input == "") {
            y[i] = 0.0;
        } else {
            y[i] = stod(input);
        }
    }

    for (int i = 0; i < n; i++) {
        if (i > 0) cout << ' ';
        cout << y[i];
    }

    cout << '\n';
}
Sandro J
  • 508
  • 3
  • 11