-1

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;

}
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    You can't `cin` to `endl`. `cin` is a stream to get data from, but the `endl` is a thing to end the line. – arsdever Dec 14 '19 at 18:09
  • So do i just rewrite the code without the "<< endl" on the cin lines of code, to make it work –  Dec 14 '19 at 18:11
  • 1
    Yes and also have a look at the answer you have. – arsdever Dec 14 '19 at 18:12
  • @arsdever can you please take a look at my answer? I think I have fixed OP's problems, by removing `endl` when asking for input, and adding the `string` header.. – gsamaras Dec 14 '19 at 18:24

2 Answers2

3

You forgot to include <string>, string isn't a basic C++ datatype; use #include <string> after iostream, without the spaces after the greater than and less than signs.

David G
  • 94,763
  • 41
  • 167
  • 253
  • @JadenGarcia this won't fix the problem of the OP, your answer is correct but not complete. I posted the other half in my [answer](https://stackoverflow.com/a/59337852/2411320). :) – gsamaras Dec 14 '19 at 18:35
  • @gsamaras His compiler told him that the problem was the `<<` operator, he hadn't made it to the `cin >> endl` at all yet, so I only told him what was necessary. Also, @arsdever already mentioned that, so why would I say it again? –  Dec 15 '19 at 05:55
  • Hi @JadenGarcia, thank you for pointing that out for the OP. Indeed, there was a comment mentioning the core problem of OP's code, but comments are not answers. In general, for the sake of future readers, we like to gather the complete information for solving the problem in the question, into compact and robust answers. I hope this makes sense to you, and please keep in mind that this is just an advice, not something that I'd like to point the finger towards you (sorry if it felt like that, I didn't really want to). :) – gsamaras Dec 15 '19 at 11:25
1

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.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • I've done that and it still only outputs "5" into the cmd, as the result of the code. Also thanks in clarifying some of the things i didn't know. –  Dec 14 '19 at 18:15
  • Hi @Luka, did you try to run my code exactly? I highly doubt it that you see only 5 printed. See it also in the online compiler (answer updated). – gsamaras Dec 14 '19 at 18:18
  • hi @gsamaras i did try it yes, and it does output 5 into the cmd for me :( –  Dec 14 '19 at 18:22
  • Hmm, that's strange. You see though the output complete in the online demo, right @Luka? – gsamaras Dec 14 '19 at 18:23
  • Yeah i have, and it doesn't make sense.. , it does compile without any errors and it does run and output "things", but only and integer 5 –  Dec 14 '19 at 18:25
  • Could you please append a screenshot of that behavior in your question @Luka? – gsamaras Dec 14 '19 at 18:27
  • https://prnt.sc/qaujvf , there it is, I just don't seem to get why is it like this :P –  Dec 14 '19 at 18:32
  • Could that be a peculiarity of Dev-C++ @Luka? Maybe it hasn't refreshed the workspace or something. I am not sure though. How about deleting the project and creating a new, fresh and clean project with the corrected source code in it? – gsamaras Dec 14 '19 at 18:34
  • Okay i'll try that, i've done it now, seems like the same story again (https://prnt.sc/qaum1d).. –  Dec 14 '19 at 18:35
  • @Luka it waits for your input, that's why it waits at 5. Type a number, and you'll see! :) – gsamaras Dec 14 '19 at 18:46
  • Hmm, why is it like that, i mean why does it work like that and how could i possibly make it so it prints it without inputing anything (also it works thanks https://prnt.sc/qauqgl :) ) –  Dec 14 '19 at 18:47
  • Because you programmed it like that @Luka ;) When you used `cin` in your code, you told your program to wait at that point, for the user to input something, and only then continue. If you remove all the `cin`, it will just print everything! – gsamaras Dec 14 '19 at 18:50
  • 1
    Done it, thanks for taking time for explaining it to me, what a teacher man! –  Dec 14 '19 at 18:52