So I've been working on a program that takes a string and then puts it in an i * 6 char array but I can't seem to get it to work, any help with the problem would be much appreciated.

Here is the unsatisfactory code I have so far:

#define WIDTH 6
#include <iostream>
#include <string>
using namespace std; 

int i,n,m;

int main ()
{
  string r = "abcdefghijklmnopqrstuvwx"; 
  
   
  if (r.length() <= 36 && r.length () >= 31 ){
  char array [] = {r[0],r[6],r[12],r[18],r[24],r[30],r[1],r[7],r[13],r[19],r[25],r[31],r[2],r[8],r[14],r[20],r[26],r[32],r[3],r[9],r[15],r[21],r[27],r[33],r[4],r[10],r[16],r[22],r[28],r[34],r[5],r[11],r[17],r[23],r[29],r[35]};
  cout << array << "\n";
  }
  if (r.length() <= 30 && r.length () >= 25 ){
  char array [] = {r[0],r[6],r[12],r[18],r[24],r[1],r[7],r[13],r[19],r[25],r[2],r[8],r[14],r[20],r[26],r[3],r[9],r[15],r[21],r[27],r[4],r[10],r[16],r[22],r[28],r[5],r[11],r[17],r[23],r[29]};
  cout << array << "\n";
  }
  if (r.length() <= 24 && r.length () >= 19 ){
  char array [] = {r[0],r[6],r[12],r[18],r[1],r[7],r[13],r[19],r[2],r[8],r[14],r[20],r[3],r[9],r[15],r[21],r[4],r[10],r[16],r[22],r[5],r[11],r[17],r[23]};
  cout << array << "\n";
  }
  if (r.length() <= 18 && r.length () >= 13 ){
  char array [] = {r[0],r[6],r[12],r[1],r[7],r[13],r[2],r[8],r[14],r[3],r[9],r[15],r[4],r[10],r[16],r[5],r[11],r[17]};
  cout << array << "\n";
  }
  if (r.length() <= 12 && r.length () >= 7 ){
  char array [] = {r[0],r[6],r[1],r[7],r[2],r[8],r[3],r[9],r[4],r[10],r[5],r[11]};
  cout << array << "\n";
  }
  system ("pause");
  return 0;
}

try null terminating the different versions of array before you try to print them to the screen. I don't believe the compiler will automatically to that for you if you explicitly initalize elements of the array.

try null terminating the different versions of array before you try to print them to the screen. I don't believe the compiler will automatically to that for you if you explicitly initalize elements of the array.

Well I'm a bit of a noob but I assume you mean '\0'?
The code above does work, but not for all cases and is laborious. I was hoping to be able to do something more like this:

int main()
{
string r;
string p = "abcdefghijklmnopqrstuvwx";
int i = p.length();
int s = i/6;


int z = i % 6;
r = p + string(z, '.');
char a[s][6] = 
{
// Here is where I wanted to simply put the name of a string and, because I already specified the width of the array, the compiler would simply fill out the array accordingly
};

system ("pause");
return 0;
}

So use loops. The first assignment:

{r[0],r[6],r[12],r[18],r[24],r[30],
 r[1],r[7],r[13],r[19],r[25],r[31],
 r[2],r[8],r[14],r[20],r[26],r[32],
 r[3],r[9],r[15],r[21],r[27],r[33],
 r[4],r[10],r[16],r[22],r[28],r[34],
 r[5],r[11],r[17],r[23],r[29],r[35]};

can be written as:

x = 0
for i = 0 to 5
  for j = 0 to 30 by 6
    array[x] = r[i + j)
    x = x+ 1
  next j
next i

Look for the pattern in the other assignments.

So use loops. The first assignment:

{r[0],r[6],r[12],r[18],r[24],r[30],
 r[1],r[7],r[13],r[19],r[25],r[31],
 r[2],r[8],r[14],r[20],r[26],r[32],
 r[3],r[9],r[15],r[21],r[27],r[33],
 r[4],r[10],r[16],r[22],r[28],r[34],
 r[5],r[11],r[17],r[23],r[29],r[35]};

can be written as:

x = 0
for i = 0 to 5
  for j = 0 to 30 by 6
    array[x] = r[i + j)
    x = x+ 1
  next j
next i

Look for the pattern in the other assignments.

I'm afraid I have no idea what you are suggesting, I tried messing with your pseudo-code but I get it to work. One of the main problems I am having is defining something as being a multiple of 6 and also specifying a group of characters in an array. If anyone can nod me in the right direction I would be very grateful.

I'm afraid I have no idea what you are suggesting, I tried messing with your pseudo-code but I get it to work.

Since I can't see what you tried, I can't point out what you did wrong.

Since I can't see what you tried, I can't point out what you did wrong.

Sorry, this is what I tried:

#include <iostream>
#include <string>

using namespace std;

int main()
{
string r;
string p;

cout << "Enter the message to be coded." << "\n";
getline (cin, p);
int i = p.length();
int s = i/6;

size_t found;
found = p.find_first_of(' ');
while (found!=string::npos)
{
p[found]='*';
found = p.find_first_of(' ',found+1);
}

int z = i % 6;
r = p + string(z, '.');
char o[] = "Helloe";
char a[s][6];
int x = 0;
int j;
for (i = 0; i < 5; i){
  for (j = 0; j < 30;j){ //couldn't understand the by 6 part of your suggestion
    a[x] = r[i + j]; //this line shows "error 32 incompatible types in assignment of `"
    x += 1;
  j++;
}
i++;
}


system ("pause");
return 0;
}

If it's completely not what you meant, I'm sorry, a little new to this

the string library is amazing.

create an array of the size of the string then loop through the string and copy each element into the array

#include <iostream>
#include <string>
#include "windows.h"

using namespace std;

int main()

{

string imastring = "i am a string";
char array[imastring.size()];

for (int i = 0; i < imastring.size() ; i++)

{
array[i] = imastring.at(i);
cout << array[i];
}

cout << imastring ;


system("pause");

}

Sorry, this is what I tried:

for (i = 0; i < 5; i){
  for (j = 0; j < 30;j){ //couldn't understand the by 6 part of your suggestion
    a[x] = r[i + j]; //this line shows "error 32 incompatible types in assignment of `"
    x += 1;
  j++;
}
i++;
}

If it's completely not what you meant, I'm sorry, a little new to this

No, it's not quite.

First, please learn to format your code. If the code is difficult to follow, it's difficult to help. I like the way you format each line, but the indentation is extremely important throughout the program. Be consistent.

Second, in the statement for (i = 0; i < 5; i) , what is the last useless i for? That should be i++. Yes, I see it at the bottom of the loop, but that's not where it goes. All you have is a while loop using for Third, my code said for j = 0 to 30 [B]by 6[/B] . You do that by adding 6 to j: for (j = 0; j < 30; j[B]+=6[/B]) Last, x += 1; is better written as x++; -- but you probably know that.

@ lexusdominos

Thanks, but I can already imitate the code you posted. What I am trying to do is make an x*6 char array from a string(it will always be divisible by 6). And then I want to be able to output the new string by reading in columns(top to bottom), not rows.

The first code I posted gives me the output I want but only for strings up to 36 characters and is very laborious.

Again any help is greatly appreciated

No, it's not quite.

First, please learn to format your code. If the code is difficult to follow, it's difficult to help. I like the way you format each line, but the indentation is extremely important throughout the program. Be consistent.

Second, in the statement for (i = 0; i < 5; i) , what is the last useless i for? That should be i++. Yes, I see it at the bottom of the loop, but that's not where it goes. All you have is a while loop using for Third, my code said for j = 0 to 30 [B]by 6[/B] . You do that by adding 6 to j: for (j = 0; j < 30; j[B]+=6[/B]) Last, x += 1; is better written as x++; -- but you probably know that.

So I took your advice(I assumed the j++ was not neccessary now that I have j+=6 ) and this is what I came up with, but it produced the same error:

#include <iostream>
#include <string>

using namespace std;

int main()
{
string r;
string p;

cout << "Enter the message to be coded." << "\n";
getline (cin, p);
int i = p.length();
int s = i/6;

size_t found;
found = p.find_first_of(' ');
while (found!=string::npos)
{
p[found]='*';
found = p.find_first_of(' ',found+1);
}

int z = i % 6;
r = p + string(z, '.');
char a[s][6];
int x = 0;
int j;
for (i = 0; i < 5; i++){
  for (j = 0; j < 30; j+=6){ 
    a[x] = r[i + j]; //this line shows "error 32 incompatible types in assignment of `"
    x++;
  cout << a << endl;
}

}


system ("pause");
return 0;
}

To account for this (because the real root of the problem is making a 2d array) I changed the line to

a[x][6] = r[i + j];

Now this compiled but it had a runtime error the first time. I ran it again and got nonsense. Any ideas?

Thanks for sticking with me.

Yes, format your code! If it's not formatted next time, you're on your own. I already gave you a link to look at.

If your definition is char a[s][6]; and you changed the assignment to a[x][6] = r[i + j]; , I would expect it to blow up. You are writing beyond the limits of the array. The second index can only go from 0 to 5. 6 is beyond the range.

1) Determine what x is.
2) Create a two dimensional array of size x by 6 using dynamic memory. I don't believe this syntax: char a[s][6]; is valid, yet.
3) Read input string into the array one char at a time using a nested loop to control access to the array elements (outer loop controls row, inner loop controls column)
4) Create a new string by adding individual letters from the above array to the end of the new string using a nested loop to control access to the array elements. (outer loop controls column, inner loop controls row)
5) Display new string (be sure to null terminate new string, if needed).

@Lerner
I don't think I'm at the stage yet where I can just take your instructions and roll with them, just not good enough yet.
Why isn't char a[s][6] valid? I'm simply defining the size of the array.
Though I will try, and i appreciate it.

@WaltP

I think I've followed the formatting rules fully and here's the code I have.

#include <iostream>
#include <string>

using namespace std;

int main()
{
        string r;
        string p;

        cout << "Enter the message to be coded." << "\n";
        getline (cin, p);

        int i = p.length();
        int s = i/6;
        int z = i % 6;
        int x = 0;
        int j;
        char a[s][6];

        size_t found;

        found = p.find_first_of(' ') ;

        while (found != string::npos)
        {
               p[found] = '*';
               found = p.find_first_of(' ', found+1);
        }


        r = p + string(z, '.');

        for (i = 0; i < 5; i++){
                for (j = 0; j < 30; j+=6){ 
                    a[x][6] = r[i + j]; 
                    x++;
                    cout << a << endl;
                    }

}


system ("pause");
return 0;
}

My confusion is coming from the x variable and the array size.

I don't understand what x is, unless it's a count for the columns, and every time x increments, we're moving to the next column.

With the array size, does char[s][6] specify 6 wide, or 7 wide? Does this mean to make the array 6 characters wide I should change the 6 to a 5?
Because I know that it starts at 0, but I assumed that for size it's different and

I also have continually returned outputs similar to this: 0x22fea0
And was wondering what causes it.

Once more, thank you.

I think I've followed the formatting rules fully and here's the code I have.

Except for the TABs to SPACEs part... Otherwise, it looks much better.

My confusion is coming from the x variable and the array size.

I don't understand what x is, unless it's a count for the columns, and every time x increments, we're moving to the next column.

Call me dense, but if you don't understand what x is, why is it in your code? Since you wrote it, you need to know what each variable is.

With the array size, does char[s][6] specify 6 wide, or 7 wide? Does this mean to make the array 6 characters wide I should change the 6 to a 5?
Because I know that it starts at 0, but I assumed that for size it's different and

Why would it be 7? It's a 6. Reread what I posted last time, and check it with your book. Why aren't you looking this stuff up in your text?

I also have continually returned outputs similar to this: 0x22fea0
And was wondering what causes it.

You never initialized any values, so there's garbage in the variable.

Run this:

#include <iostream>
using namespace std;
int main()
{
    int v[20];
    int i;

    for (i=0; i<20; i++)
        cout << v[i] << endl;
    return 0;
}

What values do the uninitialized array contain?

char a[s][6];

s is an int variable. It needs to be a const int variable. See this example in WaltP's post:

int v[20];

20 is a const int, or an int literal maybe, but it amounts to the same thing. s is not, though. I have read that the next version of the standards may allow for non const ints to be used when declaring arrays in C++, though I have no first hand knowledge of that impending change. As it stands now, if you don't know the size of the array at the time of compilation, then you need to use dynamic memory rather than static memory to declare the array. Dynamic memory uses the keywords new[] and delete[]. If you haven't run into using dynamic yet, and you need to be able to use an array of unknown size in your program, then I encourage you to look up dynamic memory in your favorite reference (textbook, class handout, Google, whatever).

For simplicity sake, let's say your string is

char word[7] = "editor";

and you want to break it up into 3 pieces of two char each. To load the string into the 3 by 2 array you could do something like this:

char array[3][2];
int j = 0;
for(int row = 0; row < 3; row++)
  for(int col = 0; col < 2; col++)
    array[row][col] = word[j++];

and to print out the first column followed by the second you could do this:

for(int col = 0; col < 2; col++)
  for(int row = 0; row < 3; row++)
    cout << word[row][col];

which should put eiodtr to the screen, not as a new string, but as group of individual characters all on the same line.

I'll leave it up to you to adapt that explanation and example to help you complete your project.

@WaltP

X is there because you wrote it in the code you had in your reply.

I though it might be 7 because in arrays the counting is: 0,1,2,3,4,5,6,; making 6 the 7th integer. That's why I had 5 in before, it's the 6th integer. Also, I don't have a text, I'm not in some sort of class. I do this as a challenge.

I don't really understand what you mean by I never intialized any variables.

The values it returns are:
1367821597
-2
2001180890
2001248506
4788024
4787832
2293528
2001248029
0
0
2147315712
180
172
3023400
43
2
2293544
2001247796
2293608
4198592

@lerner

Thanks for that, I'll give it a shot

can anyone give me any suggestions?

The values it returns are:
1367821597
-2
2001180890
2001248506
4788024
4787832
2293528
2001248029
0
0
2147315712
180
172
3023400
43
2
2293544
2001247796
2293608
4198592

That shows you exactly what I mean. If you don't set the initial value of a variable -- ANY variable -- to a value, you have no idea what the value is. Saying int x; does not make x a 0. It could be anything.

So when you use a value, unless you initialize it in some manner ( x = 3; for example) your data is unknown. Print it, you get junk. Use it ( b = x*3; ) and you get junk.

Note to others: Yes I know about global variables -- ignoring them....

Oh right, I see.

Anyway, I got it working, but not like the way I was suggesting here, I simply dealt with the string column by column.

Thanks for all your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.