Numbered output <fstream>

I'd like to create a grocery list with numbers next to each food I

Page 1 of 1

12 Replies - 989 Views - Last Post: 23 April 2010 - 10:24 PM Rate Topic: -----

#1 Guest_Andrew*


Reputation:

Numbered output <fstream>

Posted 22 April 2010 - 08:20 PM

So this is my first program that I am creating that will actually do something useful! woo!
I got into programming a while back and was using Bloodshed Dev-C++. I recently came back and it's gone, I've been messing around with wxDev-C++ but am currently using Visual Studio 2010.

Okay here we go.
#include <iostream>
#include <fstream>
#include <string>
#define editfile "grocery_list.txt"

using namespace std;
string foodItem;



int main()
{
	fstream fileString;
	fileString.open (editfile, fstream::in | fstream::out | fstream::app);
	cout << "Grocery List Creator\n\n" 
		<< "Enter your items one at a time then press Enter.\n"
		<< "When finished type 'quit'.\n\n";

	while(foodItem != "quit")
	{
		if(foodItem != "quit")
		{
			getline ( cin, foodItem);
			fileString << foodItem << endl;
		}
	}

	fileString.close();
	return 0;
}



Right now I'm trying to figure out how I can automatically number the grocery list in the output so it will look like.

1. Bacon
2. Eggs
3. Milk

So on and so forth. I've been trying to think how to do it with a for loop but can't seem to figure out how.

I'd like it eventually make it so the program will organize the list with meat products, dairy, fruit etc. via a big word list.
say you enter milk and it's in the dairy word list then it will put that under dairy items in "Grocery list.txt".
That's just a goal for now, but I hope to eventually get there.

Is This A Good Question/Topic? 0

Replies To: Numbered output <fstream>

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Numbered output <fstream>

Posted 22 April 2010 - 08:28 PM

So...what's your question?
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Andrew*


Reputation:

Re: Numbered output <fstream>

Posted 22 April 2010 - 08:39 PM

Quote

Right now I'm trying to figure out how I can automatically number the grocery list in the output so it will look like.

1. Bacon
2. Eggs
3. Milk

Was This Post Helpful? 0

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Numbered output <fstream>

Posted 22 April 2010 - 08:43 PM

That's still not a question. Programming is all about figuring out things. I can, however, wish you luck :)

( I know you're struggling. But if you just dump a problem on a forum, you won't learn. Someone will solve it, post step by step instructions, you'll follow them, and learn nothing. )
Was This Post Helpful? 0
  • +
  • -

#5 Dorito_125  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 88
  • Joined: 13-December 08

Re: Numbered output <fstream>

Posted 22 April 2010 - 08:48 PM

I didn't sign in before, this is guest_Andrew*.

I hear what you're saying, I've just hit a bit of a road block. I've been experimenting with for loops. It just makes the code messy and seems unorganized.
My question to you: Should I keep trying to attempt this with a for loop or should I abandon it and try to find some other means.

I've also been thinking about just dropping int x in the while loop and have x++ every time it comes around.
Does that sound plausible?
I'm not looking for an answer as much as a nudge in the correct direction.
Was This Post Helpful? 0
  • +
  • -

#6 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Numbered output <fstream>

Posted 22 April 2010 - 09:00 PM

Quote

Does that sound plausible?
I don't want to short circuit the learning process, so I'm going to propose this:

1. Ask yourself, don't tell me the answer: Do your proposals make sense logically?
2. If they do, go ahead and try them.
3a. If it works, great.
3b. If it doesn't, post your modified code here. Then tell me why you think logically, your code should have worked.

I'll comment on this, but I want you to try your idea first, without me telling you it's good or bad.

This post has been edited by Oler1s: 22 April 2010 - 09:00 PM

Was This Post Helpful? 0
  • +
  • -

#7 Dorito_125  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 88
  • Joined: 13-December 08

Re: Numbered output <fstream>

Posted 22 April 2010 - 09:18 PM

Solved:

#include <iostream>
#include <fstream>
#include <string>
#define editfile "grocery_list.txt"

using namespace std;
string foodItem;
int x = 1;



int main()
{
	fstream fileString;
	fileString.open (editfile, fstream::in | fstream::out | fstream::app);
	cout << "Grocery List Creator\n\n" 
		<< "Enter your items one at a time then press Enter.\n"
		<< "When finished type 'quit'.\n\n";

	while(foodItem != "quit")
	{
		if(foodItem != "quit")
		{
			getline ( cin, foodItem);
			fileString << x << ".\t" << foodItem << endl;
			x++;
		}
	}

	fileString.close();
	return 0;
}



So now that I have it working how I'd like.
What would you do different? (anybody)
Was This Post Helpful? 0
  • +
  • -

#8 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Numbered output <fstream>

Posted 22 April 2010 - 09:27 PM

That's pretty much how I would do it. I actually have one important criticism, although it's not related to this discussion. You are relying on globals.

using namespace std;
string foodItem;
int x = 1;



foodItem and x are defined globally. This is a really bad habit. You want to drop this habit now. When you're starting out, you don't face any penalties for globals. However, at a certain point, you suffer. You do not want to be a programmer that relies on globals.

When I program, I have a clear idea of what I want to logically happen, and then I look at what code it takes to make it happen. There's actually a surprising number of decisions I make on something this small.
Was This Post Helpful? 0
  • +
  • -

#9 Dorito_125  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 88
  • Joined: 13-December 08

Re: Numbered output <fstream>

Posted 22 April 2010 - 10:58 PM

So you would recommend this:

#include <iostream>
#include <fstream>
#include <string>
#define editfile "grocery_list.txt"
#define dairyList "dairy.txt"



int main()
{
	using namespace std;

	fstream listFile;
	fstream dairyWordBank;
	dairyWordBank.open (dairyList, fstream::in);
	listFile.open (editfile, fstream::in | fstream::out | fstream::app);
	cout << "Grocery List Creator\n\n" 
		<< "Enter your items one at a time then press Enter.\n"
		<< "When finished type 'quit'.\n\n";

	while(foodItem != "quit")														// Keep program open until user types 'quit'
	{
		if(foodItem != "quit")														// If user decides to quit program don't add quit to list
		{
			string foodItem;
			int numberOfItem = 1;
			getline ( cin, foodItem);
			listFile << numberOfItem << ".\t" << foodItem << endl;
			numberOfItem++;															// Increment number of food items
		}
	}

	listFile.close();
	return 0;
}




Ignore the word list stuff.
Was This Post Helpful? 0
  • +
  • -

#10 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Numbered output <fstream>

Posted 23 April 2010 - 08:27 AM

Well, your new code now is actually broken. I also didn't notice another bad habit. So here's roughly what my code would look like.

#include <iostream>
#include <fstream>
#include <string>
const std::string editfile = "grocery_list.txt";
const std::string dairyList = "dairy.txt";

int main()
{
        using namespace std;

        fstream listFile;
        fstream dairyWordBank;
        dairyWordBank.open (dairyList, fstream::in);
        listFile.open (editfile, fstream::in | fstream::out | fstream::app);
        cout << "Grocery List Creator\n\n" 
                << "Enter your items one at a time then press Enter.\n"
                << "When finished type 'quit'.\n\n";

        string foodItem;
        int numberOfItem = 0;
        while(getline(cin, foodItem) && foodItem != "quit")
        {
                        numberOfItem++;
                        if(!(listFile << numberOfItem << ".\t" << foodItem << endl))
                        {
                            // deal with write failure somehow
                        }
        }

        listFile.close();
        return 0;
}


Was This Post Helpful? 0
  • +
  • -

#11 Dorito_125  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 88
  • Joined: 13-December 08

Re: Numbered output <fstream>

Posted 23 April 2010 - 11:20 AM

Okay couple questions.

1a. why are the following strings outside of int main()?
const std::string editfile = "grocery_list.txt";
const std::string dairyList = "dairy.txt";



1b. When is it acceptable to use global variables?

2. The following if statement confused me.

while(getline(cin, foodItem) && foodItem != QUIT)														// Keep program open until user types 'quit'
	{
		numberOfItem++;
		*******if(!(listFile << numberOfItem << ".\t" << foodItem <<endl)) ***********											// If user decides to quit program don't add quit to list
		{
			system("pause");																				// Increment number of food items
		}
	}



If I understand it correctly it attempts to write the 'numberOfItem' and 'foodItem' to 'listFile'.
If it succeeds it never enters the if statement.
The part that confused me though is, I didn't think it would write to file if the if statement did not return true.

That was even confusing to write! :sweatdrop:
Was This Post Helpful? 0
  • +
  • -

#12 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Numbered output <fstream>

Posted 23 April 2010 - 11:57 AM

Quote

1a. why are the following strings outside of int main()?
Yeah, I was wondering when you would ask that. Those strings are constants, and these constants are best noted globally because they are basically stating something about the program. It's not a syntactical rule or anything, it's just a placement that I make based on my experience as a programmer. I can't really condense that entire philosophy I've built up into a rule.

Quote

1b. When is it acceptable to use global variables?
Practically, never. Take note, constants are not variables. That const is there for a reason.

Well, I hate absolutes. Too easy to find exceptions. The notion behind a global variable is this: you have a variable that you can have one and only one instance of. Any every function in the program can read and write this variable. I.e. global and singular.

Here's the problem, global is bad engineering, and singular is bad pigeonholing. These arguments, as I noted, aren't exactly understandable in small code. But imagine if you have 1000 lines of code to maintain. You have a global variable, and there's a problem relating to that variable. So you say to yourself, OK, i'll just look at what lines of code are modifying this variable. Problem is, that's your entire program.

You don't want spaghetti. When any function in your program can modify a single variable, you create spaghetti code. They all tie to this one location.

It's also bad pigeonholing. Let's say you are creating a program that draws pretty boxes on screen. The rendering system: well you can only have one, as it's one computer right? Until you try to integrate multi monitor support, or something interesting like that.

Now, you might try and update your code, but because every bit of your program depends on there being only one instance of this variable, how much fun will making changes to your code be?

It's also deadly in modern parallel code. If you've bought a computer in the past few years, you have a dual core, quad core, i5, i7, etc. computer. These computers are great for parallel programming. You can have concurrent threads of actions at once.

When you get to such a point, you will discover that doing things with shared state (like globals) is deadly. It destroys opportunities to do things in parallel. For example, consider reading from hard disk. Only one hard disk. Ten programs want something from disk. Get in line, wait for hard disk to give you what you want. Sloooow. Hard disk is like global object. Only one. Everyone can read and write to it.

Quote

The part that confused me though is, I didn't think it would write to file if the if statement did not return true.
You're reading the code incorrectly. << is an operator. To understand, does this make sense to you:

if (5+6)
{
    // always happens, right?
}



+ is an operator. Now consider fileStrm << a; << is an operator. The result of 5+6 is 11. Addition is performed. What is result of <<, for streams? If you check documentation, it is the stream. So it's basically like writing:

listFile << numberOfItem << ".\t" << foodItem <<endl;
if (!listFile)
{
    // .....
}


This post has been edited by Oler1s: 23 April 2010 - 11:58 AM

Was This Post Helpful? 0
  • +
  • -

#13 Dorito_125  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 88
  • Joined: 13-December 08

Re: Numbered output <fstream>

Posted 23 April 2010 - 10:24 PM

What kind of impact do constants have on multi-core processors?

I've made quite a few changes, and made a rudimentary system of opening and checking the word list to determine if the item is a dairy item.
I'm having trouble passing the values through the functions.
So my 'writeDairyList' function will only write if the bool value = true.
It's not being passed that value though.

#include <iostream>
#include <fstream>
#include <string>
const std::string outputFile = "Grocery_List.txt";
const std::string dairyWordList = "dairy.txt";
const std::string QUIT = "quit";	

int searchDairyWordList();
void writeDairyList(std::string, bool value);

int main()
{
	using namespace std;
	bool value = false;
	string content;				

	cout << "Grocery List Creator\n\n" 
		<< "Enter your items one at a time then press Enter.\n"
		<< "When finished type '"<< QUIT << "'.\n\n";


	searchDairyWordList();
	writeDairyList(content, value);
	system("pause");
	return 0;
}


int searchDairyWordList()
{
	using namespace std;
	int searchList = 0;
	string content;
	char compare[100];
	bool value;
	fstream live_dairyWordList;
	live_dairyWordList.open (dairyWordList, fstream::in);

	if(live_dairyWordList.is_open())
	{
		cout << "Dairy Word list initialized\n\n";
	}
	
	else if(live_dairyWordList.bad())
	{
		cerr << "ERROR: Dairy Word list not initialized.\n\n\a";
	}

	getline(cin, content);
	while(content != QUIT && searchList < 100 && compare != content)
	{
		searchList++;
		live_dairyWordList.getline(compare, 100);
			if(compare == content)
			{
				cout << "Dairy Item.\n";
				value = true;
			}
	}

	if ( compare != content ) 
	{
		cout << "Non dairy item.\n";
		value = false;
	}
	live_dairyWordList.close();
	return value;
}



void writeDairyList(std::string content, bool value)
{
	if(value == true)
	{
		using namespace std;
		fstream live_outputFile;
		live_outputFile.open (outputFile, fstream::in | fstream::out | fstream::app);
		live_outputFile << content << "test";

		live_outputFile.close();
	}
}



I already sense this way of searching word lists is ineffective. I also would have to write multiple spellings:
whip cream, whip-cream whipcream milk Milk MILK and so on and so forth. I'm still trying to figure that one out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1