For some reason my program is not working and dont know how to solve this problem. In line 15 the "users" is underline in red and will not run. Please help.

    #include <iostream>
    #include <cmath>
    #include <iomanip>
    #include <string>
    #include <fstream>
    #include <C:\Users\N.\Desktop\Popcorn\Popcorn\JJ.cpp>
    using namespace std;
    int main()
    {
    fstream InFile;
    string Name;
    int Jars;
    double Acres;

    PrintHeader();

    InFile.open("C:\Users\N.\Desktop\Popcorn\Popcorn\JJ.cpp",ios::in);
    if (!InFile)
    {
    cout<<"Can't Open the input file.";
    return 1;}

    while (!InFile.eof())
    {

    GetData(InFile, Name, Acres, Jars);}
    InFile.close();
    cin.get();
    return 0;}
    void PrintHeader(void)
    {
    cout<<setw(23)<<"Pop CoOp"<<endl;
    cout<<"Farm Name"<<setw(30)<<"Production"<<endl;
    cout<<setw(41)<<"Thousands of"<<endl;
    cout<<setw(47)<<"Pint Jars per Acre"<<endl;
    cout<<setw(53)<<" 1 2 3 4 5 6"<<endl;
    cout<<setw(52)<<"---|---|---|---|---|---"<<endl;}

    void GetData(fstream& In, string& Name, double& Acres, int& Jars)
    {
    char x=' ';
    int count=0;
    In.get(x);
    while (x < 29 || x != ',' )
    {
    Name = Name + x;    
    In.get(x);}
    if (x == ',' || x >= 29)    
    {   
    In>>Acres>>Jars;    
    cout<<Name<<endl;   
    Name=" ";   
    In.get(x);  
    count=0;    
    Jars=0; 
    Acres=0;}}

Perhaps you have to escape the escape character

"C:\\Users\\N.\\Desktop\\Popcorn\\Popcorn\\JJ.cpp"

Edited 3 Years Ago by Suzie999

As an aside, I have two comments to make. First off, while indent style is largely a matter of personal preference, it is a good idea to adopt some form of indentation and use it consistently. It is important for readability, especially as programs grow longer; and while it may not seem like it now, it is crucial that you get into the habit of seeing programs as something other coders will have to be able to read at some point in the future. To this end, here is your program re-indented (using AStyle and EMACS) in Allman style:

#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <fstream>
#include <C:\\Users\\N.\\Desktop\\Popcorn\\Popcorn\\JJ.cpp>

using namespace std;

int main()
{
    fstream InFile;
    string Name;
    int Jars;
    double Acres;

    PrintHeader();

    InFile.open("C:\\Users\\N.\\Desktop\\Popcorn\\Popcorn\\JJ.cpp", ios::in);

    if (!InFile)
    {
    cout << "Can't Open the input file.";
    return 1;
    }

    while (!InFile.eof())
    {

    GetData(InFile, Name, Acres, Jars);
    }

    InFile.close();
    cin.get();
    return 0;
}


void PrintHeader(void)
{
    cout << setw(23) << "Pop CoOp" << endl;
    cout << "Farm Name" << setw(30) << "Production" << endl;
    cout << setw(41) << "Thousands of" << endl;
    cout << setw(47) << "Pint Jars per Acre" << endl;
    cout << setw(53) << " 1 2 3 4 5 6" << endl;
    cout << setw(52) << "---|---|---|---|---|---" << endl;
}


void GetData(fstream& In, string& Name, double& Acres, int& Jars)
{
    char x=' ';
    int count = 0;
    In.get(x);
    while (x < 29 || x != ',' )
    {
    Name = Name + x;    
    In.get(x);}
    if (x == ',' || x >= 29)    
    {   
    In >> Acres >> Jars;    
    cout << Name << endl;   
    Name = " ";   
    In.get(x);  
    count = 0;    
    Jars = 0; 
    Acres = 0;
    }
}

This sort of 'modernist poetry' layout may take some getting used to, but it is well worth the effort.

The second comment has to do with the include file. You have JJ.cpp as both a header file (which should contain source code for declarations and function prototypes, and nothing else), and then open it as a data file. Which is it? Given the .cpp extension, I'm guessing that it is also the source code file you are compiling now, in which case you'll get an infinite loop as it tries to include itself over and over again. User header files (as opposed to the standard C++ headers) usually have the extension .h, and as I said should only contain declarations, function prototypes, class and struct definitions, and preprocessor directives. Data files can have any extension you want, because, after all, they are just repositories if data in some format; there's no real standard for them, though using .cpp for one is likely to be confusing.

This article has been dead for over six months. Start a new discussion instead.