Comparing Const Chars

Okay, so I've been working on my final for days, and I've gotten it to run without errors or crashing, but I have this little section here I'm having trouble with.

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
void updateArray(fstream &file, string filename, InventoryCost inv[SIZE]) {
	Edit it;
	file.open(filename, ios::in | ios::binary);
	if (!file) {
		cout << "ERROR opening file!\n";
	}
	file.clear();
	file.read(reinterpret_cast<char *>(&it), sizeof(it));
	while (!file.eof()) {
		cout << it.itID << " " << it.code << " " << it.edit << endl;
		for (int i = 0; i < SIZE; i++) {
			if (inv[i].getID() == it.itID) {
				if (strcmp(it.code,"AI")==0) {
					int add = (stoi(it.edit));
					inv[i].setQty(add);
				}
				else if (strcmp(it.code, "RI") == 0) {
					int sub = (stoi(it.edit));
					inv[i].setQty(sub);
				}
				else if (strcmp(it.code, "CW") == 0) {
					double whsale = (stof(it.edit));
					inv[i].setWCost(whsale);
				}
				else if (strcmp(it.code, "CR") == 0) {
					double retail = (stof(it.edit));
					inv[i].setRCost(retail);
				}
				else if (strcmp(it.code, "CD") == 0) {
					inv[i].setDesc(it.edit);
				}
				else
					cout << "Invalid Transaction Code\n";
			}
			else
				cout << "Invalid ItemID\n";
		}
		file.read(reinterpret_cast<char *>(&it), sizeof(it));
	}
	file.close();
}

It seems to skip through the if else statements and just tells me invalid id everytime. I put the cout stuff above to test and it shows up as it should. why aren't the strcmp's doing their jobs?
Why did you open new topic for an existing problem?
http://www.cplusplus.com/forum/beginner/190275/

How is Edit defined?


If the file doesn't open properly you should probably return from the function, just clearing the error probably won't do any good.
Last edited on
Right now the output looks something like this:
**********************************
*________MENOCU TOOL SHOP________*
**********************************
*  1. Display Inventory          *
*  2. Create a Transaction File  *
*  3. Update Inventory           *
*  4. Display Updated Inventory  *
*  5. Compute Profit Projection  *
*  6. EXIT                       *
**********************************
 Menu Choice: 2
How many records to ammend: 1
Her are the Transaction Codes:
        AI: Add to Inventory
        RI: Reduce from Inventory
        CW: Change Wholesale Cost
        CR: Change Retail Price
        CD: Change Description

Enter the ItemID #: 597895
Enter the Transaction Code: AI
Enter New Data: 54654
**********************************
*________MENOCU TOOL SHOP________*
**********************************
*  1. Display Inventory          *
*  2. Create a Transaction File  *
*  3. Update Inventory           *
*  4. Display Updated Inventory  *
*  5. Compute Profit Projection  *
*  6. EXIT                       *
**********************************
 Menu Choice: 3
597895 AI 54654
Invalid ItemID
Invalid ItemID
Invalid ItemID
Invalid ItemID
Invalid ItemID

I realize its running to many times, In my create transaction file I'm going to have the user choose how many entries they want to edit and get that into this function, but for now this is fine.
Clearly the file and read is working from the cout statements. but why is it failing the comparisons?
Last edited on
If you are seeing "Invalid ItemID", that is because inv[i].getID() == it.itID, from line 12, is false. Since line 12 is false, lines 13-30 will not be executed.
if that's false, why doesn't the output also say
Invalid ItemID
? This where I'm stuck.
Dose my reading of the file change everything in the struct 'Edit it' into chars?
@jlb
Why did you open new topic for an existing problem?
I'm sorry I figured it was dead, and this is slightly different than yesterday's woes.
1
2
3
4
5

struct Edit {
	int itID;
	char  code[3], edit[51];
};


I am leaving an extra character for the null terminator, I thinks this is correct right?
Last edited on
if that's false, why doesn't the output also say
Invalid ItemID
? This where I'm stuck.

The output you posted had 5 copies of "Invalid ItemID". This implies that line 12 was evaluated to be false 5 times.
okay, how should i go about this then? the inv[i].getID() is a int that returns an int, and the it.ID is an int.
Last edited on
@kevinkjt2000 Sorry I wasn't paying attention. took that output as "Invalid Transaction Code" haven't had much sleep this week.
I am still trying to answer your original question:
It seems to skip through the if else statements and just tells me invalid id everytime. I put the cout stuff above to test and it shows up as it should. why aren't the strcmp's doing their jobs?


You should investigate why the condition that you thought would be true is actually false. In other words, investigate why inv[i].getID() != it.ID. Earlier you claimed that your "cout stuff" shows up as it should, but you only tested it.ID and did not include inv[i].getID() in your "cout stuff" tests.

P.S.: https://xkcd.com/1024/
Last edited on
Thanks man, I put a breakpoint in the code so i could see what variables are there. They seem to match... arggg
Well I can see that they match for one line of output "597895 AI 54654" that happens before the 5 lines of "Invalid ItemID".
I've even gone so far as to make new int and assign them the value of inv[i].getID() and it.ID. They match why is this failing I just don't get it.
Do they match all 6 times? Or just the first time?
just the 1st, but i still get 5 lines of Invalid Item ID. it's not even matching the correct one

Check this out:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
file.clear();
	file.read(reinterpret_cast<char *>(&it), sizeof(it));
	while (!file.eof()) {
		
		for (int i = 0; i < SIZE; i++) {
			cout << it.itID << " " << it.code << " " << it.edit << endl;
			int itemNum, Ednum;
			itemNum = inv[i].getID();
			Ednum = it.itID;
			cout << itemNum << " " << Ednum;
			if (itemNum == Ednum) {
				if (strcmp(it.code,"AI")==0) {
					int add = (atoi(it.edit));
					inv[i].setQty(add);
				}

Gives me this:
 Menu Choice: 3
209875 AI 5435
209875 209875209875 AI 5435
176524 209875Invalid ItemID
209875 AI 5435
340965 209875Invalid ItemID
209875 AI 5435
453285 209875Invalid ItemID
209875 AI 5435
893167 209875Invalid ItemID
209875 AI 5435
597895 209875Invalid ItemID

The 1st Number matches the number and still nothing.
never mind, it worked.... now, what;s the best way to date the updates? is there a way to just pull Month/Day/Year with cTime?
Why are you reading the file when you should probably already have all the information in the inv[] array?

You really really need to simplify your functions. They should do as little as possible, ideally one thing, and do it well.

Is it an assignment requirement to use binary files? Using binary files, adds a lot of complication to the program, so if it's really not necessary then just go with regular old text files instead.

If you are required to use binary files, what exactly does your input file contain.

For your current problem you may want to insure that there are no hidden characters in either of your strings, check that the lengths() are the same and that the number of visible characters match the reported lengths.

The file comes from another function where the user chooses what ID, Transaction Code and Edited Data. The file is then used to update the existing array of class object. This is all solved now though. Thanks for everyone's input it was very helpful.
Topic archived. No new replies allowed.