6 Replies - 899 Views - Last Post: 18 September 2014 - 02:51 AM Rate Topic: -----

#1 doukknowwhattunais  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-September 14

How to get my program to read values from a file

Posted 18 September 2014 - 01:59 AM

I am trying to get my program to calculate what point goes in what quadrant using a file. But msy program is not reading the values from the file and also it is not running so im not sure what's wrong with it.

//This program uses a fstream object to write data that will read the values of a (x,y) point from a file.

//add the appropriate preprocessor
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
    //declaring X and Y
    double X, Y;

    //declaring quadrant
    string quadrant;

    //declare input file stream and open it
    ifstream fin;
    fin.open("points.txt" , ios::app);

    //File open error handling
    if (!fin)
    {
        cout << " ERROR: Cannot open the file. \n ";
        return 0;
    }

    //If it's Quadrant 1
    if ( X >= 1 && Y >= 1 )
    {
        quadrant = " Quadrant I. ";
    }

    //If it's Quadrant 2
    if ( X <= -1 && Y >= 1)
    {
        quadrant = " Quadrant II. ";
    }



    //If it's Quadrant 3
    if ( X <= -1 && Y <= -1)
      {
        qaudrant = " Quadrant III. ";
      }

    //If it's Quadrant 4
    if ( X >= 1 && Y <= -1)
    {
        qaudrant = "Quadrant IV. ";
    }

   //declaring fout
    ofstream fout;

    fout.open ("points.txt", ios :: app);

    //send the results to the points.txt file
    fout << endl << " The point ( "<< X <<" , " << Y << ") lies " << quadrant;

  //closing out of files
    fin.close();
    fout.close();

    return 0;
}
[member='doukknowwhattunais']


Is This A Good Question/Topic? 0
  • +

Replies To: How to get my program to read values from a file

#2 jimblumberg  Icon User is online

  • member icon

Reputation: 5332
  • View blog
  • Posts: 16,601
  • Joined: 25-December 09

Re: How to get my program to read values from a file

Posted 18 September 2014 - 02:21 AM

Quote

But msy program is not reading the values from the file and also it is not running so im not sure what's wrong with it.

First you don't need the ios::app open mode when dealing with a input stream.

    //declare input file stream and open it
    ifstream fin("points.txt"); // You don't need the open call, just use the constructor to open the file.


Second are you getting the error message about the input file not opening?

Third I don't see that you're reading anything from any file.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 doukknowwhattunais  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-September 14

Re: How to get my program to read values from a file

Posted 18 September 2014 - 02:26 AM

Hi Jim thank you for replying and I'm not getting an error message about the file I'm getting an error message that my quadrant for lines 45 and 51 not being declared. And how do i get it to read from a file?
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2130
  • View blog
  • Posts: 4,196
  • Joined: 30-May 10

Re: How to get my program to read values from a file

Posted 18 September 2014 - 02:35 AM

The trick is learning how to spell.
37 quadrant = " Quadrant II. ";
45 qaudrant = " Quadrant III. ";

> And how do i get it to read from a file?
Well you already managed fout << something; so how come you can't at least try fin >> something;

If you know how to use cin >> something;, you're already there in terms of knowledge. All you need to do is actually try it.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon

Reputation: 5332
  • View blog
  • Posts: 16,601
  • Joined: 25-December 09

Re: How to get my program to read values from a file

Posted 18 September 2014 - 02:39 AM

Perhaps you need to review basic file input and output?

And without knowing exactly what the input file contains it is hard to say how to read the file. And why are you opening the same file for output? I recommend using a different file name for your output.

Quote

I'm getting an error message that my quadrant for lines 45 and 51 not being declared

It appears that you may want to check the spelling of your variable.


Jim
Was This Post Helpful? 0
  • +
  • -

#6 doukknowwhattunais  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-September 14

Re: How to get my program to read values from a file

Posted 18 September 2014 - 02:44 AM

okay so i fixed the spelling problem and now it runs but it still wont read the file and i don't really know how to use the cin >> something; I'm trying to follow my notes and me textbook but it's not helping

Hi jim my teacher wants us to use the same file for output becuase its suppose to calculate what quadrant the points lie on[attachment=36804:points .txt]
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon

Reputation: 5332
  • View blog
  • Posts: 16,601
  • Joined: 25-December 09

Re: How to get my program to read values from a file

Posted 18 September 2014 - 02:51 AM

Quote

Hi jim my teacher wants us to use the same file for output becuase its suppose to calculate what quadrant the points lie on

That doesn't make any sense. What does outputting the results have to do with reading your file. When you add information to that file you will probably have problems reading the file in the future. You are better off writing either to the console or to a totally different file.

Next looking at your input file:
1 3
-4 10
9 7
8 -3

It looks like you have multiple "points" that you need to read, so you'll probably want to use a loop of some sort, and definitely not use the same file for output. And your "points" should probably be integers not doubles.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1