-
-
Save Maxime66410/a3f455bfc2b1f08a22ec353c3ad8bb94 to your computer and use it in GitHub Desktop.
Get User and Note => Average => To JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fstream> | |
#include <iostream> | |
#include <windows.data.json.h> | |
#include "single_include/nlohmann/json.hpp" | |
using namespace std; | |
void save_to_json(string* userInfo, float* nbrEnter) | |
{ | |
cout << "Enregistrement des informations en cours..." << endl; | |
Sleep(1000); | |
// Check if file exists "user.json" | |
ifstream file("user.json"); | |
if (file.good()) | |
{ | |
// If the file exists, delete it | |
remove("user.json"); | |
} | |
else | |
{ | |
// If the file does not exist, we create it | |
ofstream file("user.json"); | |
} | |
// Creating a json object | |
nlohmann::json j; | |
// Adding information to the json object | |
// Create a "user information" category in the json object | |
j["user informations"] = { | |
// Added information in the “user information” category | |
{ "Nom", userInfo[0] }, | |
{ "Prenom", userInfo[1] }, | |
{ "Age", userInfo[2] } | |
}; | |
// Create a "notes" category in the json object | |
j["notes"] = { | |
// Adding notes to the “notes” category | |
{ "Note 1", nbrEnter[0] }, | |
{ "Note 2", nbrEnter[1] }, | |
{ "Note 3", nbrEnter[2] }, | |
{ "Moyenne", (nbrEnter[0] + nbrEnter[1] + nbrEnter[2]) / 3 } | |
}; | |
// Writing to json file | |
ofstream o("user.json"); | |
o << j << endl; | |
// Closing the json file | |
o.close(); | |
// Displaying a confirmation message | |
cout << "Enregistrement terminer !" << endl; | |
} | |
void GetUserInfos() | |
{ | |
string userInfo[] = { "Nom", "Prenom", "Age" }; | |
string* userInfoPointer; | |
float nbrEnter[] = { -1.0, -1.0, -1.0 }; | |
float* nbrEnterPointer; | |
// Information is requested from the user | |
for (int i = 0; i < 3; i++) | |
{ | |
cout << "Entrez votre " << userInfo[i] << " : "; | |
cin >> userInfo[i]; | |
} | |
userInfoPointer = userInfo; | |
// Clear console | |
system("cls"); | |
// We ask the user for notes | |
for (int i = 0; i < 3; i++) | |
{ | |
cout << "Entrez la note " << i + 1 << " : "; | |
cin >> nbrEnter[i]; | |
} | |
nbrEnterPointer = nbrEnter; | |
// Clear console | |
system("cls"); | |
// Show information | |
cout << "Nom : " << userInfo[0] << endl; | |
cout << "Prenom : " << userInfo[1] << endl; | |
cout << "Age : " << userInfo[2] << endl; | |
cout << "La moyenne est de : " << (nbrEnter[0] + nbrEnter[1] + nbrEnter[2]) / 3 << endl; | |
// Added delay before sending information to the save_to_json function | |
save_to_json(userInfo, nbrEnterPointer); | |
} | |
int main(int argc, char* argv[]) | |
{ | |
GetUserInfos(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment