The following piece of code gives me the error: "too many initializers for 'char []'" :
int main()
{
int input;
char numbers[] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
std::cin >> input;
std::cout << ((input > 9) ? "Greater than 9" : numbers[input-1]) << std::endl;
}
What's needed for this to work is for numbers
to be a pointer variable, i.e:
char * numbers[] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
I'm new to c++ and I'm trying to understand why this array needs to be a pointer and what exactly is happening in memory which requires this to be a pointer?
In other languages such as Java you can do the following:
import java.util.Scanner;
public class Playground
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();
String[] numbers = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
System.out.println((input > 9) ? "Greater than 9" : numbers[input-1]);
}
}
No Pointers are required here, and from my understanding there's no need for them in this kind of scenario either.