-2

I am using a book to teach myself C++ and this is code isn't working.

#include <iostream> 
#include <string> 

using namespace std; 

int main() { 
    // Declare a variable to store an integer
    int inputNumber;

    cout << “Enter an integer: “;

    // store integer given user input 
    cin >> inputNumber; 

    // The same with text i.e. string data 
    cout << “Enter your name: “;  
    string inputName; 
    cin >> inputName;  
    cout << inputName << “ entered “ << inputNumber << endl;  
    
    return 0; 
}

Could you please point out my error?

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 6
    "all I got was errors" - but you aren't going to tell us what the errors were ? – John3136 Nov 14 '22 at 03:40
  • 2
    Are those `“` characters you're using for strings accurately copied and pasted? They're apparently some sort of extended character, when what's actually expected is the humble `"` – Nathan Pierson Nov 14 '22 at 03:40
  • Is the book really showing you `using namespace std;`? Because that should not be done. – Pepijn Kramer Nov 14 '22 at 04:01
  • @PepijnKramer: it's probably okay for code like this, that uses `std` and nothing else. It much worse in headers or in scenarios where `std` could have conflicting symbols with other namespaces. So it's possibly best to point out why it's a bad habit to get into. – paxdiablo Nov 14 '22 at 04:05
  • @paxdiablo Indeed it is just something that is best to unlearn, because in larger projects you can run into name. [More here](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Pepijn Kramer Nov 14 '22 at 05:57

1 Answers1

4

The code looks okay but for the use of "smart" quotes, such as (there are multiple places, this is just an example):

cout << “Enter an integer: “;

This is often a result of copying the code from a web page that isn't correctly formatted with code, or by using a word processor as an editor (MS Word, at a minimum, has default rules for text substitutions, such as smart quotes, capitalisation/spelling changes, or copyright symbols.

Smart quotes are not valid tokens to surround string literals with. You should use real quotes instead, such as:

cout << "Enter an integer: ";
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953