During writing C++ console application or printing any string we require new line display in output.
There are different methods to display new line c++.
1) Using “\n” for new line c++
#include <iostream>
using namespace std;
int main() {
cout << "Welcome to MrCodeHunter!! \n";
cout << "This is new line in c++";
return 0;
}
Output:
Here if you see at the end of first line we have appended “\n” character in string. It will create new line. If you add two time “\n” then it will add next line after one more line gap.
#include <iostream>
using namespace std;
int main() {
cout << "Welcome to MrCodeHunter!! \n\n";
cout << "This is new line in c++";
return 0;
}
Mostly when we deal with strings then we use given method for new line in c++.
2) Using “std::endl” for new line c++
#include <iostream>
using namespace std;
int main() {
cout << "Welcome to MrCodeHunter!!"<<endl;
cout << "This is new line in c++";
return 0;
}
#include <iostream>
using namespace std;
int main() {
cout << "Welcome to MrCodeHunter!!"<<endl<<endl;
cout << "This is new line in c++";
return 0;
}
Here we have defined in starting that using namespace std; if we have not added then we have to use std::endl. If it is already added then no need to add it.
For your c++ application development , you can use any of this two methods for new line c++ implementation. Option-1 is using “\n” and Option-2 is using “std::endl”
For more C++ and programming articles you can visit our website read information.
Leave a Reply