May 31, 2010 at 10:02pm UTC
hello, im new in c++ and im trying to make simple script that will take some info from console and then it must just post it back.
the code
#include <iostream>
using namespace std;
int main(int iA, std::string **cA)
{
std::string pr1 = "txt1";
std::string pr2 = "txt2";
std::string pr3 = "txt3";
std::string pr4 = cA[1];
char komanda1 = pr1+" "+pr4+" "+pr2+pr4+pr3;
system(komanda1);
return 0;
}
and error log
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Makefile.win"
Executing make...
make.exe -f "C:\Dev-Cpp\Makefile.win" all
g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
main.cpp: In function `int main(int, std::string**)':
main.cpp:8: error: conversion from `std::string*' to non-scalar type `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' requested
main.cpp:10: error: cannot convert `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to `char' in initialization
main.cpp:11: error: invalid conversion from `char' to `const char*'
main.cpp:11: error: initializing argument 1 of `int system(const char*)'
main.cpp:13: error: expected `;' before "return"
make.exe: *** [main.o] Error 1
Execution terminated
if im using char like this.
#include <iostream>
using namespace std;
int main(int iA, char **cA)
{
char* pr1 = "txt1";
char* pr2 = "txt2";
char* pr3 = "txt3";
char pr4 = cA[1];
char* komanda1 = pr1+" "+pr4+" "+pr2+pr4+pr3;
system(komanda1)
return 0;
}
then error:
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Makefile.win"
Executing make...
make.exe -f "C:\Dev-Cpp\Makefile.win" all
g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
main.cpp: In function `int main(int, char*)':
main.cpp:10: error: invalid operands of types `char*' and `const char[2]' to binary `operator+'
make.exe: *** [main.o] Error 1
Execution terminated
thnx for any coments and sory for bad english
May 31, 2010 at 10:06pm UTC
Last edited on May 31, 2010 at 10:09pm UTC
May 31, 2010 at 10:17pm UTC
The first example would work if you had used char** instead of std::string** as the second parameter in main().
You can't choose main's parameter types.
May 31, 2010 at 10:57pm UTC
Okay... detail. To use strcpy and strcat, you need to #include <cstring>
Ideally, however, you'd convert what you named cA to an array of strings.
-Albatross
May 31, 2010 at 11:04pm UTC
Okay. Consider this as the only correct way to do this, for simplicity's sake:
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
using namespace std;
int main(int argc, char ** argv)
{
string pr1 = "txt1" ;
string pr2 = "txt2" ;
string pr3 = "txt3" ;
string pr4 = argv[1];
string komanda1 = pr1+" " +pr4+" " +pr2+pr4+pr3;
system(komanda1.c_str());
}
Last edited on May 31, 2010 at 11:05pm UTC
May 31, 2010 at 11:15pm UTC
in my examples i was using
char **cA
char is type argv in this exmpl name of variable
nvrmd :) need to read more handbook`s and tutorials
thnx for help.
May 31, 2010 at 11:17pm UTC
It's not actually necessary that you name the parameters argc and argv, iA and cA are possible (but unusual).
It's just the types of those two parameters that you can't change (int and char**).
May 31, 2010 at 11:18pm UTC
However, if you use argc and argv, everyone who has used either will immediately know what you're doing.
-Albatross
Jun 1, 2010 at 11:20am UTC
hello, another problem with variables...
i added coment in that place where is problem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int main(int argc, char ** argv)
{
char t=0x20;
int ip;
cin >> ip;
string pr1 = "txt1" ;
string pr2 = "txt2" ;
string pr3 = "txt3" ;
string pr4 = argv[2];
string pr5 = argv[1];
string pr6 = "txt4" ;
string pr7 = "txt5" ;
string pr8 = "txt6" ;
string pr9 = "txt7" ;
string pr10 = "txt8" ;
string pr11 = "txt9" ;
string pr12 = "txt10" ;
string pr13 = "txt11" ;
string pr14 = "txt12" ;
string pr15 = argv[3];
string pr16 = "txt13" ;
string pr17 = "txt14" ;
string pr18 = "txt15" ;
string pr19 = argv[4];
string komanda1 = pr1+t+pr4+t+pr2+pr5+pr3;
string komanda2;
if (ip == 1)
{
komanda2 = pr6+t+pr4+t+pr7+t+pr9+pr4+t+pr8;
}
else
{
komanda2 = pr6+t+pr4+t+pr7+t+pr10+pr4+t+pr8;
}
string komanda3 = pr6+t+pr4+t+pr11+t+pr12+pr4+pr13+t+pr8;
string komanda4 = pr6+t+pr4+t+pr14;
string komanda5 = pr6+t+pr4+t+pr16+t+pr17+pr15;
string komanda6 = pr18+t+pr4;
system(komanda1.c_str());
system(komanda2.c_str());
system(komanda3.c_str());
system(komanda4.c_str());
system(komanda5.c_str());
system(komanda6.c_str());
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "hello," << endl;
cout << "some txt" << pr5 << " some txt:" << endl;
if (ip == 1)
{
cout << "IP: " << pr9 << pr4 << endl;
}
else
{
cout << "IP: " << pr10 << pr4 << endl;
}
time_t timestamp;
timestamp = time(NULL);
struct tm *rtime = localtime(×tamp);
cout << "Now is " << rtime->tm_year + 1900 << "." << rtime->tm_mon + 1 << "." << rtime->tm_mday << " - " << rtime->tm_hour << ":" << rtime->tm_min << endl;
int men;
men = rtime->tm_mon + 1;
// PROBLEM THERE, dont know why, but if wont understand that 0 meaby there must be null or smthng like it
if (argv[4] == 0)
{
cout << "if argv4 empty we need to add only 1 month " << rtime->tm_year + 1900 << "." << men + 1 << "." << rtime->tm_mday << endl;
}
else
{
// PROBLEM THERE, I HAVE STRING argv[4] AND INT men, i need to sum men with argv[4] how to do it?
cout << "after argv4 month will be " << rtime->tm_year + 1900 << "." << men + argv[4] << "." << rtime->tm_mday << endl;
}
return 0;
}
i think that coz if (argv[4] == 0) this is null im geting this error
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct NULL not valid
Aborted
bcoz when argv[4] is empty then apears this error when i enter any value then everithing works
and im geting result 2010.=xterm.1 if i sum like this
men + argv[4]
thnx for helping
Last edited on Jun 1, 2010 at 11:21am UTC
Jun 1, 2010 at 3:09pm UTC
You obviously have to check first whether the argument you're trying to access actually exists.
You can use argc for that, it contains the number of arguments.