![]() |
|
#1
|
|||
|
|||
C programming not giving correct valueshi, i am new to these forums and C. i am working on a project that is supposed to take data and use it in an equation. the thing is, that the teacher is going to use a different file with a diffrent # of rows. my program seems to run, but gives the incorrect values. i think i have a problem with my fscanf function. the input file has the data as shown below...
100000 2.6 1 100000 2.6 25 500000 2.6 40 100000 3.14 40 100000 3.14 41 100000 3.14 42 100000 4.01 42 heres my program... CPP / C++ / C Code:
my program should be calculating one value for each row. the first column being CurrentYr, 2nd being Rate, and third being nYrs. lastly the correct values are... 234000 615385 615385 538007 780464 538007 error (reason not stated in this post) i would really appreciate any help. i am trying to read data one line at a time, but feel i am misunderstanding how fscanf works. thank you all. greg |
|
#2
|
||||
|
||||
|
Try printing the values you are reading immediately after the fscanf() to make sure you're reading the values corectly.
CPP / C++ / C Code:
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#3
|
||||
|
||||
|
Quote:
When I run this code, while trying to debug your code I encounter some errors (logical) : CPP / C++ / C Code:
run my example and see for yourself what you are reading into your loop. I'd suggest reading line by line using fgets into a variable and then using sscanf to read the information stored inside the current line. Best regards, Kobi Hikri. P.S I know some people here would argue about the usage of fgets but I suggest you get to know this function. |
|
#4
|
|||
|
|||
|
i made some changes...
CPP / C++ / C Code:
this almost gives me the correct answers, but i need them the variables CurrentYr,nYrs,i,NextYr, and FinalYr to be integers so that the answers will be those stated in my first post. the problem however is, when i change the code to initialize the variables as integers and make the proper changes to my fscanf function and printf function, i get the same wrong values i got in the very beginning. below is an example of this code... CPP / C++ / C Code:
Anyone know why this is the case and how to fix it? thanks again! Greg |
|
#5
|
||||
|
||||
|
Details please. "i get the same wrong values" could mean you tried to read 25 and got 51, 23.4 read as 78.00. Did you try my suggestion?
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#6
|
|||
|
|||
|
Quote:
When I have a program that is giving me the "wrong" answer, I make the program tell me what it is doing. That is, in general, faster than asking some expert for help. One of the most important aspects of programming is debugging. (I say that because, in my case, I spend less than 10% of my time writing code and more than 90% of my time debugging.) It's OK to ask for help (that's what some of us like to do: help). It's more edifying to learn when it is that you can help yourself. The hardest ones to debug are the ones that compile without any error or warning messages but don't give any output (I run the program and "nothing" happens). Or, maybe the ones that start up OK and then give some strange operating system message box with something like, "Your program crashed, would you like to send an e-mail to Bill Gates reporting this malevolent activity." The easiest ones to debug are programs that give answers but not the answers that I expected. There are a couple of ways that I can think of that the program can give "wrong" answers: 1. The expressions aren't being evaluated. (Due, perhaps, to some error in program flow control). 2. The expressions aren't working on the values of variables that I thought they would see. (Due, perhaps, to improper input operations.) 3. Something is wrong with the expression: Either there is an error in the expression (multiplied when I should have added), or there is some difference between the way I evaluate the expression and the way that code generated by the compiler evaluates the expression (precedence rules --- need parentheses, arithmetic overflow, integer arithmetic, or some such thing). 4. All of the above (or, maybe, some combination of two or three things). Here's my approach: Look at the values used in the expressions. If the expressions are working on the right values but they give the "wrong" answer, then there must be something about the expression that doesn't work as I expected. Something like this: CPP / C++ / C Code:
Try this on an input file that has the single line Code:
Look at what it tells you. If you still don't understand, then post the exact output that you got. Tell us what you expected to get. What is the answer you get when you manually apply this set of input values to your formula with a calculator, or ---gasp--- pencil and paper? Regards, Dave Last edited by davekw7x : 12-Sep-2005 at 09:36.
|
Recent GIDBlog
Stupid Management Policies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Tutorial] GUI programming with FLTK | dsmith | FLTK Forum | 10 | 03-Oct-2005 16:41 |
| Graphics programming with TIFF file format | confused_pig | C Programming Language | 1 | 10-Jan-2005 08:53 |
| which language ? | onauc | C++ Forum | 2 | 19-Nov-2004 03:53 |
| GUI programming | crystalattice | C++ Forum | 5 | 14-Sep-2004 13:17 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The