If ladders

If ladders, arrays, data conversion

if ladder, auto, bool

#include <iostream>
using namespace std;

int main(){
    int x;
    x=1;
    int y=2;
    int z(3);
    auto a=81; //infers that a is an int
    auto b=3.14; //b double
    auto c("Hello"); //c string
    auto d='A';   // d char
    //bool e(true);
    //bool f(false);
    cout<< x<<y<<z<<a<<b<<c<<d; 
    if (a<9)                                                                                                                          
    {
        cout<<"en";
        cout<<"o";
    }
    else if (d=='A')
    {
        cout<<"d is A";
    }
    else
    {
        cout<<"a is not less than 9";
    }
    return 0;
}

C arrays and strings

#include <cstring>
#include <iostream>
using namespace std;

int main(){
    char x[4];
    /*
    x[0]='a';
    x[1]='b';
    x[2]='c';
    x[3]='\0'; //null char null terminated c-string
    */
    strncpy(x, "abcaaaaaaaaaaaaaaaaaaaa", sizeof(x)-1);

    //string s("abc");
    //cout<<s;
    
    cout<<x<<endl; 
    //C style
    printf("the string %s has %i chars \n", x, strlen(x));

    return 0;
}

Data conversion


    //example convert string -> int
    string age("14");  //string age="14";
    cout<< atoi(age.c_str())+1 << endl;
    //age.c_str() convert c++ to c string
    // or if using C++11
    cout << stoi(age) << endl;
    
    //example convert int -> string
    int num(14);
    stringstream ss;
    ss << num;
    cout << ss.str() << endl;
    //or if using C++11
    cout << to_string(num)+"hi" << endl;
    
    //example of c-string -> double
    char n[32];
    strncpy(n,"12",32);
    cout<<strtod(n, NULL)+1;
    
    //example of c++ string -> double
    string nn("12.03");
    cout<<endl<<strtod(nn.c_str(), NULL)+1;