hi guys,I really don't know what's the wrong with this code,it should prompt the user to enter any number but -1, and store it in const int size so as to enter charactar array and display it on the screen.
if anyone could help me and tell me what's the wrong with it and what to do ,I'll be thankfull.
#include<iostream>
using namespace std;
void main()
{
while(true)
{
int x;
cin>>x;
if(x==-1)
break;
else
{
const int y=x;
char arr[y];
cin>>arr;
cout<<arr;
}
}
}
I really don't know what's the wrong with this code...
The compiler produces error messages to help you with this.
What do they say?
There may be problems with the way you are trying to create a variable length array.
Also, a constant (as in const int y) must be assigned to a CONSTANT, not a variable. The value is determined at compile time, not run time.
Try using the string class. This is the easy way to get variable length strings.
You will need to #include <string> for this.