简体   繁体   中英

file not opening via terminal using g++ file is in same directory

i made a program that processes a file with the name passed in by the user. when compiling through an IDE i get the correect outputs, but when im compiling and running through the MacOS terminal using g++, the program doesnt catch the entered filename so im stuck in the while loop and the rest of the code wont run.

    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <iomanip>
    using namespace std;
    
    double numrows(string name)
    {
      double index = 0;
      string currentline;
      fstream myFile;
      myFile.open(name);
      while(myFile)
        {
          getline(myFile, currentline);
          index++;
          if(myFile.eof())
          {
          myFile.clear();
          myFile.seekg(0,ios::beg);
          break;
          }
        }
      return index;
    }
    
    double findaverage(double numrows, double sum)
    {
      return (sum/numrows);
    }
    
    bool checkifvalid(string victim)
    {
       
      for(auto &ch: victim)
        if(!(isdigit(ch)))
        {
          return false;
        }
      return true;
    }
    
    int main() 
    {
      string currentline, number,NaN;
      double largest = 0.0;
      int index = 0;
      double validcount =0.0;
      double firstcolsum = 0;
      fstream myFile;
    
      bool userinput = false;
      string filename;
    
      cout << "please input file name :";
      cin >> filename;
      
      myFile.open(filename);
      while(!myFile)
      {
        cout << "file not found please try again.."  ;
        cin >> filename;
        myFile.open(filename);
      }
      double size = numrows(filename);
    
      while(myFile)
      {
        getline(myFile, currentline);
        stringstream ss(currentline);
        
      while(getline(ss, number, ','))
        {
    
          bool valid = checkifvalid(number);  // checking if number is valid (in string) 
          if(valid == false)
          {
            NaN += number + ",";
          }
          else
          {
            double num = stoi(number);
            if(index%2 == 0)             //check if index even or odd if even then its first row, viceversa 
            {
              firstcolsum += num;
              validcount++;
            }
            else
            {
              if(num > largest)
                largest = num;
            }
      
          
        }
          index++;
        
      }
        if(myFile.eof())
          break;
        }
      double average = findaverage(validcount, firstcolsum);
    
      cout << fixed<< setprecision(4);
      cout << "The average value of all numbers in the first column: " << average << endl;
      cout<< setprecision(0);
      cout << "The largest value of all numbers in the second column: " << largest<<  endl;  
      cout << "The total number of rows in the file is: " << size <<  endl;
      cout << "The invalid numbers are: " << NaN << endl;   
      }`

is my current code and it works via other IDE's but not g++

i tried making new folders and sources but nothing seems to be working.

Make sure you have navigated into the folder containing the source and input files using the 'cd' command, and that the name of the input file does not contain any spaces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM