1

I am trying to write a program that stores char arrays of names.

This is my code

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

char **names;
char *input_name;

int main() {
    names = new char*[10];
    for(int i=0; i<10; i++){
        names = new char[60];
        cout << "Input name" << i << ": \n";
        cin >> input_name;
        strcpy(names[i],input_name);
        cout << names[i] << "\n";
    }
    return 0;
}

Firstly I am getting the cannot convert ‘char*’ to ‘char**’ in assignment names = new char[60]; error.

Also, getting the invalid conversion from ‘char’ to ‘const char*’ [-fpermissive] strcpy(names[i],input_name); error

I would greatly appreciate it if someone could modify my code and help me out

Thanks

5
  • 2
    Shouldn't names = new char[60]; be names[i] = new char[60];? The other errors are side-effect of the previous error. Commented Oct 28, 2014 at 22:08
  • 1
    Good luck to you if the name is greater than 59 characters. Commented Oct 28, 2014 at 22:11
  • @alvits Thanks :), but I'm still getting errors in terms of input. My code is on IDEOne - ideone.com/PAMwgw Commented Oct 28, 2014 at 22:11
  • You have input_name uninitialized. In your case, it will be easier just to make it a simple string instead of a pointer. char input_names[60];. Commented Oct 28, 2014 at 22:17
  • Why use C++ at all if you don't want to use std::string? Commented Oct 28, 2014 at 22:39

2 Answers 2

2

It's names[i] = new char[60]; instead of names = new char[60]; And you forgot to init input_name with input_name = new char[60];

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

char **names;
char *input_name;

int main() {
    names = new char*[10];
    input_name = new char[60];
    for(int i=0; i<10; i++){
        names[i] = new char[60];
        cout << "Input name" << i << ": \n";
        cin >> input_name;
        strcpy(names[i],input_name);
        cout << names[i] << "\n";
    }
    return 0;
}

As you are using c++ you may should consider using std::string instead of char*. As mentioned by PaulMcKenzie in the comments you get into trouble when a name is longer than 59 characters . Plus std::string is more convenient IMO.

Sign up to request clarification or add additional context in comments.

3 Comments

Why does my program crash if I don't do input_name = new char[60];? My program crashes at cin >> input_name; strcpy(names[i],input_name)
you just declared what input_name should be, but never assigned a value to it. As it is a pointer, it is pointing to an invalid memory address.
@H4kor This code still crashes my program. I modified the line names = new char*[10] to names = new char*[value] so that the user can input a number of names. If the user wants to input 5 names it works well, however if the user wants to input 12 names it crashes
0

The code contains lots of memory leaks! Any data actually newed should be deleted. Note that the form of delete needs to match the form of new, i.e., when an array object was allocated, an array object needs to release using, e.g., delete[] names.

When you read into a char array you need to make sure the amount of data in the array isn't exceed, you can limit the number of characters to be read by setting the stream's width, e.g.:

if (std::cin >> std::setw(60) >> names[i]) {
    // OK - do something with the data
}
else {
    // failed to read characters: do some error handling
}

Of course, in the code snippet you posted you try to reading into input_name which points nowhere: this will result in undefined (probably some crash).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.