-2

Basics: C++ using Visual Studio Code, error message is in terminal, nothing is in "problems" or "Output" section.

#include <iostream>

using namespace std;

int main() 

{  

    int number;
    cout << "Enter your age: ";
    cin >> number;
    cout << "Your age is: " << number;
    return 0;
}
Basics: Visual Studio Code, C++

When I ran the above code it responded with the following in the terminal: "Enter your age: 22 Your age is: 22%"

Does this mean the user would only see 22 or would they see 22%?
Alex
  • 1
  • 2
  • The error message is clear: "use of undeclared identifier 'cout'; did you mean 'std::cout'?" – enzo Jul 17 '21 at 22:36
  • The code you posted is not the code the compiler is complaining about. `std:cout << "Hello world!";` doesn't appear *anywhere* in this code. Regardless, I using the xcode-select installation of the full clang command line tools with my vscode, and never ran into this problem. – WhozCraig Jul 17 '21 at 22:49
  • Does this answer your question? [The #include exists, but I get an error: identifier "cout" is undefined. Why?](https://stackoverflow.com/questions/13208547/the-includeiostream-exists-but-i-get-an-error-identifier-cout-is-undefine) – user3486184 Jul 17 '21 at 23:04
  • The issue is the lack of a newline and your shell (likely zsh) telling you as much. – sweenish Jul 17 '21 at 23:04

1 Answers1

0

we need to use #include <iostream> to use cout that allows us to print output on the screen.

Edit:

Missing double std:: However since you've already mentioned using namespace std again prefixing std:: isn't required.

Edit 2:

Nextline getting appended on same line issue — To fix it we need to add a newline after our cout like

   ...
    int number;
    cout << "Enter your age: ";
    cin >> number;
    cout << "Your age is: " << number << "\n";
    return 0;
    ...
  • I did include it. It is on the very top of the first grey box. What else is wrong with it? – Alex Jul 17 '21 at 22:40
  • Is the error log inline with the actual code? I am unable to find any Hello world in the code above. – Yogesh Kurane Jul 17 '21 at 22:42
  • It looks as if the program itself was buggy by reading my previous code. I restarted the program and it ran. New question, When I ran the above code it responded with the following in the terminal: "Enter your age: 22 Your age is: 22% alexandermorse@Alexanders-MacBook-Air C++ Practice %" Does this mean the user would only see 22 or would they see 22%? – Alex Jul 17 '21 at 22:48
  • User would see 22, however since we didn't explicitly add a new line like ```... number<< "\n";``` the console carried the next output on the same line. So just add a new line in the last cout – Yogesh Kurane Jul 17 '21 at 22:54
  • Please correct your question & I'll update my answer, mark answer as accepted if you're satisfied. – Yogesh Kurane Jul 17 '21 at 22:59
  • Hi @Alex, incase there are no more questions could we close the question? – Yogesh Kurane Jul 20 '21 at 16:31