First let me thank you for taking the time to read this.

I've been working on this code for a while and I can't seem to get it to run. Basically what I have to do is take in data from a file then output the data in a report on screen. It sounds simple but unfortunately I can't seem to grasp the concept. All I am asking for is some guidance, not for someone to just fix the code as that would be cheating. I refuse to cheat and I'm just really stuck. Maybe just point me in the right direction or tell me if I'm not even close?

The .dat file gives this information:

amazon.com 820000 350 fhsaswd.gov 22040 40020 pcmagazine.com 760900 500 umbc.edu 89075 36000 mdspc.org 4500 230

And here's my buggy code:

#include <iostream>
#include <fstream>
#include <string> 

using namespace std;


struct Record
{
	string WebsiteURL;
    int Hits;
    double Revenue;
};   


void DisplayTitle();
double GetData(Record, ifstream & infile);
void DisplayTotals(Record, ifstream & infile);

int main()
{
	DisplayTitle();
	ifstream infile;
    infile.open("websiteHits.dat");
    if (infile.fail())
    {
    cout << "Input file opening failed.\n\n";
    exit(1);
    }    	

	while(!infile.eof())
	{
		GetData(Record, infile);
	
	}  

	DisplayTotals(Record, infile); 
	infile.close();

	system("pause");
	return 0;

} 

void DisplayTitle()
{
	cout << "Title"; 

}

double GetData(Record, ifstream & infile)
{
		infile.get(Record.WebsiteURL, ' ');
		infile.get(Record.Hits, ' ');
		infile.get(Record.Revenue);
		
	
}

void DisplayTotals(Record, ifstream & infile)
{
	cout << "Website URL:       Total Hits:             Revenue:  " << Record.WebsiteURL << "/n" 
		 << Record.Hits << "/n"
		 << Record.Revenue << endl;

}

Once again thank you for taking the time to read this. All help is appreciated.

Oh and if you need any additional information just ask.

Recommended Answers

All 8 Replies

Good to hear you don't want your homework done for you, it's definitely a plus on my end to see.

There are a number of things wrong with the code, but overall, not a bad start.

You may want to give us a reason why it may be buggy? Tends to boil down to syntax/logical/both. If it's syntax, just start from the top and work your way down.

I'll help you out for a minute or two.

First, your structure definition looks good. I see no problems there. What I do see wrong is the calling of a Record object that you are trying to pass through functions. Like classes, which you will likely learn next, you will have a class/structure and an object of that class that you will be manipulating.

A quick side-track moment. Think of a class/structure as the human race. Think of an object as one particular person. When you want to change someones eyes color, you need to change that particular persons eye color, not the entire human race. You do this in C++ first by creating a data type (class/structure/human race) followed by the variable(object/person); now you may change one particular persons eye color (or any other variable) through that object you just created.

ie int number; // int is a data type, number is it's variable
Record FooFighers; // Record is the data type (class), FooFighters is it's object.

then, to send FooFighters through a function call in the main program, you want to send FooFighters (the object) and not Record(the class); but you still need to properly declare your function to which you are using this class.

i.e. void function(Record& FooFighter);

This should help you get most of what is wrong with your code.

After trying to compile your code a bit, I saw a couple more problems. Your GetData function, you are trying to access your information through infile.get() which only get's one character at a time, but you need to get a string at a time. You also want to check your functions data type, why double?

Your DisplayTotals function needs to be changed so that it's not writing to the infile, but writing to an outfile.

After trying to compile your code a bit, I saw a couple more problems. Your GetData function, you are trying to access your information through infile.get() which only get's one character at a time, but you need to get a string at a time. You also want to check your functions data type, why double?

Your DisplayTotals function needs to be changed so that it's not writing to the infile, but writing to an outfile.

Wow thank's for all that! I'm reading over it all now trying to understand everything fully.

So I should switch infile.get() with getline I believe?

And to be honest, I'm not sure why I made GetData a double. I must have misread one of my directions somewhere.

And when it comes to writing to an outfile, I didn't think I had to as my instructor only wanted the data to be displayed on screen.

As you can see I'm terribly new to this :(

No, it's all good. Keep it up, though. The more time that passes without keeping up with your studies, the more behind you will get.

If for your output function you do -not- need to write to an outfile but into the DOS that you normally see after compiling/running, you do not need to pass an ifstream to the output function, just pass the class and it's object.

With the input, remember how you can input a number from the program normally such as

cin >> number;


You can do something very similar to that with infile and string/int/double variables :D If you use getline(), you will need to then parse the entire line into what you are looking for.

No, it's all good. Keep it up, though. The more time that passes without keeping up with your studies, the more behind you will get.

If for your output function you do -not- need to write to an outfile but into the DOS that you normally see after compiling/running, you do not need to pass an ifstream to the output function, just pass the class and it's object.

With the input, remember how you can input a number from the program normally such as

cin >> number;


You can do something very similar to that with infile and string/int/double variables :D If you use getline(), you will need to then parse the entire line into what you are looking for.

Ahh I see. Thank you soooo much ^.^ I'm going to work on it now :)

Okay so here's my fix so far. I think I've gotten most of it, now all I really need to work on is the infile.get thing, I think lol.

<code removed>

Yes, after compiling your new code, the only compile errors I got were from your Get function. Why int? Why do you need to return any value? All you are doing is grabbing data from your file and it's already being saved to your object, your pass by reference is making sure of that through the function call.


Also, with your input. Do you

cin >> (number, ' '); // ? No, this doesnt look right.
cin >> number; // does look right.

Yay I got it! Thank you so much Saith. You're the best.

I finally understand the concept.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.