![]() |
|
#11
|
|||
|
|||
|
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:
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:
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:
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:
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:
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
|
||||
|
||||
|
Quote:
Quote:
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
|
|||
|
|||
|
Quote:
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:
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
|
||||
|
||||
Making progressAfter 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
|
|||
|
|||
|
Quote:
OK! Don't let it grind you down. Kick back!!! Regards, Dave |
|
#16
|
||||
|
||||
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
|
|||
|
|||
|
Go, Navy!
Dave |
Recent GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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