dont know how and wich variables use

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
Use strcat() to "add" c-strings.
http://cplusplus.com/reference/clibrary/cstring/strcat/

Oh, and I think you should actually be using
int main(int argc, char * argv[])

-Albatross

EDIT: 10 posts, and counting.
Last edited on
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.
thnx it helped, now another problem is that in windows i can compile and run..

code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;
int main(int iA, char **cA)
{
        
        char str[90];
        strcpy (str,"txt1 ");
        strcat (str,cA[1]);
        strcat (str," txt2");
        strcat (str,cA[1]);
        strcat (str,"txt3");
        cout << str << endl;
        

        return 0;
}


in linux im geting error`s like

1
2
3
4
temp.cpp:3: error: second argument of āint main(int, char*)ā should be āchar **ā
temp.cpp: In function āint main(int, char*)ā:
temp.cpp:6: error: āstrcpyā was not declared in this scope
temp.cpp:7: error: āstrcatā was not declared in this scope


im using simple bash script to compile in linux

1
2
3
4
#/bin/sh

echo compiling C++ using -ansi -pedantic-errors -Wall
g++ -ansi -pedantic-errors -Wall $1 $2 $3


Meaby there is any simple compiler for linux??
I'll just repeat myself:
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.


You don't use strcpy or strcat in C++.
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
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
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.
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**).
However, if you use argc and argv, everyone who has used either will immediately know what you're doing.

-Albatross
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(&timestamp);
  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
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.
Topic archived. No new replies allowed.