Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

1 - Standardní vstup a výstup

Jmenné prostory, standardní vstup a výstup

#include  <stdio.h>    // C include, /usr/include, /usr/local/include
 
#include  <iostream>
 
namespace test
{
    int a = 10;    // test::a
    int b = 78;
}
 
using namespace std;
using namespace test;
 
int main()
{
    printf ("ahoj C\n");
 
    std::cout << "ahoj C++" << std::endl;
 
    std::cout << "jedna" << " dva" << " tri" << std::endl;
 
    std::cout << "10 + 30 = " << 10 + 30 << std::endl;
 
    int a = 33;
 
    std::cout << "a = " << a << ", test::a = " << test::a << std::endl;
 
    cout << "a = " << a << ", test::a = " << test::a << endl;
 
    cout << "b = " << b << endl;
 
    /*
        std::cout   std. vystup
        std::cin    std. vstup
        std::cerr   std. chybovy vystup
        std::clog   std. logovaci vystup
    */
 
    cout << "zadej cislo datoveho typu int: ";
 
    cin >> b;
 
    if (cin.good())
        cout << "zadal jsi " << b << endl;
    else
        cout << "nauc se datove typy" << endl;
 
    return 0;
}

Formátování standardního výstupu

#include <iostream>
#include <iomanip>
 
using namespace std;
 
int main()
{
	float pi = 3.14159265;
	float pp = 65.876866;
	float px = 768748.898;
 
	cout << pi << endl << pp << endl << px << endl;
 
	cout << setprecision(2);	
 
	cout << pi << endl << pp << endl << px << endl;
 
	cout << fixed;
 
	cout << "fixed: " << endl << pi << endl << pp << endl << px << endl;
 
	cout << setprecision(5);
 
	cout << pi << endl << pp << endl << px << endl;
 
	cout << scientific;
 
	cout << pi << endl << pp << endl << px << endl;
 
	cout.unsetf (ios_base::floatfield);
 
	cout << pi << endl << pp << endl << px << endl;
 
	cout << setw(15) << pi << endl;
 
	return 0;
}

Reference

#include <iostream>
 
void f1(int *a) 
{
	*a = 31;
}
 
void f2(int &a) 
{
	a = 32;	
}
 
int main()
{
    int x = 10;
 
    std::cout << "x = " << x << std::endl;
 
    f1 (&x);
 
    std::cout << "x = " << x << std::endl;
 
    int &y = x;
 
    f2 (x);
 
    std::cout << "x = " << x << std::endl;
 
    return 0;
}

Jednoduchá práce se souborem.

#include <iostream>
#include <fstream>
 
using namespace std;
 
int main ()
{
	ifstream in ("04.cpp");
	ofstream out ("04-copy.cpp");
 
	if (in.good ())
	{
		cout << "soubor byl otevren" << endl;
 
		string s;
 
		while (getline (in, s))
		{
			cout << s << endl;
			out << s << endl;
		}
	}
 
	return 0;
}

courses/bd5b37ppc/tutorials/01.txt · Last modified: 2019/10/03 20:43 by viteks