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

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.

Related

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"

Creating Multiple Instances of a Variable 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';

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;
}

Files created in C++ can't be found [duplicate]

This question already has answers here:
C++: Where does the ofstream class save the files to?
(4 answers)
Closed 8 years ago.
I want to use C++ to create files which I can then export for use elsewhere. The following code seems to create files okay, in the sense that I can write data into the file and then read it again later in a C++ program. When I try to actually find created file so as to use it, however, it is nowhere to be found..
using namespace std;
int main () {
ofstream myfile ("example3.dat");
if (myfile.is_open()){
myfile << 3335 << " " << 64 << " " << 43 << 9 << 5 << 6 << 5 << 4 << 6;
myfile.close();
}
else cout << "Unable to open file";
ifstream myfile2 ("example3.dat");
int b;
myfile2 >> b; cout <<b;
myfile2 >> b; cout <<b;
myfile2.close();
return 0;
}
The files will be created in the process working directory.
In case the process is being run from an IDE (e.g. Visual Studio), the working directory can be different from the executable file path. You should check the project properties to find out the actual path.
Certainly you can search for the file as suggested in comments above, but you might be better off specifying an absolute path in your program, so that you know where it's being written.
The formatting of the path is OS-specific, but maybe
/tmp/example.dat
C:\Windows\Temp\example.dat
for Linux and Windows respectively (but you will need to decide for yourself; these are just examples).

Trying to program for y in y=mx+b, but not working?

I wanted to start learning how to program, so I asked my math professor if he had a book that I could borrow. He did and so I have been reading a C++ book from ~1994 (it still has a floppy disk :P). Anyway, I made it to a point in it and it sets up a program that calculates y in y=mx+b. Pretty simple, but I decided to try it out and it is not working. I would really like to figure out why it is not working and fix it.
Here is the code for it:
#include <iostream>
using namespace std; //not in the book: added by me after some Googling
int main() {
cout << "Input m: " << flush;
int m;
cin >> m;
cout << "Y-intercept: " << flush;
int b;
cin >> b;
cout << "X coordinate of interest: " << flush;
int x;
cin >> x;
int y;
y = m * x + b;
cout << "y = " << y << "when m = " << m << "; " << "b = " << b << "; x = " << x << endl;
}
edit: Sorry. Forgot to describe what was going on. lol. The program executes properly until it it comes to displaying the final line. After submitting "X coordinate of interest: " the program simply exits. I mean I am no expert in C++, but should the final cout write to the console?
And I know it is really outdated, but I really just want a platform to stand on when I begin to look at the newage languages. The book itself is only about 700 pages, and there is a LOT of explaining in it, so it is not very much code wise. I have probably 10 to 20 700 page pdfs on Java and C#/C++/C all written within the past six years. So I'll be good. Just want a starting point. :) Plus this book explains a lot about how a computer works and certain jargon that some of the newer books just don't.
This is a common windows cmd problem. Either run the program through cmd, type in the executable name, or add getchar or cin >> variable to the end of the program.
Assuming this is in Visual Studio when you run a program with debugging (F5) the console instance is closed automatically. You can either add a input line to the end of the program as others have mentioned or run the program without debugging (Ctrl+F5) and the console window will pause and let you see the output at the end of the programs execution.