diff --git a/c++/Add Two Distances (in inch-feet) System Using Structures.cpp b/c++/Add Two Distances (in inch-feet) System Using Structures.cpp new file mode 100644 index 00000000..aa5da586 --- /dev/null +++ b/c++/Add Two Distances (in inch-feet) System Using Structures.cpp @@ -0,0 +1,38 @@ +//Add Two Distances (in inch-feet) System Using Structures + +#include +using namespace std; + +struct Distance { + int feet; + float inch; +}d1 , d2, sum; + +int main() { + cout << "Enter 1st distance," << endl; + cout << "Enter feet: "; + cin >> d1.feet; + cout << "Enter inch: "; + cin >> d1.inch; + + cout << "\nEnter information for 2nd distance" << endl; + cout << "Enter feet: "; + cin >> d2.feet; + cout << "Enter inch: "; + cin >> d2.inch; + + sum.feet = d1.feet+d2.feet; + sum.inch = d1.inch+d2.inch; + + // changing to feet if inch is greater than 12 + if(sum.inch > 12) { + // extra feet + int extra = sum.inch / 12; + + sum.feet += extra; + sum.inch -= (extra * 12); + } + + cout << endl << "Sum of distances = " << sum.feet << " feet " << sum.inch << " inches"; + return 0; +} diff --git a/c++/Hangman Game Code.cpp b/c++/Hangman Game Code.cpp new file mode 100644 index 00000000..148349f6 --- /dev/null +++ b/c++/Hangman Game Code.cpp @@ -0,0 +1,212 @@ +// C++ program for the hangman game +#include +#include +#include +#include +#include +#include + +// define maxi. number of incorrect attempts +#define MAX_ATTEMPTS 6 + +using namespace std; + +// main class +class HangmanGame { +public: + // constructor to ini + HangmanGame() + { + srand(static_cast(time(nullptr))); + secretWord = getRandomWord(); + currentWord = string(secretWord.length(), '_'); + attemptsLeft = MAX_ATTEMPTS; + } + + // function to start the game. + void play() + { + cout << "Welcome to Hangman!" << endl; + cout << "Category: Fruits" << endl; + cout << "You have " << attemptsLeft + << " attempts to guess the fruit name." + << endl; + + // the main game loop which will go on till the + // attempts are left or the game is won. + while (attemptsLeft > 0) { + displayGameInfo(); + char guess; + cout << "Guess a letter: "; + cin >> guess; + + if (isalpha(guess)) { + guess = tolower(guess); + if (alreadyGuessed(guess)) { + cout << "You've already guessed that " + "letter." + << endl; + } + else { + bool correctGuess + = updateCurrentWord(guess); + // if the guess is correct, we will + // update the word and check if the word + // is completely guessed or not + if (correctGuess) { + cout << "Good guess!" << endl; + // if the word is completely + // guessed. + if (currentWord == secretWord) { + displayGameInfo(); + cout << "Congratulations! You " + "guessed the word: " + << secretWord << endl; + return; + } + } + else { + cout << "Incorrect guess." << endl; + attemptsLeft--; + drawHangman(attemptsLeft); + } + } + } + else { + cout << "Please enter a valid letter." + << endl; + } + } + + if (attemptsLeft == 0) { + displayGameInfo(); + cout << "You've run out of attempts. The word " + "was: " + << secretWord << endl; + } + } + +private: + string secretWord; + string currentWord; + int attemptsLeft; + vector guessedLetters; + + // select random word from the predefined word + string getRandomWord() + { + vector words + = { "apple", "banana", "cherry", "grape", + "kiwi" }; + int index = rand() % words.size(); + return words[index]; + } + + // checking if the word is already guessed + bool alreadyGuessed(char letter) + { + return find(guessedLetters.begin(), + guessedLetters.end(), letter) + != guessedLetters.end(); + } + + // updating the word after correct guess + bool updateCurrentWord(char letter) + { + bool correctGuess = false; + for (int i = 0; i < secretWord.length(); i++) { + if (secretWord[i] == letter) { + currentWord[i] = letter; + correctGuess = true; + } + } + guessedLetters.push_back(letter); + return correctGuess; + } + + // function to provide the info at particular point in + // the game + void displayGameInfo() + { + cout << "Word: " << currentWord << endl; + cout << "Attempts left: " << attemptsLeft << endl; + cout << "Guessed letters: "; + for (char letter : guessedLetters) { + cout << letter << " "; + } + cout << endl; + } + + // function to progressively draw the hangman + void drawHangman(int attemptsLeft) + { + // Add your hangman drawing logic here + // For simplicity, you can print a static hangman + // ASCII art Modify this function to display the + // hangman as you like + if (attemptsLeft == 5) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 4) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | |" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 3) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 2) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|\\" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 1) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|\\" << endl; + cout << " | /" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 0) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|\\" << endl; + cout << " | / \\" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + } +}; + +// driver code +int main() +{ + + HangmanGame game; + game.play(); + + return 0; +} diff --git a/c++/Hangman game Code b/c++/Hangman game Code new file mode 100644 index 00000000..148349f6 --- /dev/null +++ b/c++/Hangman game Code @@ -0,0 +1,212 @@ +// C++ program for the hangman game +#include +#include +#include +#include +#include +#include + +// define maxi. number of incorrect attempts +#define MAX_ATTEMPTS 6 + +using namespace std; + +// main class +class HangmanGame { +public: + // constructor to ini + HangmanGame() + { + srand(static_cast(time(nullptr))); + secretWord = getRandomWord(); + currentWord = string(secretWord.length(), '_'); + attemptsLeft = MAX_ATTEMPTS; + } + + // function to start the game. + void play() + { + cout << "Welcome to Hangman!" << endl; + cout << "Category: Fruits" << endl; + cout << "You have " << attemptsLeft + << " attempts to guess the fruit name." + << endl; + + // the main game loop which will go on till the + // attempts are left or the game is won. + while (attemptsLeft > 0) { + displayGameInfo(); + char guess; + cout << "Guess a letter: "; + cin >> guess; + + if (isalpha(guess)) { + guess = tolower(guess); + if (alreadyGuessed(guess)) { + cout << "You've already guessed that " + "letter." + << endl; + } + else { + bool correctGuess + = updateCurrentWord(guess); + // if the guess is correct, we will + // update the word and check if the word + // is completely guessed or not + if (correctGuess) { + cout << "Good guess!" << endl; + // if the word is completely + // guessed. + if (currentWord == secretWord) { + displayGameInfo(); + cout << "Congratulations! You " + "guessed the word: " + << secretWord << endl; + return; + } + } + else { + cout << "Incorrect guess." << endl; + attemptsLeft--; + drawHangman(attemptsLeft); + } + } + } + else { + cout << "Please enter a valid letter." + << endl; + } + } + + if (attemptsLeft == 0) { + displayGameInfo(); + cout << "You've run out of attempts. The word " + "was: " + << secretWord << endl; + } + } + +private: + string secretWord; + string currentWord; + int attemptsLeft; + vector guessedLetters; + + // select random word from the predefined word + string getRandomWord() + { + vector words + = { "apple", "banana", "cherry", "grape", + "kiwi" }; + int index = rand() % words.size(); + return words[index]; + } + + // checking if the word is already guessed + bool alreadyGuessed(char letter) + { + return find(guessedLetters.begin(), + guessedLetters.end(), letter) + != guessedLetters.end(); + } + + // updating the word after correct guess + bool updateCurrentWord(char letter) + { + bool correctGuess = false; + for (int i = 0; i < secretWord.length(); i++) { + if (secretWord[i] == letter) { + currentWord[i] = letter; + correctGuess = true; + } + } + guessedLetters.push_back(letter); + return correctGuess; + } + + // function to provide the info at particular point in + // the game + void displayGameInfo() + { + cout << "Word: " << currentWord << endl; + cout << "Attempts left: " << attemptsLeft << endl; + cout << "Guessed letters: "; + for (char letter : guessedLetters) { + cout << letter << " "; + } + cout << endl; + } + + // function to progressively draw the hangman + void drawHangman(int attemptsLeft) + { + // Add your hangman drawing logic here + // For simplicity, you can print a static hangman + // ASCII art Modify this function to display the + // hangman as you like + if (attemptsLeft == 5) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 4) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | |" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 3) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 2) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|\\" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 1) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|\\" << endl; + cout << " | /" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 0) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|\\" << endl; + cout << " | / \\" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + } +}; + +// driver code +int main() +{ + + HangmanGame game; + game.play(); + + return 0; +} diff --git a/c++/To Find Transpose of a Matrix b/c++/To Find Transpose of a Matrix new file mode 100644 index 00000000..168b61eb --- /dev/null +++ b/c++/To Find Transpose of a Matrix @@ -0,0 +1,47 @@ +//To Find Transpose of a Matrix +#include +using namespace std; + +int main() { + int a[10][10], transpose[10][10], row, column, i, j; + + cout << "Enter rows and columns of matrix: "; + cin >> row >> column; + + cout << "\nEnter elements of matrix: " << endl; + + // Storing matrix elements + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + cout << "Enter element a" << i + 1 << j + 1 << ": "; + cin >> a[i][j]; + } + } + + // Printing the a matrix + cout << "\nEntered Matrix: " << endl; + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + cout << " " << a[i][j]; + if (j == column - 1) + cout << endl << endl; + } + } + + // Computing transpose of the matrix + for (int i = 0; i < row; ++i) + for (int j = 0; j < column; ++j) { + transpose[j][i] = a[i][j]; + } + + // Printing the transpose + cout << "\nTranspose of Matrix: " << endl; + for (int i = 0; i < column; ++i) + for (int j = 0; j < row; ++j) { + cout << " " << transpose[i][j]; + if (j == row - 1) + cout << endl << endl; + } + + return 0; +} diff --git a/c++/To Find Transpose of a Matrix.cpp b/c++/To Find Transpose of a Matrix.cpp new file mode 100644 index 00000000..168b61eb --- /dev/null +++ b/c++/To Find Transpose of a Matrix.cpp @@ -0,0 +1,47 @@ +//To Find Transpose of a Matrix +#include +using namespace std; + +int main() { + int a[10][10], transpose[10][10], row, column, i, j; + + cout << "Enter rows and columns of matrix: "; + cin >> row >> column; + + cout << "\nEnter elements of matrix: " << endl; + + // Storing matrix elements + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + cout << "Enter element a" << i + 1 << j + 1 << ": "; + cin >> a[i][j]; + } + } + + // Printing the a matrix + cout << "\nEntered Matrix: " << endl; + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + cout << " " << a[i][j]; + if (j == column - 1) + cout << endl << endl; + } + } + + // Computing transpose of the matrix + for (int i = 0; i < row; ++i) + for (int j = 0; j < column; ++j) { + transpose[j][i] = a[i][j]; + } + + // Printing the transpose + cout << "\nTranspose of Matrix: " << endl; + for (int i = 0; i < column; ++i) + for (int j = 0; j < row; ++j) { + cout << " " << transpose[i][j]; + if (j == row - 1) + cout << endl << endl; + } + + return 0; +} diff --git a/c++/hangman game code.cpp b/c++/hangman game code.cpp new file mode 100644 index 00000000..148349f6 --- /dev/null +++ b/c++/hangman game code.cpp @@ -0,0 +1,212 @@ +// C++ program for the hangman game +#include +#include +#include +#include +#include +#include + +// define maxi. number of incorrect attempts +#define MAX_ATTEMPTS 6 + +using namespace std; + +// main class +class HangmanGame { +public: + // constructor to ini + HangmanGame() + { + srand(static_cast(time(nullptr))); + secretWord = getRandomWord(); + currentWord = string(secretWord.length(), '_'); + attemptsLeft = MAX_ATTEMPTS; + } + + // function to start the game. + void play() + { + cout << "Welcome to Hangman!" << endl; + cout << "Category: Fruits" << endl; + cout << "You have " << attemptsLeft + << " attempts to guess the fruit name." + << endl; + + // the main game loop which will go on till the + // attempts are left or the game is won. + while (attemptsLeft > 0) { + displayGameInfo(); + char guess; + cout << "Guess a letter: "; + cin >> guess; + + if (isalpha(guess)) { + guess = tolower(guess); + if (alreadyGuessed(guess)) { + cout << "You've already guessed that " + "letter." + << endl; + } + else { + bool correctGuess + = updateCurrentWord(guess); + // if the guess is correct, we will + // update the word and check if the word + // is completely guessed or not + if (correctGuess) { + cout << "Good guess!" << endl; + // if the word is completely + // guessed. + if (currentWord == secretWord) { + displayGameInfo(); + cout << "Congratulations! You " + "guessed the word: " + << secretWord << endl; + return; + } + } + else { + cout << "Incorrect guess." << endl; + attemptsLeft--; + drawHangman(attemptsLeft); + } + } + } + else { + cout << "Please enter a valid letter." + << endl; + } + } + + if (attemptsLeft == 0) { + displayGameInfo(); + cout << "You've run out of attempts. The word " + "was: " + << secretWord << endl; + } + } + +private: + string secretWord; + string currentWord; + int attemptsLeft; + vector guessedLetters; + + // select random word from the predefined word + string getRandomWord() + { + vector words + = { "apple", "banana", "cherry", "grape", + "kiwi" }; + int index = rand() % words.size(); + return words[index]; + } + + // checking if the word is already guessed + bool alreadyGuessed(char letter) + { + return find(guessedLetters.begin(), + guessedLetters.end(), letter) + != guessedLetters.end(); + } + + // updating the word after correct guess + bool updateCurrentWord(char letter) + { + bool correctGuess = false; + for (int i = 0; i < secretWord.length(); i++) { + if (secretWord[i] == letter) { + currentWord[i] = letter; + correctGuess = true; + } + } + guessedLetters.push_back(letter); + return correctGuess; + } + + // function to provide the info at particular point in + // the game + void displayGameInfo() + { + cout << "Word: " << currentWord << endl; + cout << "Attempts left: " << attemptsLeft << endl; + cout << "Guessed letters: "; + for (char letter : guessedLetters) { + cout << letter << " "; + } + cout << endl; + } + + // function to progressively draw the hangman + void drawHangman(int attemptsLeft) + { + // Add your hangman drawing logic here + // For simplicity, you can print a static hangman + // ASCII art Modify this function to display the + // hangman as you like + if (attemptsLeft == 5) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 4) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | |" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 3) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 2) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|\\" << endl; + cout << " |" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 1) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|\\" << endl; + cout << " | /" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + else if (attemptsLeft == 0) { + cout << " _____" << endl; + cout << " | |" << endl; + cout << " | O" << endl; + cout << " | /|\\" << endl; + cout << " | / \\" << endl; + cout << " |" << endl; + cout << " |" << endl; + } + } +}; + +// driver code +int main() +{ + + HangmanGame game; + game.play(); + + return 0; +} diff --git a/c++/login and registration system.cpp b/c++/login and registration system.cpp new file mode 100644 index 00000000..c37de927 --- /dev/null +++ b/c++/login and registration system.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +#include + +using namespace std; + + +void mainmenu(); + + +int choice; +bool cinfail; +int confirmation; +string username, password, password2; + +void writetofile(string username){ + ofstream writefile; + string file = username+".txt"; + writefile.open(file.c_str()); + writefile << password; + writefile.close(); + mainmenu(); } + +void login(){ + cout << "You are being logged in!";} + + +void registerpassword(){ + cout << "Please enter the password:" << endl; + cin >> password; + cout << "Please renter your password:" << endl; + cin >> password2; + if (password == password2){ + cin.clear(); + cin.ignore(10000,'\n'); + writetofile(username); + exit(1); + } + else;{ + cout << "Sorry invalid" << endl; + registerpassword(); + }} + + +void registerme(){ + cout << "Please enter your username: " << endl; + getline(cin, username); + cout << "\nUsername - \""<< username << "\"\nConfirm? \n\n[1] Yes\n[2] No" << endl; + cin >> confirmation; + if (confirmation == 1){ + registerpassword(); + } + + else; { + cout << "Sorry invalid input, Please try again" << endl; + cin.clear(); + cin.ignore(10000,'\n'); + registerme(); + }} + + +void exit(){ + exit(0);} + +void mainmenu(){ cout << "Hello, Would you like to log in or register\n[1] Login\n[2] Register\n[3] Exit" <> choice; do{ + cinfail = cin.fail(); + cin.clear(); + cin.ignore(10000,'\n'); + + }while(cinfail == true);{ + switch(choice){ + case 1: + login(); + break; + + case 2: + registerme(); + break; + + case 3: + exit();}}} + +main(){ +mainmenu(); +}