-2

Script

#include <iostream>  
using namespace std;

int main()  
{  
    float nums[3];  
    nums[0]=1.5; 
    nums[1]=2.75; 
    nums[2]=3.25;                      
    const char* name[5]={"m", "i", "k", "e", "\0"};
    int coords[2][3]={{1, 2, 3}, {4, 5, 6}};     
    cout << "nums[0]: " << nums[0] << endl; 
    cout << "nums[1]: " << nums[1] << endl; 
    cout << "nums[2]: " << nums[2] << endl;
    cout << "name[0]: " << name[0] << endl; 
    cout << "Text String: " << name << endl; 
    cout << "coords[0][2]: " << coords[0][2] << endl;  
    cout << "coords[1][1]: " << coords[1][1] << endl; 
    return 0; 
}

Output:

nums[0]: 1.5  
nums[1]: 2.75  
nums[2]: 3.25  
name[0]: m  
Text String: 0xffffcbb0 
coords[0][2]: 3  
coords[1][1]: 5

Question:

How can I make the "Text String: " say "mike"
I tried char name[5],
I tried char* name[5];
Please help
Thank You
I'm new to c++, don't judge

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
MBJH
  • 1,571
  • 3
  • 17
  • 36
  • 1
    you cannot, at least without some working. You have an array of 5 null terminated strings (char*, actually), so there is no built in operator << which automatically combine them. use `std:string`, combine all the strings and ouput the resulting string. Maybe you want to deal with a simple `char*` (null terminated). try this: `const char* name="mike";` – Gian Paolo Aug 21 '17 at 23:20
  • 1
    `const char* name = "mike";` <- pointer to nul terminated string literal. `const char name[]="mike";` <- nul terminated array of characters (the size is inferred at compile time). `const char* name[5]={"m", "i", "k", "e", "\0"};` <- an array of pointers to nul terminated strings. So in the last case `cout << name;` will output the address of the pointer to the first literal in the array, and `cout << *name;` as well as `cout << name[0];` will output `m` as `*name` will evaluate to be a `char*` to a nul terminated string. – George Aug 21 '17 at 23:28
  • If you want to learn the C++ here is a nice list of [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). In my experience C++ is a world of joy. Both as a profession and as a hobby. – Ron Aug 21 '17 at 23:53

3 Answers3

3
const char* name[5]={"m", "i", "k", "e", "\0"};

Is an array of pointers to characters. You have unintentionally created an array of pointers to strings rather than a string.

cout of a pointer results in the address being printed, except in the case of a pointer to a character where the << operator has been written to assume a null-terminated c-style string.

To fix this, you can

1 Use a pointer to a string literal

const char* name="mike";

2 Use an array of characters

const char name[5]={'m', 'i', 'k', 'e', '\0'};

Or 3 use a combination of the two

const char name[5]="mike";

As an added bonus, because in the latter two cases the initialization implies the size of the array you can leave it out,

const char name[]="mike";

and the compiler will figure it out. Very handy when you change from "mike" to "henry" as you don't have to adjust the size.

When you are done this exercise, take some time and read up on std::string. Use it wherever possible instead of C-Style strings. It will save you an amazing amount of time, blood, and tears.

user4581301
  • 33,082
  • 7
  • 33
  • 54
1
const char name[5]={'m', 'i', 'k', 'e', '\0'};

What you need is an array of chars.

Character literals are written using single quotes ('). If you use double quotes ("), you get a string literal instead.

melpomene
  • 84,125
  • 8
  • 85
  • 148
0

Using your notation, change the double quotes to single quotes and get rid of the asterisk.:

const char name[5]={'m', 'i', 'k', 'e', '\0'};

Or you could use:

const char name[] = "mike";
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154