NapoleonTheCake NapoleonTheCake - 2 months ago 7
C++ Question

C++ string array returns 'null'

I have a program to list all ASCII-characters and it always returns 'null' from a string array.

Here is my code:

#include <iostream>
#include <string>
using namespace std;

int main()
{
string name[128] = {"null","start of heading","start of text","end of text","end of transmission","enquiry","acknowledge","bell","backspace","horizontal tab","NL line feed (new line)","vertical tab","NL form feed (new page)","carriage return","shift out","shift in","data link escape","device control 1","device control 2","device control 3","device control 4","negative acknowledge","synchronous idle","end of trans. block","cancel","end of medium","substitute","escape","file separator","group separator","record separator","unit separator","Space","!","quote","#","$","%","&","'","(",")","*","+",",","-",".","/","0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?","@","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","[","back-slash","]","^","_","`","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","{","|","}","~","DEL"};

for (int count = 0; count < 128; count++) {
char out;
int i(0);

cout << "Hi! My name is '" + name[i] + "'." << endl;
cout << "And I look somehow like this: " + out << endl;

out++;
i++;
}
return 0;
}


How to fix this? Thanks.

================

Fixed. So the final code is:

#include <iostream>
#include <string>
using namespace std;

int main()
{
unsigned char out = 0;

string name[128] = {"null","start of heading","start of text","end of text","end of transmission","enquiry","acknowledge","bell","backspace","horizontal tab","NL line feed (new line)","vertical tab","NL form feed (new page)","carriage return","shift out","shift in","data link escape","device control 1","device control 2","device control 3","device control 4","negative acknowledge","synchronous idle","end of trans. block","cancel","end of medium","substitute","escape","file separator","group separator","record separator","unit separator","Space","!","quote","#","$","%","&","'","(",")","*","+",",","-",".","/","0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?","@","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","[","back-slash","]","^","_","`","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","{","|","}","~","DEL"};

for (int count = 0; count < 128; count++) {
cout << "Hi! My name is '" + name[count] + "'." << endl;
cout << "And I look somehow like this: " << out << endl;
cout << endl;
out++;
}
return 0;
}

Answer

Computers only ever do what you tell them to do.

name[i] is always name[0].

Did you mean name[count]? And remove i altogether: there's no point in duplicating the counter, as that allows bugs to creep in which is essentially what's happened in this case.

Futhermore the behaviour of your program is undefined; you are reading the value of out, which is never initialised. Note that if char is a signed type on your platform then the behaviour of out++ is undefined if out is 127.

Finally, cout << "And I look somehow like this: " + out << endl; is evaluated as cout << ("And I look somehow like this: " + out) << endl; due to operator precedence. The expression "And I look somehow like this: " + out is dubious to say the least, yet more undefined behaviour as out gets larger than the character array! Did you mean to use << rather than out?

Comments