-4

I 'm new to C++ and really likely I'm doing something wrong which cause some random numbers appearing at the console before I enter a value of user input` this is the code:

    #include <iostream>    
    using namespace std;
    
    int zad_1(int K,int N){
       cout << "Enter the value of K  : " << K ;
       cin >> K;
       cout << "Enter the value of N  : " << N ;
       cin >> N;
    
       return K, N;
    }
    
    
    int main(){
       int K, N;          

       cout << zad_1(K, N) << endl;
       return 0;
    }    

The console looks like before entering the user input "Enter the value of K : 4200347" 4200347 this number is appearing on the console before I even enter any input

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
jak_es
  • 19
  • 3
  • 5
    Functions can only return a single value. There are also many other very basic mistakes in the shown code, which leads me to recommend that you might need to invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), and learn the basics of C++. – Some programmer dude Mar 29 '21 at 12:53
  • 1
    1) `cout << "Enter the value of K : " << K ;` Here you print the value of `K`, which is, at the time of printing - uninitialized, leading to undefined behavior, by reading such, uninitialized, value. Likewise for `N`. 2) `return K, N;` would be equivalent to `return N;`, in this case. – Algirdas Preidžius Mar 29 '21 at 12:54
  • 1
    `return K, N;` does not what you think it does. It's equivalent to `return N;`. Look uop what the comma operator does in C. – Jabberwocky Mar 29 '21 at 12:56

1 Answers1

3

The output Enter the value of K : 4200347 is normal for this line:

cout << "Enter the value of K  : " << K;

This line does following:

  1. it outputs Enter the value of K :
  2. then it outputs K. As K has not yet been initialized, it's content is undetermined and it happens to be 4200347.

Furthermore you cannot return multiple values like this:

return K, N;

This is equivalent to:

return N;

Lookup what the comma operator does. This is for the C language, but the comma operator works the same way in C than in C++.


You probably want something like this:

#include <iostream>    
using namespace std;

void zad_1(int & K, int & N) {          // pass parameters by reference, not by value
  cout << "Enter the value of K  : ";
  cin >> K;
  cout << "Enter the value of N  : ";
  cin >> N;
}

int main() {
  int K, N;
  zad_1(K, N);                              // call function, this will modify  K and N
  cout << "K=" << K << "  n=" << N << endl; // print K and N 
  return 0;
}

Actually there is a way to return two values from a function using std::pair, but this is probably a too advanced topic for you right now.

And just a remainder in case that's not clear: the names N and K in the zad_1 function are totally unrelated to the names Nand K in main. Your main function could be this:

int main() {
  int x, y;
  zad_1(x, y);                              // call function, this will modify  x and y
  cout << "x=" << x << "  y=" << y << endl; // print x and y
  return 0;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115