-3

I have a problem with my main part of the program. I am trying to implement the arguments in main with argc, argv that will receive as input txt files. Also when I try to read the files I receive an error like : Variable i is used without being initialised and I must click Abort.

The main part of the code is this one:

    void main(int argc, char* argv[])
{
    cout <<"<---------------FAZA 2--------------->" <<endl;
    cout << " Numar de argumente ale functiei main:" << argc << endl;
    for (int i = 0; i < argc; i++) 
    {
        if (argv[i] = "Angajat.txt")
        {
            Fisiere_Intrare f1;
            f1.Fisiere_Angajati();
            break;
        }
        else
            cout << " O.K." << endl;
    }

Fisiere Intrare is a class written like this:

 class Fisiere_Intrare
{
public:
    void Fisiere_Angajati()
    {
        ifstream fis;
        fis.open("Angajat.txt", ifstream::in);
        if (fis.is_open())
        {
            while (!fis.eof())
            {
                Angajat a;
                fis >> a;
                cout << a;
            }
        }
        fis.close();
    }
};

"Angajat" is also a class with the following atributes: name, salary, work_age.

Maker10
  • 1
  • 2
  • 8
    C++ can't be learned by guessing. Please have a look at this [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) list and the [cppreference.com](http://en.cppreference.com/w/) reference. – Ron Dec 26 '17 at 14:38
  • if (argv[i] = "Angajat.txt") is not what you meant. = is assignment. == is comparison. – KeithSmith Dec 26 '17 at 15:05
  • 2
    `argv[i] = "Angajat.txt"` is wrong in multiple ways. Even if you used the correct operator `==` it would be wrong because you would not be comparing strings. Instead you would be comparing pointers. – drescherjm Dec 26 '17 at 15:27
  • 2
    `while (!fis.eof())` https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – drescherjm Dec 26 '17 at 15:29
  • Why is the filename fixed in `Fisiere_Angajati()` maybe you wanted to get the filename from the command line parameter and use it in your function instead of the attempt to compare it to a fixed value. – drescherjm Dec 26 '17 at 15:32
  • 1
    BTW, the `main` function returns an `int`. Always. – Thomas Matthews Dec 26 '17 at 19:14

2 Answers2

2

There are several problems in your code:

if (argv[i] = "Angajat.txt")

As KeithSmith pointed out "=" is the assignment operator, for comparisons the "==" operator would be needed. Additionally C-Strings cannot be compared that easily in C/C++. As you tagged your question with c++, you could convert your argument into a std::string object and compare this with your filename:

if (std::string(argv[i]) == "Angajat.txt")

However, the error message you mentioned suggests that there might be another problem hidden somewhere.

the_summer
  • 425
  • 2
  • 10
  • 1
    Why use string::compare when you don't need three-way comparison? C++ strings are regular types, and can be compared like any other regular type: `std::string(argv[i]) == "Angajat.txt"` or even `argv[i] == "Angajat.txt"s` (..."sv if you got modern C++) – Cubbi Dec 26 '17 at 18:15
  • The `strcmp` function could be used and maybe simpler: `(strcmp(argv[i], "Angajat.txt") == 0)` – Thomas Matthews Dec 26 '17 at 19:15
  • @Cubbi: You are right. I forgot about the overloaded comparison operators. I updated the answer accordingly. – the_summer Dec 26 '17 at 20:15
  • @ThomasMatthews: Using strcmp is valid too, but as the question was tagged as C++ I decided for std::string. I updated the text a bit in this regard. – the_summer Dec 26 '17 at 20:16
-2

The argv[] is const char* and i is undetermined, which cannot be figured out by the complier.

haichuan
  • 1
  • 1