-4

So I'm trying to pass and command line argument into an array in my int main like this $< .\program nums.txt 8 but I'm getting errors and hit a dead-end with my knowledge on C++ code. (I'm relatively a new coder). Any help is greatly appreciated.

 int main(int argc,char *argv[])
    {
        string arg2=" ";
        arg2 = argv[2];
        int size; 
        size=stod(arg2);
        string arr[size];
        string file = argv[1];
        ifstream infile;
        if (getline infile.open(file))
        {
            int arr[size],val;
            for(int i=0;i<size;i++)
            {
                getline(file, arr[i]);
            }
        }
    int choice = 5,value,position,target ;

2 Answers2

1

There are lot of typos in your question. Still I think this code may help you:

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

int main(int argc, char *argv[]) {
    int size = atoi(argv[2]);
    std::vector<std::string> arr(size);
    std::ifstream infile(argv[1]);
    if (not infile) {
        std::cerr << "File not found!" << std::endl;
        return -1;
    }
    for (int i = 0; i < size; i++)
        if (infile)
            std::getline(infile, arr[i]);
        else {
            std::cerr << "Not enough lines in the file!" << std::endl;
            return -2;
        }
    // do rest of your things here ...
    return 0;
}
brc-dd
  • 10,788
  • 3
  • 47
  • 67
  • that makes sense but im supposed to do it without using vectors. do you think you could show me some code that would help me figure out how to do it in a way not using vectors – Bluehawk00 Apr 29 '20 at 19:19
  • @Bluehawk00 do you know the constraints of your size variable? – brc-dd Apr 29 '20 at 19:32
1

Her is a list of the errors you have (excluding a missing curly brace at the end, missing includes [you probably just didn't post them?] and missing std:: [the use of using namespace std; is considered bad practice ]) and mostly how to fix them:

  • std::stod parses and returns a dobule, but you assign the result to an int.

    size = std::stod(arg2);
    

    you probably meant to use stoi:

    size = std::stoi(arg2); 
    
  • VLAs (Variable length arrays) like

    std::string arr[size]; 
    

    are not part of the C++ standard. Instead use

    std::vector<std::string> arr(size);
    
  • When opening the file stream, your inention is right, but the syntax is wrong

    std::ifstream infile;
    if (std::getline infile.open(file))
    

    should be

    std::ifstream infile(file);
    if (infile)
    
  • Inside the if you have

    int arr[size],val;
    

    two unused variables, another VLA and most importantly int arr[size] will shadow string arr[size].

  • That's the reason for the next errors:

    std::getline(file, arr[i]);
    

    The first parameter file is a std::string, this should obviosly be infile. Because of the variable shadowing the second parameter arr[i] will refer to int arr[i]. Rename one of them or just remove the two unused variables (see last point).

  • Always check if reading was successfull. If you want to read a maximum of size lines use

    for(int i = 0; i < size && std::getline(infile, arr[i]); i++);
    

    It will terminate the loop, if getline fails for some reason, for example, when the file has less than size lines.

Lastly, you can think about some validation. For example check if there actually are two command line parameters given and add a try-catch-block around the stoi.

And just to mention it: Learning C++ by guessing is no fun. On https://en.cppreference.com/w/ you can find extensive information and many examples for the functions and classes you want to use.

Lukas-T
  • 11,133
  • 3
  • 20
  • 30