Login Register
MoHH2 Legendz

Legends of Medal of honor:Heores 2.. Never forgotten RiP in piece


    You are not connected. Please login or register

    View previous topic View next topic Go down  Message [Page 1 of 1]

    Post: #1Tue Aug 21, 2012 11:01 pm

    ordo ab chao

    ordo ab chao

    Member


    This should have been my second post, but I idiotically posted up source code of a "complicated" application for new learners. This post will be about basic input and output in c++. here would be an example of a Hello program:
    Code:
    #includes <iostream>
    using namespace std;//this is for the cin and cout functions

    int main()
    {
        char x[128];//edited 10:04 am 8/22/2012
        cout << "What is your name?" << endl;//this is the output
        cin >> x;//this is the input
        cout << "Hello " << x << "!" << endl; //this is the result
        system("PAUSE");//this lets you review the result
        return 0;// returns positive if it completes
    }
    And that is a basic Hello program. In the next tutorial we will be covering the use of variables.


    Post: #2Thu Oct 17, 2013 2:34 pm

    emu

    emu

    Newbie
    Newbie


    You shouldn't use an array of char unless you know all the space is going to be taken up or that not many left over spot. You are using extra space in memory, with the empty slots of the array. In your case all you should really do is change the size of your array to 20, for input of first and last names at the most.
    Code:

    #include <iostream> //input/output stream
    #include <string> //string
    #include <windows.h> //windows functions if not using vs c++

    using namespace std; //function shorteners using namespace

    int main(void) {
         string uName; //user name
        
         cout << "What is your name: ";
         cin >> uName; //store name inside uName

         cout << "Hello " << uName << "!" << endl; //output
         Sleep(3*1000); //wait 3 seconds before ending

         return 0;
    }
    best way to do this for mem purposes


    View previous topic View next topic Back to top  Message [Page 1 of 1]

    Permissions in this forum:
    You cannot reply to topics in this forum