Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am new to C++ and visual studio, can anyone please help me in creating an UI for below requirements?
Requirement: There is a .txt file which consists of 10 test cases. In the UI, I(tester) should be able to select his/her choice of test cases to run.
Try using a MFC project. You can find drop-down menus in GUI toolbox.
Learn further in MFC.
refer this - https://www.youtube.com/watch?v=6hSYZdvQ3s4&index=1&list=WL
Consider the possibility to use command line interface as the following template shows:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
// check that parameter is given in command line when program is run
if(argc != 2) // if number of parameters is insufficient
{ // explaine and stop
cout << "Please, run a program with parameter: name of file with testcases" << endl;
cout << "For example:" << endl << " " << argv[0] << " tests.txt" << endl;
return 1;
}
// try to open file with test cases
ifstream testfile;
testfile.open(argv[1], ifstream::in);
if( !testfile.is_open() )
{
cout << "ERROR: File " << argv[1] << " cannot be open!" << endl;
return 2;
}
// read test cases from file
string testCaseName;
char answer;
while( getline(testfile, testCaseName).good() )
{
if(testfile.eof())
{
break;
}
if(testCaseName.length() == 0)
{
continue;
}
cout << "Would you like to execute testcase " << testCaseName << " ? (Y/N): ";
cin >> answer;
if( answer == 'Y' || answer == 'y')
{
cout << "Test execution... " << endl;
// run test case
// . . .
// report test results
// . . .
}
}
return 0;
}
To run your test with such program you should type in the command line:
programname test_file.txt
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to write a simple backup program. Its not finished yet, but i've encountered a problem: My class responsible for setting the right path wont execute the worker which will copy the file. I don't know why and yes - i already looked up on any helping site i know. Here is my filecopy h code:
#ifndef __FILECOPY_H_INCLUDED__
#define __FILECOPY_H_INCLUDED__
#include<iostream>
#include<fstream>
#include<ctime>
class filecopy
{
std::string dest_path;
std::string src_path;
public:
filecopy(std::string, std::string);
void filecopy_worker()
{
std::cout << "FILECOPY PROCESS STARTED" << std::endl;
std::ifstream source(src_path);
std::ofstream dest(dest_path);
dest << source.rdbuf();
source.close();
dest.close();
}
};
filecopy::filecopy(std::string a, std::string b)
{
dest_path = a;
src_path = b;
}
#endif
And here my main.cpp code:
#include<iostream>
#include<stdlib.h>
#include"filecopy.h"
int main(int argc, char *argv[])
{
if(argc != 3)
{
std::cout << "USAGE: " << argv[0] << " <filesource>" << std::endl;
return 1;
}
else
{
filecopy target1(argv[2], argv[1]);
std::cout << "TARGET ASSIGNED" << std::endl;
std::cout << "EXECUTE FILEWORKER" << std::endl;
}
return 0;
}
It didn't execute the function because you never called it. Just add that function call
filecopy target1(argv[2], argv[1]);
target1.filecopy_worker();
I am new to Protocol Buffers and inexperienced with C++, I am trying to complete the tutorial at https://developers.google.com/protocol-buffers/docs/cpptutorial
I've created the proto file mentioned in the tutorial and gotten addressbook.pb.h and addressbook.pb.cc from this proto. I am trying to follow the segment "Writing A Message", so I copied and pasted the following code from the tutorial. I immediately run into an issue in the main function, which I'll explain below:
#include <iostream>
#include <fstream>
#include <string>
#include "addressbook.pb.h"
using namespace std;
// This function fills in a Person message based on user input.
void PromptForAddress(tutorial::Person* person) {
cout << "Enter person ID number: ";
int id;
cin >> id;
person->set_id(id);
cin.ignore(256, '\n');
cout << "Enter name: ";
getline(cin, *person->mutable_name());
cout << "Enter email address (blank for none): ";
string email;
getline(cin, email);
if (!email.empty()) {
person->set_email(email);
}
while (true) {
cout << "Enter a phone number (or leave blank to finish): ";
string number;
getline(cin, number);
if (number.empty()) {
break;
}
tutorial::Person::PhoneNumber* phone_number = person->add_phone();
phone_number->set_number(number);
cout << "Is this a mobile, home, or work phone? ";
string type;
getline(cin, type);
if (type == "mobile") {
phone_number->set_type(tutorial::Person::MOBILE);
} else if (type == "home") {
phone_number->set_type(tutorial::Person::HOME);
} else if (type == "work") {
phone_number->set_type(tutorial::Person::WORK);
} else {
cout << "Unknown phone type. Using default." << endl;
}
}
}
// Main function: Reads the entire address book from a file,
// adds one person based on user input, then writes it back out to the same
// file.
int main(int argc, char* argv[]) {
// Verify that the version of the library that we linked against is
// compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc != 2) {
cerr << "Usage: " << argv[0] << " ADDRESS_BOOK_FILE" << endl;
return -1;
}
tutorial::AddressBook address_book;
{
// Read the existing address book.
fstream input(argv[1], ios::in | ios::binary);
if (!input) {
cout << argv[1] << ": File not found. Creating a new file." << endl;
} else if (!address_book.ParseFromIstream(&input)) {
cerr << "Failed to parse address book." << endl;
return -1;
}
}
// Add an address.
PromptForAddress(address_book.add_person());
{
// Write the new address book back to disk.
fstream output(argv[1], ios::out | ios::trunc | ios::binary);
if (!address_book.SerializeToOstream(&output)) {
cerr << "Failed to write address book." << endl;
return -1;
}
}
// Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
In the main function the code exits without prompting for any inputs due to this portion:
if (argc != 2) {
cerr << "Usage: " << argv[0] << " ADDRESS_BOOK_FILE" << endl;
return -1;
}
which appears as one of the first few lines in the function, before any inputs are requested. I read that argc will be the number of inputs. I'm confused because I copied and pasted exactly what the tutorial wrote, but it doesn't appear to be running correctly.
That code expects a file name to be passed on the command line, not read from stdin. You've not specified what platform you're on, but you'll do something like my_program.exe C:\some\file\somewhere on Windows or ./my_program /some/file/somewhere on Linux/Mac/Other Unix like OS. If you're running the program from your IDE's run/debug function then you'll need to configure it to pass the name of the file as a command line argument. How to do that will depend on what IDE you're using.
This question already has an answer here:
gnuplot-cpp cannot feed command to pipe
(1 answer)
Closed 8 years ago.
I'm trying to implement the code below from this site:
https://sites.google.com/site/bettereaclone/introduction/gnuplot/c-example-gnuplot-1
Code:
#include <iostream>
#include "gnuplot_i.hpp"
#include <windows.h>
#include <conio.h>
using std::cout;
using std::endl;
int main(int argc, char* argv[])
{
try
{
Gnuplot g1("lines");
cout << "*** plotting slopes" << endl;
g1.set_title("Slopes\\nNew Line");
cout << "y = x" << endl;
g1.plot_slope(1.0,0.0,"y=x");
cout << "y = 2*x" << endl;
g1.plot_slope(2.0,0.0,"y=2x");
cout << "y = -x" << endl;
g1.plot_slope(-1.0,0.0,"y=-x");
g1.unset_title();
}
catch (GnuplotException ge)
{
cout << ge.what() << endl;
}
return 0;
}
I installed the gnuplot (http://www.gnuplot.info/download.html)
gnuplot_i.hpp file (https://code.google.com/p/gnuplot-cpp/source/browse/trunk/gnuplot_i.hpp)
When I run this code, I get this problem:
The error:
'C:/Program' is not recognized as an internal or external command,
operable program or batch file.
I've no idea why. thanks!!
Looks like path to gnuplot is malformed, not in quotation marks or simply not read properly.
Try forcing the path by uncommenting this line and correcting it to point gnuplot directory on your machine:
// Gnuplot::set_GNUPlotPath("C:/program files/gnuplot/bin/");
This question already has answers here:
launch an exe/process with stdin stdout and stderr?
(3 answers)
Closed 8 years ago.
I need some help on this subject.
I have a C++ .exe that I want to open with C++ and then write some arguments in the console.
Here is an example of what I want to do:
Lets assume an executable whatsyourage.exe with this code (in reallity, I don't have the corresponding code):
#include <iostream>
using namespace std;
int main()
{
int age = 0;
cout << "What's your age ?" << endl;
cin >> age;
cout << "You are " << age << " year !" << endl;
return 0;
}
and I want to do something like:
int main()
{std::string invit="21";
std::string chemin ="whatsyourage.exe";// in the same library
std::string const command = chemin+" "+ invit;
system(command.c_str());
}
I want to write the age (21).
Can someone please help me?
Here is the answer:
int main()
{std::string invit="21";
std::string chemin ="whatsyourage.exe";
FILE* pipe = _popen(chemin.c_str(), "w");
if (pipe == NULL) {
perror("popen");
exit(1);
}
fputs("30", pipe);// write the age into the pipeline
pclose(pipe); // close the pipe
}
The popen() function from POSIX does what you are looking for. It allows you to execute a program (like system) while getting a file handle on its input/output streams.
For Windows, if popen() is not available, you can use the CreatePipe() & co functions to do the same thing; check out this question for some pointers.
The second snippet you added is good, and the problem is with the first code. In order to handle the command line from a program, you have to define the main as int main(int numberOfArguments, char* arguments[]) (often people use the shorter version - main(int argc, char* argv[]), but you can name the arguments as you wish). Then, if you pass an argument to the function, it will be in argv[1], since argv[0] is always the path to the executable. So the first program should look like this:
int main(int numberOfArguments, char* arguments[])
{
if(numberOfArguments>=2)
cout << "You are " << arguments[1] << " year !" << endl;
else
cout << "Too few arguments passed!" << endl;
return 0;
}
I am trying to run the following program:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inFile;
ofstream outFile;
double first=1.49, second=2.59, third=3.69, fourth=4.79;
inFile.open("prices.txt");
char response;
if(!inFile.fail())
{
cout << "A file by the name prices.txt exists.\n" << "Do you want to continue and overwrite it\n" << " with the new data (y or n);"; cin >> response;
if(tolower(response) == 'n')
{
cout << "The existing file will not be overwritten." << endl;
return -1;
}
}
outFile.open("prices.txt");
if (inFile.fail())
{
cout << "\n The file does not exist and can not be opened" << endl;
cout << "The file has been successfully opened for output." << endl;
outFile << first << "\n" << second << "\n" << fourth << "\n" << endl;
outFile.close();
exit(1);
cout << "The file has been successfully opened for output. " << endl;
outFile << first << "\n" << second << "\n" << third << "\n" << fourth << endl;
outFile.close();
return 0;
}
}
Yet this program will not write the values to the prices.txt file. If you run the program once it says the file does not exist. Running it a second time says the file is already there and if you want to overwrite it. The thing is searching my Mac I cannot find this file anywhere.
Any ideas what I am doing wrong with running it in Xcode? A friend runs the exact same code in Visual Studio 2008 and it works. Any help is appreciated.
You need to set the working directory for the executable since you are assuming that your data files are in the current working directory. In Xcode 3.x you set this in the first tab of Get Info for the executable. In Xcode 4.x it has been moved, but the principle is the same.
Alternatively you can change your data file paths (e.g. make them absolute) so that you do not make assumptions about the current working directory.
You may not have permission to write into the directory that you are trying to save the file too.
Also, there is an error in your program and I am sure if it is there for debugging reasons. You have
outFile.close();
exit(1);
But then shortly there after you try to write to the file, then close it again.