Add '\n' to 10th position in char array

kay there's a big back story but to cut to the chase, for one part of my program I have to allow the user to type whatever they want (up to 60 characters, I completed that part) but after the 10th character it goes to a new line. I have looked everywhere and I cannot find a good way to solve this.

This is what I have out so far and I get infinie loop:

//Set the line limit to 10 characters
char d;
int j = 0;
char stu[] = "Example sentence to test isspace\n";
while (stu[j])
{
d = str[j];
if (j = 11)
d = '\n';
putchar(d);


j++;

}

Also, I just joined this forum five minutes ago so heyyy
@duroncoles

Try changing if (j = 11) to if (j == 11) // (double ==)

A single = is to assign, whereas a double ==, is to compare.
Okay thank you! That worked, but now what I also need to figure out is not how to make character 11 a '\n' but to ADD it in there and move every character over 1. Because right now it prints:

"Example sen
ence to test isspace"

but obviously what I want is for it say:

"Example sen
tence to test isspace"
@duroncoles

Well, the best way would be, is to NOT let d equal the newline, but just cout a newline.

Like so..
1
2
3
4
5
6
7
while (stu[j])
{
 if (j == 11)
   cout << '\n';
 cout << stu[j];
   j++;
}
Last edited on
Topic archived. No new replies allowed.