LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-27-2005, 02:37 PM   #1
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
Question about inserting a character into a 2dimensional sequence.


I've been trying to figure out how I would be able to replace a character in my 2 dimensional array that I have with another character. Heres the code...

#include <cstdlib>
#include <cstdio>
#include <iostream>
using namespace std;

class Map
{
public:
char* MapA[10][10];
void mkmap()
{
for(int i = 0;i < 10;i++)
{
MapA[i][10] = "OOOOOOOOOO";
cout << MapA[i][10] << endl;
}
return;
}
};

int main(int nNumberofArgs, char* pszArgs[])
{
Map mp;
mp.mkmap();
mp.MapA[0][10] = "A";
cout << endl << endl;
for (int i = 0;i < 10;i++)
{
cout << mp.MapA[i][10] << endl;
}
return 0;
}

... Now on line 25, I believe that is where I have to change in order to replace a character without erasing the entire line. I have tried...

mp.MapA[0][1] = "A"; but to no prevail. How do I change a character in a sequence without resorting to erasing the entire line? Thanx in advance!

--EDIT--
Sry, here is the output...

george@linux:~> ./Array
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO


A
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
OOOOOOOOOO
george@linux:~>

Last edited by RHLinuxGUY; 11-27-2005 at 02:39 PM.
 
Old 11-27-2005, 04:17 PM   #2
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,450

Rep: Reputation: 63
array element [0][10] is outside what you specifyed for your array size.


you inlized an array with 10 elements so the elements are 0 - 9 not 0 - 10

the element [0][10] would be the 11th element not the 10th one.


besides this the other problem you have here is when you intlize the array.


char * creates a read only array. Now it really does more then that but thats the simplist way to think about it.


should be intlized like

char space[10][10];

your not creating an array of pointers but rather an array of strings. in the example above.


theres more going on with this code then that but the core of the problem is with the intlization and the out of bounds stuff. Comple with -Wall with gcc and this would have warned you about such actions.


Last edited by exvor; 11-27-2005 at 04:33 PM.
 
Old 11-27-2005, 05:07 PM   #3
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
The reason why I changed that char was to make it const (?) as said in this output without a const(?) char. (or a pointer char, correct me please)

george@linux:~> g++ Array.cpp -o Array
Array.cpp: In member function `void Map::mkmap()':
Array.cpp:14: error: assignment of read-only location
Array.cpp:14: error: invalid conversion from `const char*' to `char'
Array.cpp: In function `int main(int, char**)':
Array.cpp:23: error: structure `mp' with uninitialized const members
Array.cpp:25: error: assignment of read-only location
Array.cpp:25: error: invalid conversion from `const char*' to `char'
george@linux:~>

... putting char* Map[][]; fixed that problem and enabled me to run the program.
 
Old 11-27-2005, 05:26 PM   #4
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,450

Rep: Reputation: 63
Yea i got the same error when i tried to compile your code with the minor changes. also your code dident generate gcc errors oddly


Im better with C then with C++ so other then that i dont think i can be much help. Ill try to make a similar pice of code in C and see if i can do it there.
 
Old 11-27-2005, 07:49 PM   #5
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
here is the c++ way to do it (with as few changes to your code as possible).. the string class takes away much of your work.. if you end up needing a char*, to can use the c_str() member function of string......

Code:
class Map
{
    public:
        string MapA[10];
        void mkmap(){
            for(int i = 0;i < 10;i++){
                MapA[i] = "OOOOOOOOOO";
                cout << MapA[i] << endl;
            }
        }
};

int main(int nNumberofArgs, char* pszArgs[])
{
    Map mp;
    mp.mkmap();
    mp.MapA[0][5] = 'A';
    cout << endl << endl;
    for (int i = 0;i < 10;i++){
        cout << mp.MapA[i] << endl;
    }
    return 0;
}
 
Old 11-27-2005, 10:07 PM   #6
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
Thank you xhi, that was helpful. I'll need to research into chars a little more, tho, I still don't understand why it wasn't working probably with my code before? Can anyone explain for future reference?
 
Old 11-27-2005, 10:52 PM   #7
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
now I am assumin on you intentions here
that you wanted an array of char 10x10 ... right..
so like this
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx

each x is char right...
well when you did
>> char* MapA[10][10];

you ended up with a 10x10 array of pointers... so when you did
>> MapA[i][10] = "OOOOOOOOOO";
you were actually getting

xxxxxxxxx"OOOOOOOOOO"
xxxxxxxxx"OOOOOOOOOO"
xxxxxxxxx"OOOOOOOOOO"
xxxxxxxxx"OOOOOOOOOO"
xxxxxxxxx"OOOOOOOOOO"
xxxxxxxxx"OOOOOOOOOO"
xxxxxxxxx"OOOOOOOOOO"
xxxxxxxxx"OOOOOOOOOO"
xxxxxxxxx"OOOOOOOOOO"
xxxxxxxxx"OOOOOOOOOO"
xxxxxxxxx"OOOOOOOOOO"

where an x is representing an uninitialized ptr and the "ooooo" are string literals.. now this is a perfectly valid data structure, however the reason it was not working is because you cannot change a string literal... you have to either do some clever copies on it, or run through it char by char until you get to where you want, then add char, then finish out the string... or just use arrays and run through char by char

again changed as little as possible...
Code:
#include <cstdlib>
#include <cstdio>
#include <iostream>
using namespace std;

class Map
{
    public:
    char MapA[10][10];
    void mkmap(){
        for(int i = 0;i < 10;i++){
            for(int j=0; j<10; ++j){
                MapA[i][j] = 'O';
                cout << MapA[i][j];
            }
            cout << endl;
        }
    }
};

int main(int nNumberofArgs, char* pszArgs[])
{
    Map mp;
    mp.mkmap();
    mp.MapA[4][5] = 'A';
    cout << endl << endl;
    for (int i = 0;i < 10;i++){
        for(int j=0; j<10; ++j){
            cout << mp.MapA[i][j];
        }
        cout << endl;
    }
    return 0;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting Text wildcat22 Linux - General 8 04-25-2004 07:51 AM
character code and keyboard input question blish_blash Programming 1 04-24-2004 06:09 PM
How do I map a key sequence to a specific character? QtCoder Linux - General 0 11-28-2003 12:03 PM
Inserting modules wgodois Linux - Hardware 0 08-03-2003 09:37 AM
how to echo special character question ArnaudVR Linux - Software 1 06-29-2003 11:11 AM


All times are GMT -5. The time now is 02:59 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration