0

I am sure there is a better way of doing this, but right now I'm stuck on why it doesn't work. I am very new at coding... and have been Googling for an hour trying to figure it out, and just can't. What am I doing wrong?

This is a Codewars challenge. One comment about the challenge said this:

"I didnt realize that I knew nothing about const keyword. Spent most of the time figuring out why I couldn't iterate through it or assign it to a string variable."

Maybe that's my problem too?

#include <iostream>
#include <string>

using namespace std;

std::string createPhoneNumber(const int arr[10]){
    
    string number;

    number.push_back('(');
    for (int i = 0; i < 2; i++) {
      number.push_back(arr[i]);
    }
    number.push_back(')');
    number.push_back(' ');
    for (int i = 3; i < 5; i++) {
      number.push_back(arr[i]);
    }
    number.push_back('-');
    for (int i = 6; i < 9; i++) {
      number.push_back(arr[i]);
    }

 return number;
}

int main() {
  int arraySend[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
  
cout << createPhoneNumber(arraySend);

return 0;
}
  • It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Debugging Guide](http://idownvotedbecau.se/nodebugging/) – NathanOliver Mar 08 '22 at 22:07
  • 3
    Hint: The integer `1` is not the character `'1'`. `std::string`s hold _characters_. – Drew Dormann Mar 08 '22 at 22:09
  • @DrewDormann Well, okay! I didn't know that. Didn't realize that "numbers" in strings weren't considered "numbers" but are characters. Thanks. – jeremyers1 Mar 08 '22 at 22:13
  • You should only push things of type `char` into your `string`. But you are pushing things of type `int`. Through some perhaps unexpected automatic behaviour, your `int` will get automatically converted to a `char`. But that means you are pushing characters with the ASCII codes: 1, 2, 3, 4, 5... Definitely not what you intended. You should search up how to convert an `int` to a `char` properly. (`'0' + myInt` probably does what you want, but there are [other ways](https://stackoverflow.com/questions/4629050/convert-an-int-to-ascii-character) too.) – Wyck Mar 08 '22 at 22:15
  • You'll have a much easier time if you put the `0` at the beginning of your `arraySend` rather than the end. – Wyck Mar 08 '22 at 22:18
  • 2
    Addendum: Your function skips over both `arr[2]` and `arr[5]`. – Drew Dormann Mar 08 '22 at 22:18
  • 2
    These challenge/competitive coding web sites, like Codewars, are mostly for highly skilled, experienced C++ hackers to spend some free time solving useless coding puzzles. Trying to learn C++ from them is like trying to learn ice-skating by playing a hockey game against a team consisting of Wayne Gretzky, Mario Lemiuex, Sidney Crosby, Mark Messier, Brian Leetch, and Phil Esposito. It's going to be far more productive to learn C++, first, using any number of reputable, edited, textbooks; and then try your luck at competitive coding or challenge/competition sites. – Sam Varshavchik Mar 08 '22 at 22:25
  • 1
    @jeremyers1 *and have been Googling for an hour trying to figure it out, and just can't.* -- As SamV noted, those "online competitive coding" websites assume you know the language well enough to solve their problems -- they are *not* to be used to learn C++. "Know the language well-enough" means that you hardly ever need to ask a question on StackOverflow concerning the C++ language, and you shouldn't be making mistakes as you're making now. You should be focusing on learning the language, and solve sane, simple/intermediate problems, not obscure brain teasers and the like. – PaulMcKenzie Mar 08 '22 at 22:40
  • Jacques Plante in net or Ken Dryden? – user4581301 Mar 08 '22 at 22:46

0 Answers0