-5
  1. first code
#include <studio.h>

int main() {
    std::cout << "apple" << endl << "banana";
}
  1. second code
#include <iostream>
using namespace std;

int main(void) {
    cout << "apple" << endl;
    cout << "banana" << endl;
}

Why am i wrong? i know the answer is second one, But i want to know the why my first code is wrong. Please help me!

hyewon
  • 1

3 Answers3

8

This first code is wrong because #include <studio.h> is the wrong header file. The correct header file for std::cout and std::endl is #include <iostream>.

It's also wrong because endl is in the std:: namespace. So even with the correct header file it should be std::endl

std::cout << "apple" << std::endl << "banana";
john
  • 85,011
  • 4
  • 57
  • 81
0

You forgot to put std:: before endl in the first case.

Sajid19
  • 21
  • 2
0

take a look at the doc: https://en.cppreference.com/w/cpp/io/manip/endl

as you can see, 2 important things are related to the error/question you posted:

  1. endl is defined in header <ostream>
  2. it is in the namespace std

so it must be used as std::endl note that the ostream is a parent of the iostream so including the iostream guaranties you have access to the ostream

enter image description here

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • 1
    omg... what a kind explanation ! I didn't understand ios but I just used it. Now i understand basic definition of ios thanks to you (: – hyewon Feb 04 '21 at 08:14