Thread: Two Questions About Two Programs

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    2

    Two Questions About Two Programs

    Program Number One:

    Code:
    #include <iostream>  // cin, cout, <<, >>
    #include <string>    // string
    using namespace std;
    
    //The program starts below
    
    int main()
    {
        cout << "What is your first name? ";
        string firstName;
        cin >> firstName;
    
        cout << "\nWelcome to C++, " << firstName <<"!\n";
    
    
    }
    My first question is: Why do I get an error if I modify the cout statement as such:

    Code:
    cout << \n"Welcome to C++, " << firstName << ;
    Program number two:

    Code:
    #include <iostream>
    
    using namespace std;
    #include <string>;
    
    
    int main()
    {
        cout << "Hello world!" << endl;
        return 0;
    }
    How come that it does not work if I modify it like this:

    Code:
    cout << "Hello world!" << ;
    ...but it works if I change it to this:

    Code:
    cout << "Hello world!" << "\n";
    (Note the first cout statement in program number one)

    What rules am I violating?

    I apologize for asking such basic questions, but the book I am learning from does not explain it.

    Thanks in advance.

    Best
    Last edited by Alienspecimen; 07-06-2017 at 08:45 PM. Reason: Clarify

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    So normally you would get an error like this:

    Code:
    C:\Users\jk>g++ sandbox.cpp -o sandbox.exe
    sandbox.cpp: In function 'int main()':
    sandbox.cpp:13:50: error: expected primary-expression before ';' token
         cout << "\nWelcome to C++, " << firstName << ;
                                                      ^
    When the compiler says it expects a primary expression, it's really trying to say it expects a literal constant (such as a "string", 'char' or number) or something with a name (for instance a variable). A program cannot be compiled if you have the stream insertion operator and the semi-colon with nothing in between. To put it even simpler, all you've done is end a statement right after using an operator that needs to have things on both sides. This error is no different than this perhaps more obvious problem:
    Code:
    int main() {
       int answer = 2 + ;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginer has outdated programs & questions
    By Randyk in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2011, 02:23 PM
  2. Replies: 1
    Last Post: 11-09-2009, 07:03 AM
  3. Questions regarding programs
    By girish1026 in forum C Programming
    Replies: 1
    Last Post: 01-10-2009, 09:24 AM
  4. A few questions on programs and when they can run...
    By Junior89 in forum Windows Programming
    Replies: 2
    Last Post: 04-05-2005, 07:47 PM
  5. Programs opening programs
    By LinuxPLC in forum C Programming
    Replies: 1
    Last Post: 11-21-2002, 12:50 PM

Tags for this Thread