Skip to content

Instantly share code, notes, and snippets.

@revirth
Last active October 29, 2022 18:40
Show Gist options
  • Save revirth/47b7dc1dd23dbab28e51c4162bd5ea30 to your computer and use it in GitHub Desktop.
Save revirth/47b7dc1dd23dbab28e51c4162bd5ea30 to your computer and use it in GitHub Desktop.
C++_Unreal_Cours_1028
#include <iostream>
#include <string>
using namespace std;
void Exercise_While_Break()
{
string password;
while (true)
{
std::cout << "Enter a password : ";
getline(cin, password);
for (int i = 0; i < password.length(); i++)
password[i] = tolower(password[i]);
if (password == "exit") break;
}
}
void Infant_Ado_Adult(int repeat = 2)
{
string name;
int age;
for (int i = 0; i < repeat; i++)
{
cout << "User." << i + 1 << " Nom : ";
getline(cin, name);
cout << "User." << i + 1 << " Age : ";
cin >> age;
if (age < 13)
cout << name << " " << age << "ans " << ": infant";
else if (age < 19)
cout << name << " " << age << "ans " << ": ado";
else
cout << name << " " << age << "ans " << ": adult";
cin.ignore();
cout << endl;
}
}
void Exercise_For_If(int balance)
{
int total_price = 0;
int total_article = 0;
cout << "How many items did you buy ? ";
cin >> total_article;
for (int i = 1; i <= total_article; i++)
{
string nom;
int price, quantity;
cout << i << ". What did you buy ? (name price quantity)" << endl;
cin >> nom >> price >> quantity;
total_price += (price * quantity);
cout << endl;
}
cout << "Total price is $" << total_price << "\n\n";
cout << "<<I try to pay with my debit card...>> \n\n";
if (total_price >= balance)
cout << "[Monitor] Sorry, not enough money" << endl;
else
cout << "[Monitor] It's done, $" << (balance - total_price) << " in your bank account" << endl;
}
void Exercise_While_SwitchCase()
{
int total_move = 0;
char direction;
while (true)
{
cout << "Select action (W:↑ S:↓ A:← D:→ X:Quit) : ";
cin >> direction;
switch (toupper(direction))
{
case 'W': cout << "moving to the NORTH\n\n";
total_move++;
break;
case 'S': cout << "moving to the SOUTH\n\n";
total_move++;
break;
case 'A': cout << "moving to the WEST\n\n";
total_move++;
break;
case 'D': cout << "moving to the EAST\n\n";
total_move++;
break;
case 'X': cout << "Thank you for playing this game. You moved " << total_move << " times\n\n";
return;
default:
cout << "[ERROR] Please retry\n\n";
break;
}
}
}
void Exercise_PrimeNumber(int repeat)
{
srand(time(NULL));
for (int i = 0; i < repeat; i++)
{
int number = rand() % 100 + 2;
bool isPrime;
if (number == 1) continue;
if (number == 2 || number == 3 || number == 5 || number == 7)
isPrime = true;
else if (number % 2 == 0)
isPrime = false;
else if (number % 3 == 0)
isPrime = false;
else if (number % 5 == 0)
isPrime = false;
else if (number % 7 == 0)
isPrime = false;
else
isPrime = true;
cout << number << " is " << (isPrime ? "A" : "NOT a") << " Prime Number\n\n";
}
}
int main()
{
//Exercise_While_Break();
//Infant_Ado_Adult(3);
//Exercise_For_If(300);
//Exercise_While_SwitchCase();
Exercise_PrimeNumber(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment