this is what I did, knowing that NUL comes after any char array. Did I do something wrong here?
First this 'NUL' is a multi-character constant, not a null character.
Second a "null" character is not automatically placed at the end of an array of char.
Third in C/C++ the "null" character is '\0' not 'NUL'.
Fourth be careful about incrementing your loop control variable (i) outside the loop body as this will increment the variable multiple times and in this loop which could cause you to access the array out of bounds.
Fifth Why aren't you using std::strings instead of the more dangerous character arrays?
#include <iostream>
usingnamespace std;
int main()
{
constint MAXSIZE = 101;
char text[MAXSIZE];
constint COLS = 2;
cout << "Input some text (maximum " << MAXSIZE - 1 << " characters): ";
cin.getline( text, MAXSIZE ); // shouldn't overrun character array; extracts '\n' from the stream
int i= 0; // start of text
while ( text[i] != '\0' ) // loops until null terminator
// while ( text[i] ) // alternative - works just as well
{
cout << text[i]; // output character
if ( ( i + 1 ) % COLS == 0 ) cout << '\n'; // start a newline when it reaches a multiple of COLS
i++; // increment counter
}
cout << '\n';
}
Input some text (maximum 100 characters): This is Manchester; we do things differently here.
Th
is
i
s
Ma
nc
he
st
er
;
we
d
o
th
in
gs
d
if
fe
re
nt
ly
h
er
e.
#include <iostream>
usingnamespace std;
int main()
{
constint MAXSIZE = 101;
char text[MAXSIZE];
constint COLS = 2;
cout << "Input some text (maximum " << MAXSIZE - 1 << " characters): ";
cin.getline( text, MAXSIZE ); // shouldn't overrun character array; extracts '\n' from the stream
int counter = 0;
char *p = text; // point to start of text
while ( *p ) // loops until it hits the null terminator
{
cout << *p; // output character
if ( ++counter % COLS == 0 ) cout << '\n'; // start a newline when it reaches a multiple of COLS
p++; // increment pointer to point to next position
}
cout << '\n';
}
#include <iostream>
usingnamespace std;
int main() {
char s1[102];
cin >> s1;
for (int i = 0; i < 101; i++) {
if (s1[i] == '\0') {
break;
}
else
cout << s1[i];
i++;
if (s1[i] == '\0') {
break;
}
else
cout << s1[i];
cout << endl;
}
return 0;
}
thank you
jlb
for your third tip!
Third in C/C++ the "null" character is '\0' not 'NUL'.
Also,
ne555
for line 4
if (s1[101] == 'NUL') {
I was trying to set parameter of input thinking that if the word was longer than 100alphabets
the 101th s1 would have something other than NULL or '\0' in it.
So I just thought using an "if statement" to check whether the 101th s1 has NULL or not could help the code to not work when more than 100alphabets were entered.
jonnin
I think I will try your way out to shorten my code! thanks!
lastchance
A whole lot of things I don't quite get yet, but still, I will try to do my research!
Thankyou for all the // you added to help me understand!
Like I said, I am completely new to this whole coding concept, and I have no idea about most of the statement priority or settings(?). I am also not learning code in an orthodox way haha ;) I guess I'm trying out concepts one by one in a weird order haha.
if you are learning, get away from character array strings as fast as you can, use string type instead. Its ok for your first few programs, but the sooner you get away from them, the better off you will be.