Creating Multiple Instances of a Variable C++ - c++

I'm attempting to make a tool for this Minecraft Chat Client where you can log into servers using batch files instead of launching the actual minecraft client. I'm attempting to make a tool that you could put your account details in and have it output the rest of the text needed in the batch file along with the server ip that you want. Also I'm sorry if what I'm asking for in the title isn't what I should be doing. I just needed something to put there to give someone a rough idea before looking at the thread of what I need help with.
Here is a picture I made in paint showing the basic layout. I'm not asking for help with the gui, I just made it to help visual people and just in case I'm making absolutely no sense.:
Minecraft Chat Client
http://www.minecraftforum.net/topic/1314800-winmaclinux-minecraft-console-client-175/
All I need help with is how to input lets say 25 minecraft accounts into that "Input Alts" box and have the program recognize how many alts are there and then simply output them in the format I have in the picture. I know how to do everything else using basic cin or cout. I was wondering if creating an array would be a good solution but I just don't know how to make the program recognize each account as its own separate identity. If it would be easier to just have a separate input where you could manually put the number of alts you are trying to output then I don't mind doing it that way.
Any help would be greatly appreciated.
I figured out how to do it with one account at a time but it takes too long.
#include <iostream>
#include <string>
using namespace std;
//Minecraft Account Details
string account = " ";
//Minecraft Server IP
string serverIPAddress = "";
int main()
{
cout << "Please Enter your email and password in the following format\n";
cout << "example#email.com password1\n";
// Lets say we input "example#email.com password1".
getline(cin, account);
// Lets just use "minecraftserver.com".
cout << "Please Enter The server IP";
getline(cin, serverIPAddress);
cout << "Minecraft.exe " << account << " " << serverIPAddress << endl;
//Output: Minecraft.exe example#email.com password1 minecraftserver.com
return 0;
}

I guess you want to read the user/pass details and then store them in a container. The code in main() could look like:
// input stuff
std::vector<std::string> accounts;
std::cout << "Please Enter The username and passwords\n";
for ( std::string temp; std::getline(std::cin, temp); )
accounts.push_back(temp);
std::string server_ip;
std::cout << "Please Enter The server IP\n";
std::getline(server_ip);
// output stuff
for ( auto&& acc : accounts )
std::cout << acc << ' ' << server_ip << '\n';

Related

Reading and writing to a .dat file in c++

I am currently learning to program in c++. I am making my way through a programming project I found online, and try and recreate it line by line looking up why certain things work the way they do. The project is a simple hotel booking system that has a menu system and saves users input i.e name, address, phone number etc.
I have been looking about trying understand what certain parts of this code do. I want to take a users input and save it to a .dat file, however it doesnt seem to work and im not sure why. Is there a better way to read and write to a text file.
This is the function that deals with checking if a room is free or reserved:
#include <fstream>
#include "Hotel.h"
int Hotel::check_availabilty(int room_type){
int flag = 0;
std::ifstream room_check("Room_Bookings.dat",std::ios::in);
while(!room_check.eof()){
room_check.read((char*)this, sizeof(Hotel));
//if room is already taken
if(room_no == room_type){
flag = 1;
break;
}
}
room_check.close();//close the ifstream
return(flag);//return result
}
This is the code that books a room:
#include "Hotel.h"
#include "check_availability.cpp"
void Hotel::book_a_room()
{
system("CLS");//this clears the screen
int flag;
int room_type;
std::ofstream room_Booking("Room_Bookings.dat");
std::cout << "\t\t" << "***********************" << "\n";
std::cout << "\t\t " << "THE GREATEST HOTEL" << "\n";
std::cout << "\t\t" << "***********************" << "\n";
std::cout << "\t\t " <<"Type of Rooms "<< "\t\t Room Number" "\n";
std::cout << "\t\t" << " Standard" << "\t\t 1 - 30" "\n";
std::cout << "\t\t" << " Luxury" << "\t\t\t 31 - 45" "\n";
std::cout << "\t\t" << " Royal" << "\t\t\t 46 - 50" "\n";
std::cout << "Please enter room number: ";
std::cin >> room_type;
flag = check_availabilty(room_type);
if(flag){
std::cout << "\n Sorry, that room isn't available";
}
else{
room_no = room_type;
std::cout<<" Name: ";
std::cin>>name;
std::cout<<" Address: ";
std::cin>>address;
std::cout<<" Phone No: ";
std::cin>>phone;
room_Booking.write((char*)this,sizeof(Hotel));
std::cout << "Your room is booked!\n";
}
std::cout << "Press any key to continue...";
getch();
room_Booking.close();
}
And this is the Hotel.h file
class Hotel
{
int room_no;
char name[30];
char address[50];
char phone[10];
public:
void main_menu();
void book_a_room();
int check_availabilty(int);
void display_details();
};
I dont fully understand what this part of the while loop does:
room_check.read((char*)this, sizeof(Hotel));
If you need any more info, please ask.
Any hints and tips towards making this better would be welcomed.
Hotel is an entirely self-contained type with no heap allocations or references to external objects. Therefore, one can serialize its state by simply writing out the object's representation in memory, and deserialize by doing the opposite.
room_check.read((char*)this, sizeof(Hotel));
This line of code asks the room_check input stream to read sizeof(Hotel) bytes and store them directly where the Hotel object pointed to by this lives. Effectively, you're restoring the memory contents as they were before being written to disk.
(Note that (char*)this is better written as reinterpret_cast<char *>(this) in C++.)
That's the inverse of this operation:
room_Booking.write((char*)this,sizeof(Hotel));
There's some advantages to serializing this way instead of creating your own data structure.
It's really easy; with one line of code you can serialize, and with another you can deserialize.
Serialization and deserialization is very fast since there is no parsing or conversion happening.
However, there are also some disadvantages:
The on-disk format is dictated by the layout of objects in memory. If you reorder or change any class data members, old serialized objects will not load correctly any more. Moreover, the read operation will succeed but you'll be left with a garbage object state.
You depend on the endianness of the host machine for number types. A data file created on a little-endian machine will be useless on a big-endian machine.
It's very easy to accidentally create a security vulnerability. For example, with just this code, an attacker could easily craft a .dat file that causes out-of-bounds reads when you go to read the "string" (character array) members by simply not NUL-terminating any of those arrays.
Using a different serialization mechanism, such as leveraging JSON, XML, protocol buffers, etc. requires more work but the results are more portable because your data structure on disk is no longer tied to the object's layout in memory.
when doing
room_check.read((char*)this, sizeof(Hotel));
you are reading from the stream, and write in the buffer, but you need to specify how many characters must be read...
room_check contains the data as a stream. room_check.read((char*)this, sizeof(Hotel)); reads the data from the stream and stores it in the current instance of Hotel (this). sizeof(Hotel) tells the function how many bites should be read from the stream.
room_check contains the data of the class Hotel in the order it is listed in the class declaration:
int room_no;
char name[30];
char address[50];
char phone[10];
With this declaration the byte-size of an instance of hotel is known: sizeof(1*int + 30*char + 50*char + 10*char). The content of the dat-file is stored int the members of this very current instance of Hotel.

ShellExecute - ERROR code 5

I am using Notepad++ with TDM-GCC. My computer is 2Gb RAM Windows 10 32 bit 3.30 GHz. When I execute my simple program, it shows error.
Access is denied.
An attempt was made to execute the below command.
Command: D:\Deane\Github\CPP_Projects\AnalysisName\main.bat
Arguments:
Error Code: 5
Image of the error
I follow this: ShellExecuteEx function always returning error code 5 (C++)
Program's code (if necessary):
/* AnalysisName Program - written by Vo Tran Nha Linh */
#include <iostream> // Input and Output library.
using namespace std;
int main()
{
string name;
cout << "Hello friend! It's nice to meet you, what is your name?" << endl; // Ask the name.
cin >> name; // Input name.
cout << "Hello " << name << ". Your name is interesting." << endl; // Have a greeting.
cout << "Your name has " << name.length() << "letters." << endl; // Show the name's length.
cout << "It starts with " << name.front() << "letter." << endl; // Show the first letter of the name.
cout << "It ends with " << name.back() << "letter." << endl; // Show the last letter of the name.
return 0;
}
But it doesn't active, please give me a help. Thank you very much!
My problem solved!
I miss Visual C++ Redistributable 2008 and 2010.
Moderators please close my topic. Thank you!
Navigate to C:\Program Files (x86)\Notepad++ Right mouse click the Notpad++.exe file click properties & under the compatability Tab UN-TICK run the program as administrator box DONE.
Refer to this link.
this solved it for me:
right click on the file that won't run (in my case a .cmd file)
check the 'Unblock' checkbox next to the remark "This file came from another computer and might be blocked to help protect this computer"

clearing out extra input from terminal

Here is an example code demonstrating the problem I'm facing.
#include <iostream>
#include <string>
extern "C" {
#include <unistd.h>
}
int main()
{
std::cout << "Making tests ready!" << std::endl;
std::cout << "\nTo start out, Enter an integer: ";
int a = 0;
std::cin >> a;
std::string input;
sleep(3); // what to do if user enters data during this?
std::cout << "\n Now enter a string";
std::getline(std::cin, input);
std::cout << "\nHere are your values - " << a << " & " << input;
return 0;
}
See the sleep call in between the code? This could be replaced with somewhat long delays while computing something when my program isn't accepting any inputs. Now if user presses some keys during this time, that input is captured by std::getline() in next line of code. I know this is the default behavior since it should capture the input being provided.
But what I want is to clear all that captured input and start fresh with 15th line that is std::cout << "\n Now enter a string";, which is immediately after sleep. I don't know exact term to describe this or else I would have used that. Thanking you.
Edit: I've tried using std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); in my code, but it asks for input and then discards it.
Please mind my english, not a native speaker.
Reading the comments causes me to think that you can't really solve this problem (at least by the means suggested there). There's an inherent race condition in any case. Consider the following lines:
sleep(3);
// (*) <- Potential location 1.
std::cout << "\n Now enter a string";
// (**) <- Potential location 2.
std::getline(std::cin, input);
The various comments show some (very technically-competent) ways to flush the standard input. The problem is, you cannot put them in the location marked (*) nor (**).
First location - you clear the standard input some way. Now you decide it's time to move to the next line (std::cout << ...). While you do that, the user types in some more input. Race!
Second location - You print out the message (std::cout << ...), and proceed to clear the standard input. Before you manage to do that, the user typed in something. Race!
It seems to me that any of the techniques described in the comment require locking the standard input, and I don't think there's a (standard) way to do so.

How do I make a C++ program that opens another program? [duplicate]

This question already has answers here:
How to get the current user's home directory in Windows
(4 answers)
Closed 7 years ago.
So I know how to make it, I just want it to open the file without specifying the path.
For example: I have it in
C:\Users\\(me)\Desktop\Projects\BCs\BSCV2\bin\Debug\BSC.exe
but if I give it to a friend, he has a different username, (him) for example, so the command won't be able to execute even if he has it on his desktop because the path isn't valid anymore.
Here's a part of the code:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
int a;
cout << endl;
cout << " This window is used for launching the programs." << endl;
cout << " Type in the number of the program you want to use and press Enter." << endl;
cout << endl;
cout << " 1) BSCV2 << endl;
cout << endl;
cout << " "; cin >> a; cout << endl;
cout << endl;
if (a == 1){
system ("start C:\\Users\\(me)\\Desktop\\Projects\\BCs\\BSCV2\\bin\\Debug\\BSCV2.exe");
system ("pause");
}
return 0;
}
How can I make it run on anyone's PC, regardless of where they put it?
Also, if you could re-write my code as an example, I'd appreciate it.
You will need to get the "home" directory for the current user logged in. Reference this post: How to get the current user's home directory in Windows, or this one: How can I find the user's home dir in a cross platform manner, using C++?
However, are you absolutely sure that all users (on their respective machines) running your application will have the exact directory path to the executable you're trying to call (\Desktop\Projects\BCs\BSCV2\bin\Debug\BSCV2.exe)?
You may be better off writing a function to search for the executable, or ask the user to specify where it is.

How do I make my previous messages disappear in Visual Studio 2013 console applications? (C++)

So if I write a piece of code like this:
string name, feeling;
cout << What is your name?" << endl;
cin >> name;
cout << "Hello, " << name << "!"<<endl;
cout << "So how are you feeling today?" << endl;
cin >> feeling;
I get the output:
What is your name?
James (input from user)
Hello, James!
So how are you feeling today?`
But I want it to remove the first message and the input, so the user will get just this on the console window:
Hello, James!
So how are you feeling today?
As long as you stay on the same line, it's usually pretty easy to use a combination of \b (back-space) and/or \r (carriage return without new-line) and some spaces to go back to the beginning of a line and write over what's displayed there.
If you need to do (much) more than that, you can use the Windows console functions such as SetConsoleCursorPosition and FillConsoleOutputCharacter to write data where you want it, overwrite existing data, etc.
If you care about portability to Linux and such, or already know how to program with curses, you might prefer to use PDCurses instead. This is basically a re-implementation of the ncurses programming interface on top of the Windows console functions.
If you work on windows environment, try this
#include <iostream>
int main()
{
std::cout << "This is the first line";
system("cls");
std::cout << "This is the line on clear console" << std::endl;
return 0;
}