HelpQuestion about c++

Posts 1–8 of 8 · Page 1 of 1
Question about c++
Hi,

I've been wanting to code along time and recently just started reading a book about c++ how to start etc...

And i had to make some exercises now, my question is about a certain erxercise:

The following program contains several errors:
*/ Now you should not forget your glasses //
#include <stream>
int main
{
cout << "If this text",
cout >> " appears on your display, ";
cout << " endl;"
cout << 'you can pat yourself on '
<< " the back!" << endl.
return 0;
)

Resolve the errors and run the program to test your changes.

While i did get that outcome the solution was different than what i had altough it was the same. Here is mine and the solution :

MINE:

#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{

cout << endl << "If this text,"
<< endl << "appears on your display,"
<< endl << "you can pat yourself on"
<< endl << "The back!" << endl;
return 0;
}

SOLUTION:

#include <iostream>
using namespace std;
int main() {
cout << " If this text ";
cout << " appears on your display, ";
cout << endl;
cout << " you can pat yourself on "
<< " the back!" << endl;
return 0;
}

Would love to hear from you guys. Thanks alot if you help me out
I'd like to know if the one i did is wrong, or Not or why i would use the other one.

EDIT: I'm doing all this in visual studio 2018.
Quote Originally Posted by Trenw View Post
Hi,

I've been wanting to code along time and recently just started reading a book about c++ how to start etc...

And i had to make some exercises now, my question is about a certain erxercise:

The following program contains several errors:
*/ Now you should not forget your glasses //
#include <stream>
int main
{
cout << "If this text",
cout >> " appears on your display, ";
cout << " endl;"
cout << 'you can pat yourself on '
<< " the back!" << endl.
return 0;
)

Resolve the errors and run the program to test your changes.

While i did get that outcome the solution was different than what i had altough it was the same. Here is mine and the solution :

MINE:

#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{

cout << endl << "If this text,"
<< endl << "appears on your display,"
<< endl << "you can pat yourself on"
<< endl << "The back!" << endl;
return 0;
}

SOLUTION:

#include <iostream>
using namespace std;
int main() {
cout << " If this text ";
cout << " appears on your display, ";
cout << endl;
cout << " you can pat yourself on "
<< " the back!" << endl;
return 0;
}

Would love to hear from you guys. Thanks alot if you help me out
I'd like to know if the one i did is wrong, or Not or why i would use the other one.

EDIT: I'm doing all this in visual studio 2018.
the way you did it is fine and correct, thats how most of us honestly would do it to since its short little strings.
Quote Originally Posted by Trenw View Post
Hi,

I've been wanting to code along time and recently just started reading a book about c++ how to start etc...

And i had to make some exercises now, my question is about a certain erxercise:

The following program contains several errors:
*/ Now you should not forget your glasses //
#include <stream>
int main
{
cout << "If this text",
cout >> " appears on your display, ";
cout << " endl;"
cout << 'you can pat yourself on '
<< " the back!" << endl.
return 0;
)

Resolve the errors and run the program to test your changes.

While i did get that outcome the solution was different than what i had altough it was the same. Here is mine and the solution :

MINE:

#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{

cout << endl << "If this text,"
<< endl << "appears on your display,"
<< endl << "you can pat yourself on"
<< endl << "The back!" << endl;
return 0;
}

SOLUTION:

#include <iostream>
using namespace std;
int main() {
cout << " If this text ";
cout << " appears on your display, ";
cout << endl;
cout << " you can pat yourself on "
<< " the back!" << endl;
return 0;
}

Would love to hear from you guys. Thanks alot if you help me out
I'd like to know if the one i did is wrong, or Not or why i would use the other one.

EDIT: I'm doing all this in visual studio 2018.
You maybe read again what exactly std::endl does.
Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{

	cout << "If this text,\n"
	<< "appears on your display,\n"
	<< "you can pat yourself on\n"
	<< "The back!" << endl;
	return 0;
}
Quote Originally Posted by MikeRohsoft View Post


You maybe read again what exactly std::endl does.
Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{

	cout << "If this text,\n"
	<< "appears on your display,\n"
	<< "you can pat yourself on\n"
	<< "The back!" << endl;
	return 0;
}

Could you tell me why you wrote it with the \n" and only cout once?
Quote Originally Posted by Trenw View Post
Could you tell me why you wrote it with the \n" and only cout once?
its not the same but for now think of it like endl, continues the output on a new line so in the cmd these two will look the same

Code:
cout << "next\n" << "line";
cout << "next" << endl << "line;
Quote Originally Posted by MikeRohsoft View Post


You maybe read again what exactly std::endl does.
Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{

	cout << "If this text,\n"
	<< "appears on your display,\n"
	<< "you can pat yourself on\n"
	<< "The back!" << endl;
	return 0;
}
Hey man. I need your help about coding my generator. Can I send PM?
Quote Originally Posted by Trenw View Post
Could you tell me why you wrote it with the \n" and only cout once?
with this Message you only write me again "i still didn't read what std::endl does"
'\n' is ASCII Table 10 and is the UNIX NewLine Operator
And from the CPPReference about std::endl:
Inserts a newline character into the output sequence os and flushes it as if by calling os.put(os.widen('\n')) followed by os.flush().

This is an output-only I/O manipulator, it may be called with an expression such as out << std::endl for any out of type std::basic_ostream.
2 Questions @Trenw

1. Do you even know whats "#include "stdafx.h" for?

2. Stop "using namespace std;"
Posts 1–8 of 8 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us