Please have a look on the two codes below which differ slightly. CODE 1 is working fine but CODE 2 won't run. I remember I have been told not to use a variable as an array index. But I don't see any rationale for not using a variable the way I used in CODE 2. Please help me with it.
CODE 1
// practice_array_string.cpp
// this program demostrates arrays and c-strings
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int v;
[B]//char str[v];[/B]
cout << "enter string length: "; cin >> v;
[B]char str[v];[/B]
cout << "enter string: "; cin >> str;
cout << str << endl;
system("pause");
return 0;
}
CODE 2
// practice_array_string.cpp
// this program demostrates arrays and c-strings
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int v;
[B]char str[v];[/B]
cout << "enter string length: "; cin >> v;
cout << "enter string: "; cin >> str;
cout << str << endl;
system("pause");
return 0;
}


Sign In
Create Account


Back to top









