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