Hi i am in need of an explanation to the following:

I have created a program to take in a string(stored in a character array).
With my string,i must pass it into a function and 2 functions i have created are stringLength and toUpperCase.(i know there are string functions to do these,it is just practice.)

The following code is what i have done for this,however i am not sure that i am doing this correctly,as when i call the first function it will give me the length,but then crash.

Similarly,when i pass in the array to my second function,it will print out the string in upper case,but random characters will sometimes follow after.Even though i have used the '\0' test in the for loop to stop it once it reaches this character.

What i am basically asking is,am i passing it the array correctly with the use of pointers because i have just ended up confusing myself so much that i cant see a way around it.

Thanks for any help.


MAIN()

#include <iostream>
#include "string.cpp"
using namespace std;


main(){

    const int max_chars =100;
    int length= 0;

    char* letters[max_chars + 1];

    String* string;

    string = new String();
    
    cout << "Enter string: " ;
    
    cin.getline(*letters, max_chars + 1, '\n');

    string->stringLength(*letters);
    

    string->toUpperCase(*letters);

}

Class implementation file

// Class implementation file

#include <iostream>
#include "String.h"
using namespace std;


String::String(){

    length = 0;
    letters[length];
}

// function to find length of string
void String::stringLength(char* _letters){

    //letters[length];
    //int length = 0;

    for(length = 0; _letters[length] != '\0'; length++)
    {
        
        letters[length] = _letters[length];
    }


    cout << "Length of string is " << length << endl;
}

// Function to covert string to uppercase characters.
void String::toUpperCase(char* _letters){


    int length;

    // puts driver program array into function array
    for(length = 0; _letters[length] != '\0'; length++)
    {
        letters[length] = _letters[length];
    }


    for(int i = 0; i < length; i++)
    {
        if( (letters[i]>=97) && (letters[i]<=122) ) // checking if character is lowercase.
        {
            letters[i] -= 32; // convert to uppercase.
        }
    }

    cout << letters ;
}
#ifndef STRING_H
#define STRING_H

class String {

    private:

        int length;
        char letters[];

    public:

        String();
        void toUpperCase(char* _letters);
        void stringLength(char* _letters);
   
};

#endif

Do you know why you're trying to use an array of pointers to char instead of an array of char?

main(){

    const int max_chars =100;
    int length= 0;

    char* letters[max_chars + 1];

    String* string;

    string = new String();
    
    cout << "Enter string: " ;
    
    cin.getline(*letters, max_chars + 1, '\n');

    string->stringLength(*letters);
    

    string->toUpperCase(*letters);

}

And main returns an int; be explicit.

What is this supposed to do?

String::String(){

    length = 0;
    letters[length];
}

I am using pointers because thats what i (probably wrongly) thought was the best way to pass arrays,because a pointer passes the address to the first element,instead of passing the whole array in?

letters[length] is just a way of counting the elements of an array and storing them in the variable length so i can print out the length of the string.

With the second function,convert toUpperCase,the string is printed out correctly but then with rendom ascii characters attached.So my '\0' is not working for some reason and as far as i can see it is correct.

I am using pointers because thats what i (probably wrongly) thought was the best way to pass arrays,because a pointer passes the address to the first element,instead of passing the whole array in?

You can't pass an array by value ("the whole array"). When you "pass an array", you instead pass a pointer to the first element. What I believe you are doing is writing to a random memory location pointed to by one of a pile of uninitialized pointers. If you want to write characters into an array, use an array.

letters[length] is just a way of counting the elements of an array and storing them in the variable length so i can print out the length of the string.

That line of code merely examines and discards the character at letters[length]. It does no counting.

With the second function,convert toUpperCase,the string is printed out correctly but then with rendom ascii characters attached.So my '\0' is not working for some reason and as far as i can see it is correct.

Perhaps the random memory locations you are writing to are being overwritten?

Thanks for the tip,im going to rewrite the code minus the use of pointers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.