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
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>
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!
Summary: I have a basic C++ string question. Let's say that a function reads a string of any size from a data file or user. I need the function to reduce the amount of characters in that string by 1, from the ...
Summary: What im trying to do is this. (this is for a school project). i am using visual c++. What i am doing is this. i ask the user for a number from which i use to make a dynamic array. I have 2 main values...
Summary: thanks alot for the helps from you guys. I have modified the program so as to allow it to do the string conversion when I changed the array size to a smaller value, say for instance, 30, instead of 10...