Help on C++ homework

I need to write a program to use getline in order to read from a text file and output the first and last name, whether their a republican or democrat, and what their building name is. All of which are in the selected file. I cant seem to get it to work. I was provided with a sample program that does the same thing with integers but I don't know how or where to save the text file so that the program will go out and read it.

The sample program looks like this:

// This program inputs integers
// Determines the average, largest, smallest
//input file has integers one per line
//User inputs the name of the data file

#include<iostream>
#include <string>
#include<fstream>
#include <iomanip>
using namespace std ;

int main()
{
int average, num, sum, count;
int hi, lo;

ifstream inData; //input file stream variable
string filename; // name of the input file


cout << "Enter the name of the input file: " ;
cin >> filename;

inData.open(filename.c_str () );

//inData.open("numbers1.txt");
if ( !inData)
{
cout << " input file not found \n";
return 1;
}


sum = 0;
count = 0;
hi = -1;
lo = 1000;

cout << "Input values:\n";

inData >> num ; //input 1
while ( inData)
{
cout << num << " " ;
sum = sum + num;
count++;
// To print 5 per line
if (count % 5 ==0)
cout << endl;

if ( num > hi)
hi = num;
if ( num<lo)
lo = num;

inData >> num ; //input 2
}

average = sum / count;
cout << "\n\nThere were "<< count << " values input.\n";
cout << "\n\nsum = " << sum << endl;
cout << "\n\naverage = " << average << endl;
cout << "hi = " << hi << endl;
cout << "lo = "<< lo << endl;
inData.close () ;


return 0;
}
You can place the file anywhere as long as you enter the right file path. Try left clicking on the file, and then clicking on properties there should be a file path in the general tab. You may have to add the file name and extension.
Last edited on
Even if I save it as num.txt which is what I am supposed to do it heeps returning with "input file not found" and I cant figure out where I made my error.
I think you just need to do

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;

int main(int nNumberofArgs,char* pszArgs[])
{
    char filename[256];
    cout << "Enter the name of file you want to open:";
    cin.getline(filename,256);

    for(int s = 0;s != '\n';s++)
    {
        if(filename[s] == NULL)
        {
            filename[s] = '.';
            filename[s + 1] = 't';
            filename[s + 2] = 'x';
            filename[s + 3] = 't';
            filename[s + 4] = NULL;
            break;
        }
    }

    cout << "Opening and reading contents of '" << filename << "'" << endl;

    ifstream File(filename);

    for(int n = 1;;n++)
    {
        char object[256];
        File.getline(object,256);

        if(File.fail())
        {
            break;
        }

        cout << n << " - " << object << endl;
    }

    system("PAUSE");
    return 0;
}
Well this is probably differant depending on what compiler and os you are using.
But on my computer (windows 7 and visual studio) this is how/where you save the file. Assuming visual studio is saved in it's default location.

click start
user name
My Documents
visual studio
projects
Folder name (this is the name you gave your project)
Folder name again
save file
in this case the file path would be num.txt
If you save the file anywhere else you would need the full file path
Last edited on
I would do it like this:
1
2
3
4
5
6
7
8
9
10
11
string FileLocation="";

cin.getline(FileLocation,256);

ifstream infile(FileLocation);
while(infile)
{
  infile.get(k);

  //Code goes here
}


It works for me!
The code isn't the problem I ran the code it works fine. The problem is that the op can't get the file path right
Last edited on
Oh, sorry! I should have read a little more...

Numeri
Open a file with a unique name for output, then close it. This will create a file. Look for where that file is in your project directory. The file you want to open should end up in the same directory.

Alternately, if you're on windows, just do a system("dir"); and make a note of the directory. I'm sure there's an equivalent on other operating systems.
Topic archived. No new replies allowed.