Computing.Net > Forums > Programming > C++ string size change

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ string size change

Reply to Message Icon

Name: djas
Date: July 16, 2005 at 05:53:15 Pacific
OS: Windows XP Pro SP2
CPU/Ram: P4 2.40 GHZ 256 RAM
Comment:

I edited this code from a book but can't get it to work no that i've changed it.

<code>
#include <iostream>

using namespace std;

int main()
{

int strsiz;
cout << "Enter String Size: ";
cin >> strsiz;
char string[strsiz];
cout<<"Please enter a long string: ";
cin.getline (string,strsiz, '\n' );
cout<<"Your long string was: "<< string<<endl;
cin.get();
}
</code>

Any thoughts anyone?
Thanx

djas



Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: July 16, 2005 at 08:48:46 Pacific
Reply:

char string[strsiz];

When declaring an array, you cannot use a variable. The value to indicate the size of the array must be available at compile time.

It appears that you want to allocate the string array during runtime. To do that, you must allocate the memory for the string array during runtime. Something like this:

cin >> strsiz;
char *string;
string = new char[strsiz];


Be sure to come back and let us know if our suggestions helped!


0
Reply to Message Icon

Related Posts

See More


NOT or ALL BUT html video



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ string size change

c++ string manipulation question www.computing.net/answers/programming/c-string-manipulation-question/11524.html

c++ string trouble pls help www.computing.net/answers/programming/c-string-trouble-pls-help/6206.html

C strings conversion www.computing.net/answers/programming/c-strings-conversion/10336.html