PRIMEIRO PROGRAMA
PRIMEIRO PROGRAMA
#include<iostream> // AQUI INCLUIR UMA BIBLIOTECA PADRAO DE ENTRADA E SAIDA
using namespace std;
cout - saida padrao tipo print
endl - quebra de linha
int main (){
cout << "***************************************" << endline;
cout << "* Boas vindas ao Jogo da Adivinhação! *" << endl;
cout << "***************************************" << endl;
}
Compilando
PS C:\Users\User\Desktop\CPPESTUDOS> g++ .\AULA01.cpp -o AULA01.EXE
PS C:\Users\User\Desktop\CPPESTUDOS> .\AULA01.EXE
***************************************
* Boas vindas ao Jogo da Adivinha癟瓊o! *
***************************************
#include<iostream>
using namespace std;
int main (){
cout << "***************************************" << endl;
cout << "* Boas vindas ao Jogo da Adivinhação! *" << endl;
cout << "***************************************" << endl;
int numero_secreto = 42;
// o << var << serve para identificar por uma var dentro de uma string contatenando
cout << "o numero secreto eh " << numero_secreto << "nao conte pra ninguem" << endl;
}
#include<iostream>
using namespace std;
int main (){
cout << "***************************************" << endl;
cout << "* Boas vindas ao Jogo da Adivinhação! *" << endl;
cout << "***************************************" << endl;
const int NUMERO_SECRETO = 42;
// o << var << serve para identificar por uma var dentro de uma string contatenando
// cout << "o numero secreto eh " << numero_secreto << "nao conte pra ninguem" << endl;
int chute;
cout << "* CHUTA UM NUMERO AI *" << endl;
// esse eh o input do comando MAIOR MAIOR E >> PRA DENTRO E << PRA FORA
cin >> chute;
cout << "o seu chute foi "<< chute << endl;
if (chute == NUMERO_SECRETO ) {
cout << "* VC ACERTOU O NUMERO SECRETO *" << endl;
}
else if (chute > NUMERO_SECRETO ){
cout << "* ERROR O NUMERO EH MAIOR QUE O SECRETO *" << endl;
}
else{
cout << "* ERROR O NUMERO EH MENOR QUE O SECRETO *" << endl;
}
}
#include<iostream>
using namespace std;
int main (){
cout << "***************************************" << endl;
cout << "* Boas vindas ao Jogo da Adivinhação! *" << endl;
cout << "***************************************" << endl;
const int NUMERO_SECRETO = 42;
bool ACERTOU = true;
while (ACERTOU)
{
/* code */
// o << var << serve para identificar por uma var dentro de uma string contatenando
// cout << "o numero secreto eh " << numero_secreto << "nao conte pra ninguem" << endl;
int chute;
cout << "* CHUTA UM NUMERO AI *" << endl;
// esse eh o input do comando MAIOR MAIOR E >> PRA DENTRO E << PRA FORA
cin >> chute;
cout << "o seu chute foi "<< chute << endl;
if (chute == NUMERO_SECRETO ) {
cout << "* VC ACERTOU O NUMERO SECRETO *" << endl;
ACERTOU = false;
}
else if (chute > NUMERO_SECRETO ){
cout << "* ERROR O NUMERO EH MAIOR QUE O SECRETO *" << endl;
}
else{
cout << "* ERROR O NUMERO EH MENOR QUE O SECRETO *" << endl;
}
}
}
RESULTADO
* CHUTA UM NUMERO AI *
20
o seu chute foi 20
* ERROR O NUMERO EH MENOR QUE O SECRETO *
* CHUTA UM NUMERO AI *
50
o seu chute foi 50
* ERROR O NUMERO EH MAIOR QUE O SECRETO *
* CHUTA UM NUMERO AI *
45
o seu chute foi 45
* ERROR O NUMERO EH MAIOR QUE O SECRETO *
* CHUTA UM NUMERO AI *
43
o seu chute foi 43
* ERROR O NUMERO EH MAIOR QUE O SECRETO *
* CHUTA UM NUMERO AI *
42
o seu chute foi 42
* VC ACERTOU O NUMERO SECRETO *
srand(time(NULL));
// usando o time para definir uma seed
// gerando um numero aleatorio fazendo a divisao do time por 100 no caso o % eh o resto
const int NUMERO_SECRETO = rand() % 100;
cout << NUMERO_SECRETO << endl;
bool ACERTOU = true;
while (ACERTOU)
FOR
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
cout << i << endl;
}
return 0;
}
MAP DICIONARIO
#include <iostream>
#include <map>
using namespace std;
int main() {
string str = "exemplo";
// CHAVE E VALOR DECLARAR OS TIPOS
map<char, int> freq;
for (char c : str) {
// ASSIM SE ADICIONA O VALOR NO DICT
freq[c]++;
}
for (auto p : freq) {
cout << p.first << ": " << p.second << endl;
}
return 0;
}
VETORES
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v = {1, 2, 3};
// Adicionando valores
v.push_back(4);
v.push_back(5);
// Modificando valores
v[2] = 6;
// Removendo valores
v.erase(v.begin() + 1);
for (int x : v) {
cout << x << endl;
}
return 0;
}
FUNÇÃO
#include <iostream>
using namespace std;
int fatorial(int n) {
int fat = 1;
for (int i = 1; i <= n; i++) {
fat *= i;
}
return fat;
}
int main() {
cout << fatorial(5) << endl;
return 0;
}
lendo arquivos txt
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream file;
file.open("arquivo.txt");
string conteudo;
while (getline(file, conteudo)) {
cout << line << endl;
}
file.close();
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// estancia o objeto file
ofstream file;
// usa o metodo open
file.open("arquivo.txt");
file << "Linha 1\n";
file << "Linha 2\n";
file.close();
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5};
for (int x : v) {
cout << x << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
try {
int x = 5;
int y = 0;
if (y == 0) {
throw "Divisão por zero!";
}
int z = x / y;
} catch (const char* msg) {
cerr << "Erro: " << msg << endl;
}
return 0;
}
EXEMPLO USANDO O FOR PARA ESCREVER EM UM ARQUIVO
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// estancia o objeto file
ofstream file;
// usa o metodo open
file.open("arquivo.txt");
int far = 10;
for (int i = 1; i <= far; i++) {
file << "Linha " << i << "\n";
}
file.close();
return 0;
}
Para importar é necessario definir um header para cada funcção usando a extensão .hpp