Thread: cant run this program

  1. #1
    Unregistered
    Guest

    Unhappy cant run this program

    i am using microsoft visual c++ and cant get this to run. i have the file in the right spot.....any suggestions, or do you need any more info from me.....it always tells me that there is an error opening the file....



    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>

    using namespace std;


    int main()
    {
    string filename;
    cout<< "Enter file name to open:";
    cin >> filename;

    ifstream fin(filename.c_str());
    if (!fin)
    {
    cout << "Error opening" + filename + "\n";

    //We're outa here.
    return 0;
    }

    string firstname,lastname,ssn; //ssn has to be a string because the input
    int age; // text has minues (-) in it. So you can't use an int.
    int total=0; //We'll use this to add up the ages as we get them.

    //Loop until end of file
    while (!fin.eof())
    {
    fin >> firstname;

    //If the firstname is blank, we don't have a line. It's probably just
    // the carriage return at the very end of the file.
    if (firstname != "")
    {
    fin >> lastname;
    fin >> ssn;
    fin >> age;
    cout << firstname << " " << lastname << " " << ssn << " " << age << "\n";

    //Add the age to the total. total = total + age is another way to write it.
    total += age;
    }
    }


    fin.close();

    cout << "Total= " << total;

    return 0;
    }

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >it always tells me that there is an error opening the file

    What are you entering as the filename? Where is the file located on your drive?

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    cout << "Error opening" + filename + "\n";
    shouldn't that be:
    cout << "Error opening " << filename << '\n';

    [Next part added instead of new reply]

    I'm not too familiar with the C++ way of opening files (I like fopen() and such), but check to see that you open the file in write mode. If you try to open a file in read mode and it doesn't exist, you should be getting the error.
    Last edited by neandrake; 03-10-2002 at 12:19 PM.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    shouldn't that be:
    cout << "Error opening " << filename << '\n';
    No, a std::string will be created from "Error opening" + filename + "\n".


    I'm not too familiar with the C++ way of opening files
    Evidently. The fact that it's an istream opens the file in write mode.

  5. #5
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    instead of putting me down, shouldn't you be trying to help the guy's problem?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >instead of putting me down
    No harm is meant, here's an excerpt from the smart questions link in my signature which should make you feel a bit better...but only a bit

    Dealing with rudeness
    Much of what looks like rudeness in hacker circles is not intended to give offence. Rather, it's the product of the direct, cut-through-the-bull**** communications style that is natural to people who are more concerned about solving problems than making others feel warm and fuzzy.

    When you perceive rudeness, try to react calmly. If someone is really acting out, it is very likely that a senior person on the list or newsgroup or forum will call him or her on it. If that doesn't happen and you lose your temper, it is likely that the person you lose it at was behaving within the hacker community's norms and you will be considered at fault. This will hurt your chances of getting the information or help you want.

    On the other hand, you will occasionally run across rudeness and posturing that is quite gratuitous. The flip-side of the above is that it is acceptable form to slam real offenders quite hard, dissecting their misbehavior with a sharp verbal scalpel. Be very, very sure of your ground before you try this, however. The line between correcting an incivility and starting a pointless flamewar is thin enough that hackers themselves not infrequently blunder across it; if you are a newbie or an outsider, your chances of avoiding such a blunder are low. If you're after information rather than entertainment, it's better to keep your fingers off the keyboard than to risk this.

    (Some people assert that many hackers have a mild form of autism or Asperger's Syndrome, and are actually missing some of the brain circuitry that lubricates `normal' human social interaction. This may or may not be true. If you are not a hacker yourself, it may help you cope with our eccentricities if you think of us as being brain-damaged. Go right ahead. We won't care; we like being whatever it is we are, and generally have a healthy skepticism about clinical labels.)
    Please note that 'hacker' in this sense means gifted programmer or computer person and not a malicious kid who simply runs programs that he/she download off of the internet.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Unregistered
    Guest
    i have the file on my c drive and it looks like this....

    John Smith 123-45-6789 57
    Judy Jones 987-65-4321 22
    Jeff Jingles 111-11-1111 32
    Jennifer Gennifer 000-00-0000 18
    Angie Pangie 111-22-3333 20
    Brad Pitt 333-44-5555 34

  8. #8
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >instead of putting me down, shouldn't you be trying to help the guy's problem?

    Sorry no offense meant, and I was trying to help the guy instead of asking him to go down blind alleys. Besides evidently I don't know what I'm talking about because istreams open a file for reading not writing (unregistered wants to read from a file not write to it) .

    >i have the file on my c drive and it looks like this....

    Ensure that you're typing the full path of the file name. You could try moving it to your project directory and just type the filename. If the filename contains spaces you'll have to use fin.get() or fin.getline() rather than operator >>.

  9. #9
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Doesn't ifstream read from the file Sorensen?

  10. #10
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Ahh, I hate when that happens.

  11. #11
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Sorry no offense meant
    none taken

    No harm is meant, here's an excerpt from the smart questions link in my signature which should make you feel a bit better...but only a bit
    hehe, cool
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Re-doing a C program to run in Win2000 or XP
    By fifi in forum C Programming
    Replies: 5
    Last Post: 08-17-2007, 05:32 PM
  3. Replies: 3
    Last Post: 07-11-2005, 03:07 AM
  4. plz help me run this program!!!
    By galmca in forum C Programming
    Replies: 8
    Last Post: 02-01-2005, 01:00 PM
  5. Cannot run program in background?
    By registering in forum Linux Programming
    Replies: 3
    Last Post: 06-16-2003, 05:47 AM