Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,916 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,595 people online right now. Registration is fast and FREE... Join Now!




char array declaration problems

2 Pages V  1 2 >  
Reply to this topicStart new topic

char array declaration problems

polymath
9 May, 2008 - 05:13 PM
Post #1

D.I.C Regular
Group Icon

Joined: 4 Apr, 2008
Posts: 407



Thanked: 4 times
Dream Kudos: 500
My Contributions
I am writing a program that writes a line to a string and allows for that string to be written to a char array. I use a constant, but it interprets it as zero. I don't get why it won't let me use this constant!

cpp

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main () {
string INITIALimput;
getline (cin, INITIALimput);
int stringlegnth;
stringlegnth = INITIALimput.size();
const int arraySize = (stringlegnth-1);
char imput[arraySize];
stringstream(INITIALimput) >> imput;
cout << imput;
cin >> imput;
return 0;
}


Only error(s) is/are in line 12, the array declaration
User is offlineProfile CardPM
+Quote Post

realNoName
RE: Char Array Declaration Problems
9 May, 2008 - 05:28 PM
Post #2

D.I.C Regular
***

Joined: 4 Dec, 2006
Posts: 299



Thanked: 5 times
My Contributions
constants cant change during run time... what you need to do is dynamically allocate space for the char


here is an example
CODE
#include <iostream>
using namespace std;

int main ()
{
    int size;
    char * myArray;

    cout << "Enter size:";
    cin >> size;

    myArray = new char[size];

    return 0;
}

User is offlineProfile CardPM
+Quote Post

DominationXVI
RE: Char Array Declaration Problems
9 May, 2008 - 05:29 PM
Post #3

New D.I.C Head
*

Joined: 2 Feb, 2008
Posts: 36

I believe you'll need to dynamically allocate storage for your array. stringlength is always going to be defined at runtime, so the compiler isn't going to know how much memory you'll need for your array when it compiles your code.
User is offlineProfile CardPM
+Quote Post

sno
RE: Char Array Declaration Problems
9 May, 2008 - 05:31 PM
Post #4

New D.I.C Head
*

Joined: 4 May, 2008
Posts: 17


My Contributions
im guessing that the compiler wont allow you to declare an array as a constant because the array wont be of any use unless you declare each value. If you have a constant array the array slots are gonna stay what that memory slot contains and not what you want.

what im trying to say is taht is you have a constant array you cannot usefully use that array because you cannot assign values to the array slots.

(and its input not imput tongue.gif)
User is offlineProfile CardPM
+Quote Post

DominationXVI
RE: Char Array Declaration Problems
9 May, 2008 - 05:43 PM
Post #5

New D.I.C Head
*

Joined: 2 Feb, 2008
Posts: 36

QUOTE(sno @ 9 May, 2008 - 06:31 PM) *

im guessing that the compiler wont allow you to declare an array as a constant because the array wont be of any use unless you declare each value. If you have a constant array the array slots are gonna stay what that memory slot contains and not what you want.

what im trying to say is taht is you have a constant array you cannot usefully use that array because you cannot assign values to the array slots.

(and its input not imput tongue.gif)


The array itself is not constant, only the array's size. However, thats not the issue here. OP's "constant" isn't really a constant at all, hence the problem. Need dynamic alloc
User is offlineProfile CardPM
+Quote Post

polymath
RE: Char Array Declaration Problems
9 May, 2008 - 05:51 PM
Post #6

D.I.C Regular
Group Icon

Joined: 4 Apr, 2008
Posts: 407



Thanked: 4 times
Dream Kudos: 500
My Contributions
What i'm looking for is an array in which you do not need to enter the size, but doesn't take up extraneous space. I also want to directly manipulate each "cell" of an array. Additionally, when implement the code above, and add a cout << *MyArray; it outputs an equal sign. I can't assign an identifier to the new char that is created, thus, i can't manipulate any values in the direct cell i store it in. Hope this makes sense.
User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Char Array Declaration Problems
9 May, 2008 - 06:02 PM
Post #7

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
I'm a bit confused as to what you are trying to do... You can directly access and modify a character in a string with operator[]. Plus, string already has built in support for C++ string to C style string (c_str()).

From what I do understand, you will not be able to store a array on the stack, since that number must be specified at compile time. You can't really get out of manual memory allocation, unless you use the string class, or a similar alternative.
User is offlineProfile CardPM
+Quote Post

DominationXVI
RE: Char Array Declaration Problems
9 May, 2008 - 06:05 PM
Post #8

New D.I.C Head
*

Joined: 2 Feb, 2008
Posts: 36

QUOTE(polymath @ 9 May, 2008 - 06:51 PM) *

What i'm looking for is an array in which you do not need to enter the size, but doesn't take up extraneous space. I also want to directly manipulate each "cell" of an array. Additionally, when implement the code above, and add a cout << *MyArray; it outputs an equal sign. I can't assign an identifier to the new char that is created, thus, i can't manipulate any values in the direct cell i store it in. Hope this makes sense.



The new operator returns a pointer to the newly created memory block. This pointer is what you use to manipulate your array. Like MyArray[2] or *(MyArray +2), u can use bracket notation or pointer arithmetic.
User is offlineProfile CardPM
+Quote Post

realNoName
RE: Char Array Declaration Problems
9 May, 2008 - 07:19 PM
Post #9

D.I.C Regular
***

Joined: 4 Dec, 2006
Posts: 299



Thanked: 5 times
My Contributions
another example

CODE
#include <iostream>
#include <string>
using namespace std;

int main ()
{
    string myString = "This is my text";
    int size = myString.size();
    //allocate space for the text that way you only use what memory you need
    char * myArray = new char[size];
    //copy text from myString into myArray
    strcpy(myArray,myString.c_str());

    //print every thing out to show its all the same
    cout << myString << endl << myArray << endl;
    for(int i=0; i<size; i++)
        cout << myArray[i];

    cout << endl;
    return 0;
}

User is offlineProfile CardPM
+Quote Post

polymath
RE: Char Array Declaration Problems
10 May, 2008 - 02:27 PM
Post #10

D.I.C Regular
Group Icon

Joined: 4 Apr, 2008
Posts: 407



Thanked: 4 times
Dream Kudos: 500
My Contributions
This works but only outputs the first word. Basic copying, once i get this to work i will replace it with character manipulation. Can somebody help?

My life would be so much easier if variables could be used to allocate memory... a kind of dynamicstatic memory, sadly not to be.

CODE

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main () {  
    string INITIALinput;  
    getline (cin, INITIALinput);  
    int stringlegnth;  
    stringlegnth = INITIALinput.size();  
    int arraySize = (stringlegnth-1);  
    char * input=new char[arraySize];
    stringstream(INITIALinput)>>input;
    char * output=new char[arraySize];
    int position=0;
    while (position<stringlegnth) {
        output[position]=input[position];
        position++;
    }
    cout << output;
    char exit;
    cin >> exit;
    return 0;
}

User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Char Array Declaration Problems
10 May, 2008 - 04:38 PM
Post #11

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
It's only printing out the first word, since that's all you telling it to do.

CODE
stringstream(INITIALinput)>>input;


Remember, for streams, the operator>> will separate each word, based on the spaces. For example, say you had his

CODE

string first;
string last;
cout << "Enter you first and last name\n";
cin >> first >> last;

cout << "First Name: " << first << "\n";
cout << "Last Name: " << last << "\n";


So, if the user type in "foo bar", the program would print out
First Name: foo
Last Name: bar

A much simplier way to copy the string is to use strcpy() or memcpy() (which is much more efficient, since you have the length of the string).

CODE
memcpy(input, arraySize, INITIALinput.c_str());


Another Note: int arraySize = (stringlegnth-1); is wrong. You want +1, not -1.
User is offlineProfile CardPM
+Quote Post

polymath
RE: Char Array Declaration Problems
10 May, 2008 - 04:55 PM
Post #12

D.I.C Regular
Group Icon

Joined: 4 Apr, 2008
Posts: 407



Thanked: 4 times
Dream Kudos: 500
My Contributions
memcpy works, except when i cout output it has a lot of extraneous chars at the end.

also, i found that param 2 and 3 should be switched for memcpy.

Thank you, i swear i'm almost there. one more bug!
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/1/08 07:59AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month