Trying EVERYTHING but I don't know what the *heck* I'm even supposed to try because there shouldn' tbe a *bleeping* PROBLEM. The file is being opened fine, I guess, because I tried changing the file path and then got an error. It's just not reading the DATA. THERE'S NO REASON FOR THIS. ALL THE VALUES ARE COMING TO ZERO. HERE IS THE DATA:

2 3 50
2 1 75
2 0 40
1 3 25
1 5 10
2 6 50
2 8 300
6 3 100
2 0 30
2 2 10
1 6 100
2 4 30
2 6 200
1 5 10
1 6 100

HERE IS THE CODE:

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

void printStock (int v[],int n);
double Sales (int v[], int n, double cost);
int main () 
{
	int transtype, autonumb, units, i;
	int inv[10]={0,0,0,0,0,0,0,0,0,0};
	
	for (i=0; i<10; i++)
		printStock(inv,i);
		
	ifstream transInfo;
	transInfo.open("transinfo.txt");
	
	if(!transInfo.is_open()) {
		cout << "File was not opened correctly" << endl;
		cout << "program halted";
		return 0;
	}
	
	transInfo >> transtype >> autonumb >> units;
	cout << transtype << autonumb << units;
	
	while (transInfo){
		
	//	(i=0; i<10; i++)
	//	inv[i]=i;
		
		cout << autonumb;
		
		if (transtype==1){
			if (inv[autonumb]>=units){
				inv[autonumb]-=units;
				cout << "Sell " << units << " units of car " << autonumb << endl;
			}
			else cout << "REJECTED: Insufficient Stock" << endl;
		}
		
		else if (transtype==2){
			inv[autonumb]+=units;
			cout << "Receive " << units << " units of car " << autonumb << endl;
		}
		
		else cout << "INVALID TRANSACTION CODE" << transtype << endl;
		
		transInfo >> transtype >> autonumb >> units;
	}
	
	cout << endl;
	for (i=0; i<10; i++)
		printStock(inv,i);
	cout << "\n\nTotal potential income: " << Sales(inv,10,10000) << endl;
	
	transInfo.close();
    return 0;
}

void printStock (int v[], int n) {
	cout << n << " " << v[n] << endl;
	return;
}

double Sales (int v[], int n, double cost) {
	int autos=0;
	for (int i=0; i<n; i++)
		autos+=i;
	double val=autos*cost;
	return val;
}


WHAT THE ??

Ancient Dragon commented: bad language -7

Recommended Answers

All 30 Replies

Everyone will just ignore you when you use such cursing.

This is what I got when I ran your program. What's wrong with it?

0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
23503Receive 50 units of car 3
1Receive 75 units of car 1
0Receive 40 units of car 0
3Sell 25 units of car 3
5REJECTED: Insufficient Stock
6Receive 50 units of car 6
8Receive 300 units of car 8
3INVALID TRANSACTION CODE6
0Receive 30 units of car 0
2Receive 10 units of car 2
6REJECTED: Insufficient Stock
4Receive 30 units of car 4
6Receive 200 units of car 6
5REJECTED: Insufficient Stock
6Sell 100 units of car 6

0 70
1 75
2 10
3 25
4 30
5 0
6 150
7 0
8 300
9 0


Total potential income: 450000
Press any key to continue . . .

Works fine here, data goes in, data comes out, looks ok.

It is advisable for you to get a grip. This type of post is extremely unprofessional and immature.

Your values aren't all coming to zero (0). They are initialized to 0 when the program starts, which they should be, but they get modified as it executes:

0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
23503Receive 50 units of car 3
1Receive 75 units of car 1
0Receive 40 units of car 0
3Sell 25 units of car 3
5REJECTED: Insufficient Stock
6Receive 50 units of car 6
8Receive 300 units of car 8
3INVALID TRANSACTION CODE6
0Receive 30 units of car 0
2Receive 10 units of car 2
6REJECTED: Insufficient Stock
4Receive 30 units of car 4
6Receive 200 units of car 6
5REJECTED: Insufficient Stock
6Sell 100 units of car 6

0 70
1 75
2 10
3 25
4 30
5 0
6 150
7 0
8 300
9 0


Total potential income: 450000

Really, all that I see are some output formatting issues and an issue with the accumulator in your Sales() function. What I don't see is the issue you're describing...

What, seriously? Oh boy. Then what's wrong with my setup? I'm using XCode 3.2... running it in the "Debugger Console", excuse my lack of knowledge, I only know what we're taught in my introductory C++ course so far... But this is the output I get:

0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
000
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0


Total potential income: 450000

Any idea as to why this would be?

:confused: :confused: I really have no good idea. Are you sure that your file has anything in it? It's acting like there is no data in the file...

EDIT:
Do you have multiple copies of the file stored in multiple locations? I suspect you may be editing/viewing one version of the file that is correctly populated, but your program is opening an empty version.

If I run the program with an empty input file, I get similar results:

0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
-858993460-858993460-858993460
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0


Total potential income: 450000

(note that the 3 negative values instead of 3 zeros is a compiler-dependent behavior, yours automatically initializes to 0, mine doesn't)

Are you using MAC? I'm using Windows 7 and VC++ 2010 Express.

(just an FYI, for comparison reasons)
I've got Win7 and VS 2008 Pro...

Professor said the same thing as you guys, when he ran it it ran as it was supposed to... He thinks it's my filepath? But it's opening the file. Could it be my compiler? What could I do about the filepath? Unless I reference it from the root /User all the way down, it doesn't open from its local location (in the same folder as the program). Could it be the filepath? Compiler? (I guess that's GCC.)

Suggestions? Thank you...

Ancient Dragon: Yes, I am on a Mac;

Fbody: I do right now as I'm messing around with the filepaths, but I didn't originally and it's the same result... I never had an empty file with that name either? ... But to answer your question, no.

Professor said the same thing as you guys, when he ran it it ran as it was supposed to... He thinks it's my filepath? But it's opening the file. Could it be my compiler? What could I do about the filepath? Unless I reference it from the root /User all the way down, it doesn't open from its local location (in the same folder as the program). Could it be the filepath? Compiler? (I guess that's GCC.)

Suggestions? Thank you...

Extremely doubtful it's anything to do with the compiler. Easy enough to test.

transInfo.open("transinfo.txt");

Change it to a non-existent file like "transinfo2.txt". Compile and run. Make sure you get the "File was not opened correctly" error message.

Assuming you do, create that file and put the data into it. Run it and see what results you get. If it still cannot be found, consider it a path issue. Assuming it DOES find the file, now there is for sure only one file, so you know it's found it. See if you get your results or if you get FBody's and others' results. If so, FBody must be right and it's finding some file somewhere else.

commented: Good Suggestion. +5

Cant blame gcc

My GCC version

compiled on ubuntu 10.04 LTS GCC Target: i486-linux-gnu
Thread model: posix
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)

Compile options

g++ main.cpp -O0 -g -o testapp

output

./testapp
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
23503Receive 50 units of car 3
1Receive 75 units of car 1
0Receive 40 units of car 0
3Sell 25 units of car 3
5REJECTED: Insufficient Stock
6Receive 50 units of car 6
8Receive 300 units of car 8
3INVALID TRANSACTION CODE6
0Receive 30 units of car 0
2Receive 10 units of car 2
6REJECTED: Insufficient Stock
4Receive 30 units of car 4
6Receive 200 units of car 6
5REJECTED: Insufficient Stock
6Sell 100 units of car 6

0 70
1 75
2 10
3 25
4 30
5 0
6 150
7 0
8 300
9 0


Total potential income: 450000

EDIT: i dont know if this is what you need to do but i changed the file opening line to transInfo.open("./transinfo.txt"); to specify that the input file is in the same directory as the program

As written, your data file needs to be in the same directory as the currently-running executable. You'll have to see where the finished executable is being placed and run from, then put a copy of your data file in that same location.

EDIT:
Wow, where did all those posts come from??? :-O

commented: Goes without saying that the attacks aren't justified. +13

It is advisable for you to get a grip. This type of post is extremely unprofessional and immature.

Your values aren't all coming to zero (0). They are initialized to 0 when the program starts, which they should be, but they get modified as it executes:
Really, all that I see are some output formatting issues and an issue with the accumulator in your Sales() function. What I don't see is the issue you're describing...

Messed up, A-Drag. You're unprofessional and immature. and, most of all, unreasonable. I have my reasons. unlike everyone else, and unlike my compiler. Thank you.

And in response to your intentionally condescending/smartass "tips" (i.e., not intended to be helpful):

1) I KNOW they are initialized to zero when the program starts. I made that happen. You're an idiot. I also made them get modified as the program executes. All my doing. obviously. Get a grip.

2) Output formatting issues are a GIVEN because all I'm trying to do is get the ******** DATA to read in correctly for now, before I ******* FORMAT ****. That's obviously the proper order by which to refine and work on it. ...obviously...

3) As for this issue with the accumulator in my Sales() function that you speak of... I'm not there yet. Again: obviously. This is a total rough draft, I'm taking it step by step, which turns out to make the most sense. You're an idiot.

Anyone who hates me like you do is just retarded and you're rude, as well as owned. so get a grip. grow up. **** off :) A-D-bag ;)

commented: Jerk -1
commented: Unbelievable -2
commented: watch your feet with that gun +0
commented: You've more than shown your immaturity in past threads. Case closed. -2

Messed up, A-Drag. You're unprofessional and immature. and, most of all, unreasonable. I have my reasons. unlike everyone else, and unlike my compiler. Thank you.

And in response to your intentionally condescending/smartass "tips" (i.e., not intended to be helpful):

1) I KNOW they are initialized to zero when the program starts. I made that happen. You're an idiot. I also made them get modified as the program executes. All my doing. obviously. Get a grip.

2) Output formatting issues are a GIVEN because all I'm trying to do is get the ******** DATA to read in correctly for now, before I ******* FORMAT ****. That's obviously the proper order by which to refine and work on it. ...obviously...

3) As for this issue with the accumulator in my Sales() function that you speak of... I'm not there yet. Again: obviously. This is a total rough draft, I'm taking it step by step, which turns out to make the most sense. You're an idiot.

Anyone who hates me like you do is just retarded and you're rude, as well as owned. so get a grip. grow up. **** off :) A-D-bag ;)

And if I get another infraction for this then you forum administrators here at DaniWeb are clearly unjust. For the profanity is totally understandable -- but this is just a matter of truth. I'm sincere. Who's with me. For example, I'm plenty nice about helping people out. I sacrifice myself to help people out. Who's right? Obviously me if it's between A-Drag and I. Not trying to stir anything up, just trying to rush through it and settle it. and show D-bag that he's not supposed to be trying to start problems. Isn't everyone here to resolve problems? Nobody likes problems. That's why I'm so friggen frustrated. Peace

thanks everyone else who's not a d-bag like A-Drag

commented: See ya. -3

I agree with Fbody that you probably have multiple copies of transinfo.txt. Suggest you search your hard drive and delete all of them except one.

And try being a little nicer, or you might find yourself banned for awhile.


>>thanks everyone else who's not a d-bag like A-Drag
I'll have you know I'm no d-bag. I'm an ass hole, or so I've been told. But did I post profaniy??? NOoooooooooooo. Now whose calling the kettel black.

:-O WTH!

>>1) I KNOW they are initialized to zero when the program starts. I made that happen. You're an idiot. I also made them get modified as the program executes. All my doing. obviously. Get a grip.

int transtype, autonumb, units, i;

Your array is initialized, but your variables "transtype", "autonumb" and "units" are in fact uninitialized. This is a declaration statement, not an initialization statement, there is a difference.

>>2) Output formatting issues are a GIVEN because all I'm trying to do is get the ******** DATA to read in correctly for now, before I ******* FORMAT ****. That's obviously the proper order by which to refine and work on it. ...obviously...
You didn't say that this was a "draft", you never even implied it.

>>3) As for this issue with the accumulator in my Sales() function that you speak of... I'm not there yet. Again, obviously. This is a total rough draft, I'm taking it step by step, which turns out to make the most sense. You're an idiot.

You're an idiot and a horse's backside for treating people like this when they're trying to help you. Enjoy solving your problem on your own...

I agree with Fbody that you probably have multiple copies of transinfo.txt. Suggest you search your hard drive and delete all of them except one.

And try being a little nicer, or you might find yourself banned for awhile.

Quit it Drag. You're instigating. I disagree with you. as anyone should.

And cool, glad you agree with his *inquiry* about whether I have multiple copies of transinfo.txt on my hard drive... because that just means you're totally wrong, because I don't, so you're not helping, again. I don't know how you can just assume that the mistake you want me to be making is actually being made... just because you want it to be. You crazy!

Anything else I can try? all crazy & persistent assumptions aside?

:-O WTH!

>>1) I KNOW they are initialized to zero when the program starts. I made that happen. You're an idiot. I also made them get modified as the program executes. All my doing. obviously. Get a grip.
Your array is initialized, but your variables "transtype", "autonumb" and "units" are in fact uninitialized. This is a declaration statement, not an initialization statement, there is a difference.

>>2) Output formatting issues are a GIVEN because all I'm trying to do is get the ******** DATA to read in correctly for now, before I ******* FORMAT ****. That's obviously the proper order by which to refine and work on it. ...obviously...
You didn't say that this was a "draft", you never even implied it.

>>3) As for this issue with the accumulator in my Sales() function that you speak of... I'm not there yet. Again, obviously. This is a total rough draft, I'm taking it step by step, which turns out to make the most sense. You're an idiot.

You're an idiot and a horse's backside for treating people like this when they're trying to help you. Enjoy solving your problem on your own...

Why wouldn't this be a draft when I can't see what I'm doing.
and why can't you be civil


More importantly though let me address that then... I'm going to try to initialize them right now but in case it doesn't work let me just ask here real quick... Initialize to zero? I didn't initialize those variables because they're supposed to be read in. Not too clear on all that yet I guess...

So if this is a solution to my problem, thank you very much, I don't know why you needed to be so inappropriately insolent along the way.

Initialized transtype, autonumb, and units all to 0. Nothing doing. Isn't it the compiler? I don't understand! My other programs work and read the data from my files, and they're set up exactly the same -- with the text files in the same [relative] locations, the same codes, etc. What the hell.

Dude, chill!

Basically your code works for everyone but you, we have tried various compilers, ide's and OS's this would lead me to the conclusion that you dont know how to use your particular IDE/Compiler. I would suggest you try using a command line tool for a while (gcc is best i would say) until you get a good feel for how the whole process works.

The bottom line hard fact is, the code is sound, to get the result you get it has been verified to be caused by an empty input file so its something your doing wrong.

Sorry but thats just how it is

>> Isn't it the compiler?

It isn't the compiler. You're on your own solving this. No one wants to help you due to your combative behavior. Go see your professor. Make sure you show him this thread.

Oh I give up too. I tried to help but all I got was shit. Let him solve the problem himself.

commented: Everyone here knows you're not at fault. +13

Don't stop helping, i haven't seen a rant like this in ages.:twisted::D

Dude, chill!

Basically your code works for everyone but you, we have tried various compilers, ide's and OS's this would lead me to the conclusion that you dont know how to use your particular IDE/Compiler. I would suggest you try using a command line tool for a while (gcc is best i would say) until you get a good feel for how the whole process works.

The bottom line hard fact is, the code is sound, to get the result you get it has been verified to be caused by an empty input file so its something your doing wrong.

Sorry but thats just how it is

Right, except all my other programs work. Quit not being helpful and be helpful or at least cordial jesus

Either way I went on a school computer and did it all after class, "thanks anyway" :-D

>> Isn't it the compiler?

It isn't the compiler. You're on your own solving this. No one wants to help you due to your combative behavior. Go see your professor. Make sure you show him this thread.

Respect

Oh I give up too. I tried to help but all I got was shit. Let him solve the problem himself.

YOU did not try to help, thanks :) I always help myself that's what I'm saying, no one ever seems to be of any help on here. I ask you guys for help, but always end up solving it myself. So boo you whore

I've kept my mouth shut on this so far, but that's too far, AD does not deserve that. I'm sort-of glad that Ezzaral banned you.

On at least one occasion you were ranting at AD in response to my post. He did not deserve that, his posts were very helpful, as always, and you just chose to dismiss them as irrelevant. That was your choice, and yours alone.

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.