cout is not working

even after using namespace std cout is not working

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  #include <stdio.h>
using namespace std;

int main() {
    int martialarts(int arr[],int arrSize){
        int max = arr[0];
    int min = arr[0];
    for(int i=0;i>arrSize;i++){
        if(arr[i]>max){
            max = arr[i];
        }else if(arr[i]<min){
            min = arr[i];
        }
    }
    return max;
    return min;
    }
    
int arr[] = {1,4,6,7,8};
int arrSize = sizeof(arr)/sizeof(arr[0]);
martialarts(arr,arrSize);
cout << max;
cout << min;

}
Can you do a hello world program in C++?
Last edited on
 
#include <stdio.h> 

This is a C header file, written in the C way.
cout is in #include <iostream>


1
2
int main() {
    int martialarts(int arr[],int arrSize){

C++ doesn't have nested function definitions.

1
2
    return max;
    return min;

Nor can you return two values. Well not like that anyway.
Maybe pass min and max as two reference parameters.

 
martialarts(arr,arrSize);

And you're ignoring the return result anyway.

I suspect this is a "Console Disappears" problem.

Either:

(1) Run your program from the command console, or

(2) Add cin.ignore(~0UL, '\n'); to the end of your main function so that the console does not disappear before you press Enter.
There are several problems with the code:

1. as others have mentioned <stdio.h> is a C header, not a C++ header.

2. std::cout is part of the <iostream> header.

3. can't define a non lamba function within another function. main is a function.

4. a function can only return one value, not two. need 2 (or more) values returned, pass variables to hold the values as references or pointers, and the function return type should be changed to void.

5. your for loop at line 8 is ill-formed, your condition part (i > arrSize) is wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <iterator>

void martialarts(int arr[], int arrSize, int& max, int& min)
{
   for (int i { }; i < arrSize; i++)
   {
      if (arr[i] > max)
      {
         max = arr[i];
      }

      if (arr[i] < min)
      {
         min = arr[i];
      }
   }
}

int main()
{
   int arr[]  { 1, 4, 6, 7, 8 };
   // int arrSize = sizeof(arr) / sizeof(arr[0]);
   int arrSize { std::size(arr) };

   int max { };
   int min { 65556 };

   martialarts(arr, arrSize, max, min);

   std::cout << max << '\n';
   std::cout << min << '\n';
}

Passing a C++ container, such as a std::vector, is easier since the container retains its size. A vector's size can be retrieved using the std::vector::size member function.
http://www.cplusplus.com/reference/vector/vector/size/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <vector>

void martialarts(const std::vector<int>& arr, int& max, int& min)
{
   for (size_t i { }; i < arr.size(); i++)
   {
      if (arr[i] > max)
      {
         max = arr[i];
      }

      if (arr[i] < min)
      {
         min = arr[i];
      }
   }
}

int main()
{
   std::vector<int> arr  { 4, 6, 1, 7, 8 };

   int max { };
   int min { 65556 };

   martialarts(arr, max, min);

   std::cout << max << '\n';
   std::cout << min << '\n';
}
even after using namespace std cout is not working

It might help you to spend some time poking around Learn C++ and get a handle on some more of the C++ language basics.

https://www.learncpp.com/
Topic archived. No new replies allowed.