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

string multiply(int num) {

  int multiple = 0;
  int multiplied = 0;
  string num2;

  if(num<=10){

    while (num!=0){

      for(multiple=0;multiple<10;multiple++){
        multiplied = num*multiple;
        std::string num1 = to_string(multiplied);
        std:string space = " ";
        num2 += num1 + space;
      }

    }
    return num2;
    cout << num2 << endl;

  }

}

int main(){

int num = 5;

multiply(num);

}

This is the code I have come up with in C++, it compiles but it doesn't do anything, I am using VSCODE with windows wsl and atom editor, I am trying to learn the rules and operations in C++. Can anyone give me ideas . I also tested with many cout statements and no idea what its doing,

Thanks

shas_0002
  • 3
  • 1
  • Your return statement is before you output anything. Swap the `return num2;` and `cout << num2 << endl;` lines. – 1201ProgramAlarm Feb 02 '20 at 03:34
  • Your only output statement `cout << num2 << endl;` can never be reached, because you return directly before reaching it. Your compiler should have given you a warning for that. Also it should be warning you that not all paths in the function have a `return` statement. Please don't ignore any compiler warnings and fix all of them. See also https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings. – walnut Feb 02 '20 at 03:35
  • Initialize `string num2 {};`. Refactor your code to check `if (!num || num > 10) { handle error/return }`. Then just `for (multiple = 0; multiple < 10; multiple++) {...}; cout << num2 << endl; return num2;` – David C. Rankin Feb 02 '20 at 03:42
  • @DavidC.Rankin Thank You – shas_0002 Feb 02 '20 at 03:51

1 Answers1

1
return num2;
cout << num2 << endl;

The value is returned before it can be printed.

Also, the condition is while (num!=0), but num is never changed here, and therefore it enters infinite loop.

The code below will gives output:

#include <iostream>

using namespace std;

string multiply(int num) {

  int multiple = 0;
  int multiplied = 0;
  string num2;

  if(num<=10){

    //while (num!=0){

      for(multiple=0;multiple<10;multiple++){
        multiplied = num*multiple;
        std::string num1 = to_string(multiplied);
        std:string space = " ";
        num2 += num1 + space;
      }

    //}
    cout << num2 << endl;
    return num2;


  }

}


int main()
{
   int num = 5;

    multiply(num);

   return 0;
}

And the output is: 0 5 10 15 20 25 30 35 40 45

GoldenArcher
  • 812
  • 1
  • 9
  • 12
  • @tedlyngmo no problems, my problems was already closed, I am looking for a place to accept the answer, if you tell me where I can accept it – shas_0002 Feb 02 '20 at 04:11
  • @tedlyngmo Done, there were good answers too but they were comments only – shas_0002 Feb 02 '20 at 04:23
  • @shas_0002 Comments are comments and can't be accepted as answers. This time you only got one answer and it seemed to help you to solve the problem, so accepting it is the right thing to do. I'm removing my advice to accept the answer btw. :) – Ted Lyngmo Feb 02 '20 at 04:24
  • @shas_0002 When I first took coding class in college and always went to find instructor for the questions I thought were very hard at that time, and my instructor told me, add some more print statement. I still find that comment helpful today. :) Also, you can also check how to debug in vsc. Debug is very important as many times you can fall into logical error cannot be detected by reviewing code directly. – GoldenArcher Feb 02 '20 at 08:43