![]() |
C Programming Homework Help Needed
1 Attachment(s)
Hi all,
I'm taking an course, "Structured Programming in C" and am having some issues with my homework for the week. My assignment is to write a program that calculates various aspects of projectile motion, but I am running into a problem with an output(?) in my program. When I compile the program I receive no errors or warnings, but when I try to enter values into the first input, it turns the value into '+NAN'. I don't know what '+NAN' is or how to fix it. I've attached a screen capture of what happens when the program is run and values are entered. HELP! Please excuse the messiness of the code, as it is not completed or functional. Code:
/* |
Re: C Programming Homework Help Needed
As one step, you can remove the & sign from your printf statements.
For example, printf("Time of flight is %f seconds.\n", &time); becomes: printf("Time of flight is %f seconds.\n", time); |
Re: C Programming Homework Help Needed
Joel is correct. Remove the & from your printf functions. The & means "address of". Printf doesn't want the address of the location where the number is kept, printf wants the number itself.
FYI, the & is correct in the scanf function. Scanf requires the address of the variable. Btw NAN means "Not A Number". Flash. |
Re: C Programming Homework Help Needed
I'm a bit confused as to exactly how this code compiled, and why it isn't happy. I know exactly enough C to confuse myself.
I understand why the code is wrong. I don't fully understand why this is the error that is given. Since printf takes a variable number of untyped objects, I'll believe that it can't catch the type mismatch of address and float. This must be the case, since it did compile. Since C has no run-time environment to speak of, we must assume that the computer will do exactly what it is told: Take the address and interpret it as a float. The code then loosely translates to: Code:
float time;I may be reading too far into this, but I'd really appreciate the assistance of someone with a deeper understanding of C. Thanks Eric |
Re: C Programming Homework Help Needed
I just ran it in Code::Blocks (open-source IDE, it is quite good), and no matter what number I entered, I got 0.0000 degrees, 0.00000 radians.
Check the scanf documentation, there is probably something small you're doing wrong. Edit: I read the scanf documentation, and there doesn't appear to be anything wrong. Good luck. |
Re: C Programming Homework Help Needed
1 Attachment(s)
Quote:
To answer another question, I'm using the Borland C 5.5 compiler. |
Re: C Programming Homework Help Needed
2 Attachment(s)
Back again...
I'm only concerned with a small portion of my program at the moment. This part collects an integer value for use to set the color of an object later in the program, then checks to make sure it is within a certain range of numbers. If the number isn't within that set of numbers, it is supposed to ask the user to enter again, then collects a value again. Currently, whether the value entered is within the parameters that I want, this chunk of program gets stuck in the error checking loop. Oddly, when you enter a two digit integer, it breaks the loop. Code:
/* Collects shape selection from user and checks for invalid inputs */ |
Re: C Programming Homework Help Needed
Pat,
It looks like you are not being consistant with the data type for "color". You are inputting it as an integer (%d) so I'll assume you've defined it as such, however you are comparing it in the switch statement as a character. The value '0' will be converted to it's ASCII value (48) for the compare. If 'color' is an integer, then I'd try removing the single quotes from around the numbers in the case statements and see if that works. Mike |
Re: C Programming Homework Help Needed
Quote:
|
Re: C Programming Homework Help Needed
I think I need to make this the "Help Pat pass EGR 261" thread. :p
My problem this time is that a switch statement again. After the menu displays, the user can enter a selection, but when they hit enter, it just displays a blank line and doesn't leave the switch statement. Thanks for the continuing help guys.:) Code:
int menu_error; |
Re: C Programming Homework Help Needed
Pat,
I'm not sure the problem is in the switch statement. Try removing the \n from the menu_selection scanf line ("%d" rather than "%d\n") Mike |
Re: C Programming Homework Help Needed
Quote:
|
Re: C Programming Homework Help Needed
Back again!
I can't figure out how to convert a float value to a char string. Suggestions? Thanks! |
Re: C Programming Homework Help Needed
Quote:
Idk how you would get each number individually but to change the number into its ASCII code you would do # + '0' to get the ascii code of the number |
Re: C Programming Homework Help Needed
Quote:
|
| All times are GMT -5. The time now is 14:10. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi