GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #11  
Old 06-Sep-2004, 22:40
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Let's do it a step at a time:

Go back to your original query.cpp, and add the small loop that I indicated in the previous post at the end of the do-while loop in the two functions.

Your interactiveOpen() functions don't return a value, and you don't try to use a return value, so leave them as void. I was merely trying to impart some indication of the way things are "usually" done. Forget it.

Now what's up with these:
CPP / C++ / C Code:
	assert (income==('A' || 'B' || 'C'));	//ensure only 3 income groups
	assert ((rating1 >= 1) && (rating1 <= 10));	//ensure valid ratings


I have two problems with this:

1. This is, in my opinion, not a proper use of assert. Using assert statements here is not necessarily "wrong", but assert is usually reserved for program checks of things that really, really shouldn't happen. Like in case you had previously checked input data and should never have gotten this far with bad data.(In other words a program bug that you need to get to the bottom of.)

Cases where user input data are wrong happen quite often, and you can expect it in any program and should take care of it more elegantly. For example, you should indicate to the user what was the line number where the error occurred and exactly what the error was. Giving the user an "assertion error, abnormal program termination" is not really better than some operating system message about floating point check or something.

2. Furthermore, the logic in the first comparison is wrong.

The logic operator || works as follows.

Consider the expression xxxx || yyyy, where xxxx and yyyy are any C/C++ expressions. Now, in C/C++ this is evaluated as follows:

xxxx is evaluated (it may be an arithmetic expression or a logical expression).

If the value xxxx is non-zero, then the entire expression is given a value of 1, and expression yyyy is not evaluated.

If the value of xxxx is zero, then the expression yyyy is evaluated. If the value of yyyy is non zero the entire expression is given a value of 1. If the value of yyyy is zero, the entire expression is given a value of 0.

Now, consider the expression
CPP / C++ / C Code:
 if (income == 'A')

Thi expression in parentheses is a logical expression, and it takes on the value 1 if the char variable income has the value of 'A', otherwise the value is of the expression is 0. The value of 1 corresponds to "true" and the value of 0 corresponds to "false".

The way you use || is in expressions like
CPP / C++ / C Code:
if ((income == 'A') || ( income == 'B') || (income == 'C')) {
  // do something if the expression is true
}
else {
 // do domething if the expression is false
}

Now what is the value of this expression
[c]
('A' || 'B' || 'C')
[c]

Well, what do you think?

Then, if char variable income has the value 'A', what is the value of
CPP / C++ / C Code:
income == ('A' || 'B' || 'C')

Try the following: (Don't sneak a peek at the answer below; try to apply the rules I stated above to predict the answer. Compile and run the program to see if the program output agrees with your prediction. This is really important.)

CPP / C++ / C Code:
#include <iostream>

using std::cout;
using std::endl;

int main()
{
  int value1;
  int value2;
  char mychar;
  mychar = 'A';
  value1 = 'A' || 'B' || 'C';
  cout << "value1 = " << value1 << endl;

  if (mychar == ('A' || 'B' || 'C')) {
    cout << "Test number 1 passed" << endl;
  }
  else {
    cout << "Test number 1 failed" << endl;
  }

  cout << endl << endl;

  value2 = (mychar == 'A') || (mychar == 'B') || (mychar == 'C');
  cout << "value2 = " << value2 << endl;

  if ((mychar == 'A') || (mychar == 'B') || (mychar == 'C')) {
    cout << "Test number 2 passed" << endl;
  }
  else {
    cout << "Test number 2 failed" << endl;
  }

  return 0;
}

Did you get what you thought? Test number 1 is testing to see if mychar is equal to 1 (it isn't). That's not what you intended. Test number 2 shows how to use the logic || operator.

Dave
  #12  
Old 07-Sep-2004, 02:23
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by crystalattice
Here's what I've written so far. I do have cout << statements in it for troubleshooting, but it doesn't even get far enough to use them...
CPP / C++ / C Code:
cout << "This program will determine product rating information based on\n" << "respondent income level.\n\n";
...
cout << "Enter the input file name: ";

Quote:
Originally Posted by davekw7x
As far as debugging when you get an error message that seems to indicate faulty arithmetic ("floating point exception" probably indicates division by zero), put a cout<< or printf() just before expressions that involve division.
...
p.s. printf() is your friend....

One thing that must also be considered using C++ console I/O (and this may not be affecting you -- but then again...) C++ uses buffered I/O. Which means when you output something with cout it may place it in a buffer but will wait until the buffer is full before actually outputting the information. Therefore your cout statements may be pointing to your problem...

You need to get into the habit of using cout properly. If you need to see the information immediately, flush the buffer. This is done 2 ways:
1) cout << "This program will determine product rating information based on" << endl << "respondent income level." << endl << endl; (one at the end will in fact suffice, but you might as well go full bore)
IOW, to output a new-line use endl instead of '\n'
2) cout << "Enter the input file name: " << flush;
This will output the buffer without a new-line.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #13  
Old 07-Sep-2004, 09:22
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by crystalattice
I found that the Query.cpp file was bad. I added the line
CPP / C++ / C Code:
ifstream theIFStream;
to it and now I don't get the floating point error, and I can get the rest of the program to run. However, the output is incorrect so I know it's not reading the input file.

Well, since you declare theIFStream inside the interactiveOpen() function, the parameter theIFstream is ignored. The file is opened but is unavailable outside the function where theIFStream is declared. (So, main() cannot read from its version if theIFStream, since it was not opened.)

I think you have lost focus. The objective is not to eliminate those pesky error messages, but to fix the program so that it will do the right thing. You were reading the correct input file previously, then you correctly concluded that your change caused it not to read the input file. So, unfix the fix, then proceed.

Here's something else to consider. Back up a minute and forget about error messages and dividing by zero and all other good and bad things that happen when you run the program.

What is the program supposed to do? Read in some stuff and calculate average values of a couple of things.

How would you calculate the average value of some stuff if you didn't have a computer? Add up the items and divide by the number of items. Right?

So, now, suppose we have a function that would give us a known number of integers. For example, suppose we have a function, say GetNextItem(), that can be called exactly N times to give us the items. How would we find the average with a program. Here is a snipplet that assumes all variables have been declared and defined appropriately:

CPP / C++ / C Code:
  sum = 0;
  for (i = 0; i < N; i++) {
    sum += GetNextItem();
  }
  average = (double)sum / N;

See the plan: add up all of the values, then, and only then, do the division.

Of course your program doesn't know in advance how many things there will be, so your loop to read input lines and call the functions for each line is organized OK.

Thinking about the above code, I hope that you see that your functions avgRatingProd1() and avgRatingProd2() shouldn't do any division(!!!!!!!!) Does this make sense to you? That not only gets rid of those pesky "divide by zero" messages, it also gives the program the possibility of doing the right thing.

Regards,

Dave

(p.s.: I have sent you a Private Message on this board with some additional information. Check it out.)
  #14  
Old 09-Sep-2004, 20:12
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,628
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Talking

Making progress


After I made some changes, I got the program to read the test file (thanks for the hints about flushing the '\n' from the buffer; that appears to be part of the problem) but I have some new errors to deal w/. However, I think there just some "easy" math/logic errors in the other 2 functions.

But, I am making significantly more progress than before. I greatly appreciate all the help. Thank you. I'm sure you'll hear more if these new problems kick my butt too.
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
  #15  
Old 10-Sep-2004, 08:25
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by crystalattice
After I made some changes, I got the program to read the test file (thanks for the hints about flushing the '\n' from the buffer; that appears to be part of the problem) but I have some new errors to deal w/. However, I think there just some "easy" math/logic errors in the other 2 functions.

But, I am making significantly more progress than before. I greatly appreciate all the help. Thank you. I'm sure you'll hear more if these new problems kick my butt too.

OK! Don't let it grind you down. Kick back!!!

Regards,

Dave
  #16  
Old 10-Sep-2004, 22:19
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,628
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Talking

Yaaayyy!!!! I finally got it!!!


I finally got it working as it should! Thanks for everyone's help. I really, really appreciate it.
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
  #17  
Old 11-Sep-2004, 08:37
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Go, Navy!

Dave
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Encryption implementation issue bigbangman C Programming Language 6 02-Sep-2004 12:21
Floating point operand? warny_maelstrom C Programming Language 11 04-Mar-2004 14:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:09.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.