1. Basic - C++

3 minute read

  1. Basic - C++





#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main(int argc, const char * argv[]){
    
    
    // 1. Variable
    
    // const 는 뒤를 상수 선언
    // char * argv[] 는 포인트 배열
    // argc 는 인트형 인자 argc
    
    int a = 12;
    float b = 1.234l;
    char c = 'C';
    bool d = true;
    
    std::cout << a << ' ';
    std::cout << b << ' ';
    std::cout << c << ' ';
    
    if (d)
        std::cout << true << " \n";
    

    // 2. User Input

    short int aa;
    std::cout << "Enter an mount";
    std::cin >> aa;

    std::cout << "The amount you entered was " << aa;
    std::cout << "\n";



    // 3. User Input & Reading a String

    char aaa[20];
    std::cout << "Enter an name";
    std::cin >> aaa;

    std::cout << "The amount you entered was " << aaa;
    std::cout << "\n";


    
    
    // 4. If statement
    int aaaa = 4;
    
    if (aaaa == 4 )
        std::cout << "You entered 4" << " \n";
    
    // 5. If else statement
    int aaaaa = 50;
    
    if (aaaaa == 40)
        std::cout << "You entered 40" << " \n";
    else
        std::cout << "You entered 50" << " \n";
    
    
    // 6. Else if statement
    int aaaaaa = 600;
    
    if (aaaaaa == 400)
        std::cout << "You entered 400" << " \n";
    else if ( aaaaaa == 500)
        std::cout << "You entered 500" << " \n";
    else if ( aaaaaa == 600)
        std::cout << "You entered 500" << " \n";
    else
        std::cout << "You entered else";
    
    
    
    // 7. Switch case statement
    int s = 2;
    
    switch(s) {
        case 1:
            std::cout << "one \n";
            break;
        case 2:
            std::cout << "two \n";
            break;
        case 3:
            std::cout << "three \n";
            break;
        
        default:
            std::cout << "Error \n";
            break;
    }
    
    
    
    
    // 8. For statement
    
    int e;
    
    for (e=0; e<5; e++){
        std::cout << "for" << "\n";
    }
    
    
    // 9. Arrays
    
    int f[5] = {1,2,3,56,75};
    
    for (int i = 0; i < 5; i++) {
        std::cout << "Number #" << i+1 << ":" << f[i] << "\n";
        
    }
    
    
    
    // 10. Character Arrays
    
    char str[] = "Hello Hanul";
    int i = 0;
    while (str[i] != '\0') {
        std::cout << str[i++] << "\n";
    }
    
    
    
    // 11. Pointers
    
    int x = 5;
    std::cout << x << '\n'; // print the value of variable x
    std::cout << &x << '\n'; // print the memory address of variable x
    std::cout << *&x << '\n'; /// print the value at the memory address of variable x

    
    int h = 25;
    int* ip = &h;
    std::cout << *ip << "\n";
    
    
    
    
    return 0;
}





#include <iostream>

//12. Point arithmetic
int countdown[] = {10,9,8,7,6,5,4,3,2,1,0};

float dues[] = {
    30.00,
    55.00,
    90.00
};


//13. Function declare
void errormessage(){
    // Declare function
    std::cout << "Error \n";
};



//14. The return statement
// prototype
void when(){
    
    int when = 0 ;
    
    if (when ==0){
        std::cout << "Retruning now \n";
        return;
    }
    std:: cout << "Returning later \n";
    return;
    
};


// 15. Returning Value
int width(){
    
    int a;
    std::cout << "Enter width in feet";
    return a = 12;
};


// 16. Function Argument
int getfeet(){
    int feet = 8;
    return feet;
};
inline int width2(int feet){
    return feet * 12;
}



// 17. Recursion
void displayints(int from, int to){
    if(from<to)
        displayints(from+1, to);
    std::cout << from << "\n";
}


// 18. Switch Case
void DisplayMenu(){
    std::cout << "---Menu---\n";
};
int getSelection(){
    int a=1;
    
    std::cout << "Get selction";
    return a;
};




int main(int argc, const char * argv[]) {
    
    // 12. Point arithmetic
    
    int *cdp = &countdown[0];
    do {
        std::cout << *cdp << ", ";
        cdp++;
    } while (*cdp);
    
    std::cout << "Blast - off \n";


    float* dp = &dues[0];
    for (int i = 0; i < 3; i ++){
        std::cout << *(dp+i) << "\n";
    }


    // 13. Function Declare
    
    int a = 2;
    
    if (a!=0 && a!=1) {
        errormessage();
    }
    
    
    // 14. The return statement
    
    when();
    
    
    // 15. Returning Value
    
    int wd = width();
    std::cout << "Width = " << wd << "\n" ;
    
    
    // 16. Function Argument
    int feet = getfeet();
    int wdd = width2(feet);
    std::cout << "The width2 in Inches = " << wdd << "\n" ;

    
    // 17. Recursion
    displayints(0, 2);
    
    
    // 18. Switch Case
    int ai = 0;
    while (ai!=1){
        DisplayMenu();
        ai = getSelection();
        switch (ai) {
            case 1:
                std::cout << "Recieved \n";
                break;
            case 2:
                std::cout << "play \n";
                break;
                
            default:
                std::cout << "Error \n";
                break;
        }
        
    }
    
    
}
       
    


Leave a comment