Trouble with arrays

I'm new to code, and i am trying to do a program with arrays. I'm starting with imputing characters into an array, and then i try to transfer those characters into an multidirectional array(if thats what you call it).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main (){
char imputarray[10];

cout << "letters seperated by spaces:" << endl;
cin >> imputarray;

char mainarray[10][100];
int n = -1;

while(n>10) {n++;
	mainarray[n][0] = imputarray[n];
}
	cout << mainarray[1][0] << endl;}



when i try to test and see if it transfered by displaying one of the characters, it only returns whitespace. Why is that happening?

p.s. if anyone can tell me how to input characters into a multidirectional array, that would make my life a whole lot easier.
closed account (iw0XoG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main ()
{
    char imputarray[10];

    cout << "letters seperated by spaces:" << endl;
    for(unsigned i = 0 ; i != 10;cin >> imputarray[i++]);

    char mainarray[10][100];
    int n = -1 ;
   
    while(n<10) {
        n++;
        mainarray[n][0] = imputarray[n];
    }
    cout << mainarray[1][0] << endl;
    return 0;
}

http://www.cplusplus.com/doc/tutorial/arrays/
Last edited on
that doesn't work. It never puts the letters into the program, just returns when i hit return; theres no result with that code...
closed account (iw0XoG1T)
what doesn't work? you mean imputarray is not populated? or do you mean mainarray is not populated?
it doesn't allow me to imput the text. the imput array doesn't seem to be populated
closed account (iw0XoG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

int main ()
{
    char imputarray[10];

    cout << "letters seperated by spaces:" << endl;
    for(unsigned i = 0 ; i != 10;cin >> imputarray[i++]);
    cout << "show that imputarray is populated\n";
    for(unsigned i = 0; i != 10; i++) cout << imputarray[i]<< ' ';
    cout << endl;

    char mainarray[10][100];

    int n = -1 ;
    while(n<10) {
        n++;
        mainarray[n][0] = imputarray[n];
    }
    cout << "show that mainarray has been populated:\n";
    for(unsigned i = 0; i != 10; i ++)
        cout << mainarray[i][0] << ' ';
    cout << endl;
    return 0;
}
im having a hard time explaining what's happening, but the problem is still the same. are you running these programs yourself? if not, can you? its probably a very simple problem but i dont know how to explain what that problem is
closed account (iw0XoG1T)
I am sorry I don't understand what you want--yes I did run the above program and it worked as I expected. I would suggest that you read the link I posted above it is very simple but it explains arrays quite well.

post what you enter and post what you expect.

sorry I could not help.
I just ran the program, and i realized what was wrong. it only runs the program once 10 characters have been entered. how do you fix that? i want to be able to enter any series of characters that is ten or less in length and press enter and assign those values to imput array.
closed account (iw0XoG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

int main ()
{
    char imputarray[10];

    cout << "letters seperated by spaces:" << endl;
    char c;
    unsigned i = 0;
    while(i != 10) {
        cin.get(c);
        if( c == '\n')break;
        else if (isspace(c));
        else imputarray[i++] = c;
    }
    cout << "show that imputarray is populated\n";
    for(unsigned j = 0; j != i; j++) cout << imputarray[j]<< ' ';
    cout << endl;

    char mainarray[10][100];

    int n = -1 ;
    while(n<10) {
        n++;
        mainarray[n][0] = imputarray[n];
    }
    cout << "show that mainarray has been populated:\n";
    for(unsigned j = 0; j != i; j ++)
        cout << mainarray[j][0] << ' ';
    cout << endl;
    return 0;
}

I am sure this doesn't work as expected either because it will accept characters entered without spaces...but at least you may have something to work with--modify and make work the way you want.
Last edited on
works great, thanks for the help!
Topic archived. No new replies allowed.