SAcoder
SAcoder

Reputation: 61

How to put strings and numbers together in a multidimensional array?

Difference from other questions

Well first, I am a beginner, and I couldn't understand at least two questions. Secondly, one of the questions I looked at applied this concept in a completely new way. They had other variables and functions that were clouding my view of their code.

Goal

I am using SoloLearn as a C++ interpreter and I can't seem to get a string in a multidimensional array that has numbers in it. So I would appreciate you guys telling me what I need to fix so that there are no error messages when I run my code.

Code

#include <iostream>
using namespace std;

int main()
{
    int numberArray [2] [3] = {{1, 2, 3}, {4, 5, "null"}};
    return 0;
}

Error message

..\Playground: In function 'int main()':

..\Playground:6:57: error: invalid conversion from 'const char*' to 'int' [-fpermissive] int numberArray [2] [3] = {{1, 2, 3}, {4, 5, "null"}};

Tries

I have tried to change int into string, but this is what showed up on my screen: Compilation error. I don't want anything to show up on my screen. I also tried to change int into bool, but all that showed up was this: No output..

Upvotes: 1

Views: 67

Answers (1)

WillW
WillW

Reputation: 81

First, the reason that "string" does't work is that you're not including string as a package. Put

#include <string>;

below the other include directive

Secondly, I don't think you can mix types in C++

I agree with the approach you started on - making the multiD array "string"

load all as strings (i.e. load a 4 as "4")

When reading this array, check the entry in question using the C++ function atof

You'll need to include #include <stdlib.h>

atof will return a float if the ascii is a float number

otherwise will throw an exception

you can distinguish between numbers and text that way

Upvotes: 3

Related Questions