LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   simple C++ Problem :) (https://www.linuxquestions.org/questions/programming-9/simple-c-problem-295843/)

indian 02-28-2005 09:06 AM

simple C++ Problem :)
 
Guys I am trying to compile this problem in g++..but is giving all kind of Errors...there is soem problem with the line cin.getline(arr,max)
But I am not able to understand that what is wrong..

Code:


#include<iostream>

using namespace std;
int main()
{
    int max=100;
    char arr[max];

    cin.getline(arr,max);
    cout << arr;
}


deiussum 02-28-2005 09:27 AM

The problem is actually this part:

Code:

int max=100;
char arr[max];

You cannot use a non-const variable to create an array on the stack in this way. Try change it to:

Code:

const int max=100;
char arr[max];

Edit: Just tested the code with g++ w/o using const and it worked, probably because the compiler was smart enough to know that the size was defined beforehand. Your problem may actually be that you are using gcc to compile instead of g++. gcc is a C compiler, g++ is the C++ compiler.

indian 02-28-2005 09:42 AM

Thansk alot...yes the problem was Not putting CONST before int ..:D thanks alot :)


All times are GMT -5. The time now is 03:02 AM.