0
votes

Before you light the torches, I am trying to teach myself C++. I feel like I've got a good grasp of it, but I gotta dive in to reach the bottom if you know what I mean. I am trying to debug a simple program I am trying to make to teach myself. This programs goal is to alphabetically sort an array of strings in print each one out. I probably should use pointers in here when passing the arrays to my length() function but I haven't gotten that far yet. There are so many errors that I am not sure where the error actually is. Please tell me it's something like a missing semi-colon. I am pretty sure that the if (itemsToSort[j] < firstInAZOrder) line won't work but again, I am trying to learn the language and I have to start somewhere.

main.cpp:

#include <iostream>
#include "stringarray.h"

void output (int sortedItems) {
    for (int i = 0; ; i++) {
        std::cout << sortedItems[i] << std::endl;
    }
}

int main (int argc, char * const argv[]) {
    std::string itemsToSort[] = {
        'bob', 'john', 'tyler', 'anthony', 'luke', 'eric'
    };
    std::string sortedItems[length(itemsToSort)];
    string firstInAZOrder;

    for (int i = 0; i < length(itemsToSort); i++) {
        firstInAZOrder = itemsToSort[i];

        for (int j = i + 1; j < length(itemsToSort); j++) {
            if (itemsToSort[j] < firstInAZOrder) {
                firstInAZOrder = itemsToSort[j];
            }
        }

        sortedItems[i] = firstInAZOrder;
    }

    output(sortedItems);
    return 0;
}

stringarray.cpp:

int length (std::string array) {
    return sizeof(array) / sizeof(array[0]);
}

stringarray.h:

int length (std::string array);

Here is the list of errors and warning Xcode gave me:

  • /Users/Tyler/Desktop/sort/main.cpp:12:3: warning: multi-character character constant -
  • /Users/Tyler/Desktop/sort/main.cpp:12:18: warning: character constant too long for its type
  • /Users/Tyler/Desktop/sort/main.cpp:12:27: warning: character constant too long for its type
  • /Users/Tyler/Desktop/sort/main.cpp: In function 'void output(int)':
  • /Users/Tyler/Desktop/sort/main.cpp:6: error: invalid types 'int[int]' for array subscript
  • /Users/Tyler/Desktop/sort/main.cpp: In function 'int main(int, char* const*)':
  • /Users/Tyler/Desktop/sort/main.cpp:13: error: invalid conversion from 'int' to 'const char*'
  • /Users/Tyler/Desktop/sort/main.cpp:13: error: initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator]'
  • /Users/Tyler/Desktop/sort/main.cpp:13: error: invalid conversion from 'int' to 'const char*'
  • /Users/Tyler/Desktop/sort/main.cpp:13: error: initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator]'
  • /Users/Tyler/Desktop/sort/main.cpp:13: error: invalid conversion from 'int' to 'const char*'
  • /Users/Tyler/Desktop/sort/main.cpp:13: error: initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator]'
  • /Users/Tyler/Desktop/sort/main.cpp:13: error: invalid conversion from 'int' to 'const char*'
  • /Users/Tyler/Desktop/sort/main.cpp:13: error: initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator]'
  • /Users/Tyler/Desktop/sort/main.cpp:13: error: invalid conversion from 'int' to 'const char*'
  • /Users/Tyler/Desktop/sort/main.cpp:13: error: initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator]'
  • /Users/Tyler/Desktop/sort/main.cpp:13: error: invalid conversion from 'int' to 'const char*'
  • /Users/Tyler/Desktop/sort/main.cpp:13: error: initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator]'
  • /Users/Tyler/Desktop/sort/main.cpp:14: error: conversion from 'std::string*' to non-scalar type 'std::string' requested
  • /Users/Tyler/Desktop/sort/main.cpp:15: error: 'string' was not declared in this scope
  • /Users/Tyler/Desktop/sort/main.cpp:15: error: expected `;' before 'firstInAZOrder'
  • /Users/Tyler/Desktop/sort/main.cpp:17: error: conversion from 'std::string*' to non-scalar type 'std::string' requested
  • /Users/Tyler/Desktop/sort/main.cpp:18: error: 'firstInAZOrder' was not declared in this scope
  • /Users/Tyler/Desktop/sort/main.cpp:20: error: conversion from 'std::string*' to non-scalar type 'std::string' requested
  • /Users/Tyler/Desktop/sort/main.cpp:26: error: 'sortedItems' was not declared in this scope
  • /Users/Tyler/Desktop/sort/main.cpp:29: error: 'sortedItems' was not declared in this scope
  • /Users/Tyler/Desktop/sort/stringarray.cpp:10: error: 'string' is not a member of 'std'
  • /Users/Tyler/Desktop/sort/stringarray.cpp:10: error: expected ',' or ';' before '{' token
3

3 Answers

3
votes

First of all:

std::string itemsToSort[] = {
    'bob', 'john', 'tyler', 'anthony', 'luke', 'eric'
};

The quote character (') can only be used on a single character. Use double-quotes (") for this:

std::string itemsToSort[] = {
    "bob", "john", "tyler", "anthony", "luke", "eric"
};

Next:

void output (int sortedItems) {
    for (int i = 0; ; i++) {
        std::cout << sortedItems[i] << std::endl;
    }
}

This function takes a single integer, and that's not what you want at all. Assuming you want to learn how to use pointers, you could try something like:

void output (int* intArray, int arraySize) {
    for (int i = 0; i < arraySize; ++i) {
        std::cout << intArray[i] << std::endl;
    }
}

This function takes a pointer to some number of integers in memory, and the number that are there. This solves the next problem of knowing when your for loop should terminate.

In real code you would use something like std::vector in place of int*.

4
votes

A string constant should be in double quotes, not single quotes:

"bob"

Single quotes are for character constants.

Then,

int length (std::string array) {
    return sizeof(array) / sizeof(array[0]);
}

should take an array as input, but even then won't work and can't be fixed except by making it a macro (which is a sure way to get you banned from the C++ experts' guild for a decade) or by making length a template function, which may be a bit advanced. Use std::vector instead, it has a size method for this.

Finally,

void output (int sortedItems) {
    for (int i = 0; ; i++) {
        std::cout << sortedItems[i] << std::endl;
    }
}

doesn't really take an int, does it? A stop condition in the loop would also be nice, to prevent the proverbial dragons flying out your nose.

0
votes

Just fix the errors one at a time -

The first one is because you need double quotes (") not single quotes (')

The second one - try int *sortedItems

Just keep redcuing them one at a time, after it compiles, the debugger will be your friend