Why can I seem to use outside the Arrays limit [10][10]? For instance if I use the following code, it assigns and outputs the arrays element.
Code:
code[1][50] = "outside array";
cout << code[1][50];
Why does an extra string get added on to sepstr in the seperate_string function?
Are their any other problems with my code?
Code:
#include <iostream>
#include <string>
#include <sstream>
#include <istream>
#define WIDTH 10
#define HEIGHT 10
using namespace std;
string code[HEIGHT][WIDTH];
void somefunc(void);
void add_double_quotes(string &str);
void clear_array(void);
void clear_ibuffer(void);
void seperate_string(string &str);
int main(void)
{
somefunc();
//cout << code[0][0] << code[1][0] << code[1][1] << endl;
//cout << code[3][0] << code[3][1] << code[3][2] << code[3][3] << code[3][4] << endl;
//clear_array();
return 0;
}
void somefunc(void)
{
int x;
string uinput;
cout << "1. group \t2. rank \n3. rank1 \t4. names \n0. quit\n" << endl;
do{
cin >> x;
clear_ibuffer();
switch(x)
{
case 1:
code[0][0] = "group ";
break;
case 2:
code[1][0] = "rank";
break;
case 3:
code[1][1] = "rank1";
break;
case 4:
getline(cin, uinput);
default:
x = 0;
break;
}
}while(x != 0);
seperate_string(uinput);
}
void add_double_quotes(string &str)
{
str = '\"' + str + '\"';
}
void clear_array(void)
{
for (int n=0;n<HEIGHT;n++)
for (int m=0;m<WIDTH;m++)
{
code[n][m] = "\0";
}
}
void clear_ibuffer(void)
{
cin.clear();
cin.ignore(100, '\n');
}
void seperate_string(string &str)
{
int x = 0;
istringstream iss(str);
do{
string sepstr;
iss >> sepstr;
++x;
add_double_quotes(sepstr);
//cout << sepstr << endl;
code[2][x] = sepstr;
}while (iss);
for (int a=0;a<x;a++)
{
cout << code[2][a] << endl;
}
}