I just started learning C++, and this test seemed like a good idea so i tried doing it, doesn't seem to work, and it really doesn't make sense why (to me).
#include <iostream>
using namespace std;
int myNum = 5; // Integer (whole number without decimals)
double myFloatNum = 5.32543; // Floating point number (with decimals)
char myLetter = 'H'; // Character
string myText = "test text: test"; // String (text)
bool myBoolean = true; // Boolean (true or false)
int main() {
cout << myNum << endl;
cin >> myNum >> endl;
cout << myFloatNum << endl;
cin >> myFloatNum >> endl;
cout << myLetter << endl;
cin >> myLetter >> endl;
cout << myText << endl;
cin >> myText >> endl;
cout << myBoolean << endl;
cin >> myBoolean >> endl;
return 0;
}
It does not make sense to cin
something into endl
. cin
is a stream to get data from, but the endl
is a thing to end the line, as @arsdever commented.
Simply remove it, and your code will compile:
#include <iostream>
#include <string> // You forgot to include that header, for using std::string
using namespace std;
int myNum = 5;
double myFloatNum = 5.32543;
char myLetter = 'H';
string myText = "test text: test";
bool myBoolean = true;
int main() {
cout << myNum << endl;
cin >> myNum;
cout << myFloatNum << endl;
cin >> myFloatNum;
cout << myLetter << endl;
cin >> myLetter;
cout << myText << endl;
cin >> myText;
cout << myBoolean << endl;
cin >> myBoolean;
return 0;
}
Although, you may want to first read the user's input, and then print it. Now, you print the predefined by you value of the variable (and then print an end of line), and then read the input from the user for that specific variable.