0
#include <iostream>
#include <cstring>
using namespace std;

void getInput(char *password, int length)
{
    cout << "Enter password: ";
    cin >> *password;
}
int countCharacters(char* password)
{
    int index = 0;
    while (password[index] != "\0")
    {
        index++;
    }
    return index;
}
int main()
{
    char password[];
    getInput(password,7);
    cout << password;
    return 0;
}

Hi! I'm trying two things here which I'm unable to do atm. I'm trying to create a char array with unspecified length in main, and I'm trying to count the number of words in the char array in the function countCharacters. But password[index] doesnt work.

EDIT: I'm doing a homework assignment, so I have to use cstrings only. EDIT2: And I'm not allowed to use the "strlen"-function either.

3
  • There's simply no such thing as an array of unspecified length. It is possible to do with dynamically allocated memory, but you should just use std::string to encapsulate this. Commented Mar 19, 2013 at 10:37
  • What is the length parameter of getInput function? It's not used there. Commented Mar 19, 2013 at 10:43
  • The password length is 7. It is to be the limit of the password length. In my assignment I am to take the length as an argument in the getInput, and limit the input to 6 symbols + "\0". But how? Commented Mar 19, 2013 at 10:47

1 Answer 1

1

At first replace

char password[];

by

char password[1000]; // Replace 1000 by any maximum limit you want

And then replace:

cin >> *password;

by

cin >> password;

Also instead of "\0" you should put '\0'

P.S. There is no char array with unspecified length in C++, you should use std::string instead(http://www.cplusplus.com/reference/string/string/):

#include <string>

int main() {
    std::string password;
    cin >> password;
    cout << password;
    return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'm doing a homework assignment, so I have to use cstrings only.

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.