Why do I get error when ifstream is creating file with string read from keyboard?

s_diaconu

I am doing exercises from Bjarne Stroustrup's book, Programming Principles and Practice Using C++. I am on the first exercise in Chapter 10, where it says to write a program that produces the sum of all the numbers in a file of whitespace-separated integers. I based my code below on what's used for Exercise 2 of Chapter 10.5. I get an error when the ifstream object is created. Here is the code I am trying to run:

#include "../../std_lib_facilities.h"

int main(int argc, const char * argv[]) {
    // insert code here...

    cout << "Plese enter the input file name: " << endl;
    string iname;
    cin >> iname;
    ifstream ist {iname};
    if (!ist) error("Can't open input file ",iname);

    vector<int> numbers;
    int sum;
    int n;
    while(ist>>n) {
        numbers.push_back(n);
    }

    for (int i=0; i<numbers.size(); ++i) {
        sum += numbers[i];
    }
    cout << sum << endl;

    return 0;
}

Any input I enter is getting error. I tried myin, myin.txt or any other name. The error("Can't open input file ",iname); is from the library created by the author.

I know the file does exist in the same directory with main.cpp and created with TextEdit from Mac using the format for plain text.

largest_prime_is_463035818

[...] in the same directory with main.cpp [...]

It does not really matter where you put the input file relative to the source file. The file should be in the environment's current working directory when you run the program.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why do I get a syntax error when creating a PostgreSQL function?

SQL, Why do I get this error when creating the reports table?

Why do I get a warning of ignoring return value of ‘fscanf’ when I read numbers from a file in C?

Why do I get 'undefined' error when I try to read session atrribute from Controller

Why do I get an error when trying to read a file in geopanda's included datasets?

Why do I get a "no matching function" error when I open an fstream with the file name in a std::string?

Why do I get an error when passing a string as a filename, but not a char*?

Why do I get an error when converting a list to a string?

Why do I get error when trying to add file attachments?

How do I read both from keyboard and from file?

Why do I get a permission error in the OpenUrl function on an iOS device when trying to read in the file that has been sent to my app?

Why do I get the error "Cannot store non-PrivateKeys" when creating an SSL Socket in Java?

Why do I get a 'list index out of range' error here when creating an instance of a class?

Why do I get an error when creating a new module in Android Studio?

Why do i get a laravel error while creating a new project?

Why do i get this compilation error in creating JComboBox object?

why do i get this error creating superuser django?

Why do I get a "cannot read property of undefined" error when the data is defined?

Why do I get the "can't read properties of null" error, when using "querySelectorAll.forEach"?

Why do I get a warning when creating a custom slash screen?

why do i get a key error from output when i do a merge

Why do I get an TypeError: 'in <string>' requires string as left operand, not list error when running my program?

Why do I get an error reading an RDS file when the file exists and the path is correct?

Why do I get a Bad File Descriptor error when writing to opened File?

Why do I get a "String index out of range" error when I run this program?

Why do I get an <incomplete type> message for a ifstream object in gdb?

Why do i get network error when i tried to access a database file(.mdf)?

Why do I get a Error 400 when I want to post a JSON file HTTP REQUEST

How do I read XML string from file into a String?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive