Weird Symbols when outputting a char array

First off I know there are better ways to deal with strings but in my C++ class the professor wants us to store them in a char array. When trying to output the array char by char it gives me this strange output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "pch.h"
#include <iostream>
using namespace std;

int main(){
	char firstName[10];

	cout << "Enter your FIRST name:" << endl;
	cin >> firstName;

	for (int i = 0; i < 10; i++) {
		if (firstName[i] != '/0') cout << firstName[i];
	}

	system("pause");
}


OUTPUT: https://imgur.com/a/NGwvfZo
It's really not a strange output since you're printing past the end of the string. Your compiler should be warning you about potential problems:

||=== Build: Debug in c++homework (compiler: GCC 8-1) ===|
main.cpp warning: multi-character character constant [-Wmultichar]|
main.cpp||In function ‘int main()’:|
main.cpp warning: comparison is always true due to limited range of data type [-Wtype-limits]|
||=== Build finished: 0 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|

The problem is that '/' is not the proper escape character.

Why are you printing out each element of the array instead of just printing out the C-string?

By the way you should never use a method that doesn't limit the number of characters it will try to retrieve when dealing with C-strings. The insertion operator can be limited by using the setw() manipulator with the proper size.


cout << firstname; //what you are trying to do. cout is overloaded to do this loop for you.
or
don't use escaped zero, just use zero. use
blah blah firstname[i] != 0; //why type 4 letters for 0 ? its just clutter.
you may need to do this kind of loop on occasion (maybe to print it one letter per row going down instead of normally) so seeing the loop isnt a bad thing. Though mostly you should be using string not char*.
Last edited on
Well Outputing the specific chars was just a test, I have to reverse the chars but wanted to figure out how to omit the weird symbols first. Hopefully someone can help me there
Last edited on
wanted to figure out how to omit the weird symbols first.


You create an array of ten char.

None of those char are specifically set; they will be some weird value.

Looks like this:
 
<weird value> - <weird value> - <weird value> - <weird value> - <weird value> - <weird value> - <weird value> - <weird value> - <weird value> - <weird value>


Then you are taking in the users name, and writing that input and the number zero into the array.

Let's say the user enters the name "BRADY". Now what does the array look like? Like this:

<B> - <R> - <A> - <D> - <Y> - <0> - <weird value> - <weird value> - <weird value> - <weird value>

So what happens when you execute this loop?

1
2
3
	for (int i = 0; i < 10; i++) {
		if (firstName[i] != '/0') cout << firstName[i];
	}


You write out EVERYTHING, except the single zero value. So what's the output?
<B> - <R> - <A> - <D> - <Y> - <weird value> - <weird value> - <weird value> - <weird value>

So, you tell us, how do you avoid outputting the weird values?

I managed to fix it, apparently I didnt initiate the array properly, changing it to 'char firstName[10] = {};' fixed it.
Did you fix the problem with your if() statement?

Did you use something to limit the number of characters retrieved?

If you didn't do these two things then you really didn't "fix" your problems. You will still be printing the whole array not just the string (now you're just printing a "non-printable" character), and you still have possibilities of buffer overflows.
Yes I changed the if to look for ' ' and I did limit the chars thanks
Why are you looking for a space character? You should be looking for the end of string character, '\0' or 0.
to print reverse it, you might find strlen() useful.
eg for(i = strlen(var)-1; i; i--) //iterate backwards over the string.
you don't need the zero char at all, strlen finds it for you and gets you the info you need.


Topic archived. No new replies allowed.