Why does it not add up

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

53 Replies - 2393 Views - Last Post: 18 June 2010 - 02:03 AM Rate Topic: -----

#1 lew3611  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 22-April 10

Why does it not add up

Posted 13 June 2010 - 10:49 AM

[\]Hi all, I am trying to figure out why I continue getting some off the wall output from this code that I have written. Here it is and when I simulate I continue getting -1432.......etc and I do not know why:

/* 
 * 
 * This program converts distance in miles to kilometers.
 */
#include <stdio.h>						/* printf and scanf standards from the library						*/
#include <cstdlib> 
#define KMS_PER_MILE 1.609				/* conversion constant												*/

int
main ()
{
	double miles, kms;					/* input - distance in miles and output - distance in kilometers	*/

	/* Get the distance in miles,			*/
	printf_s("Enter the distance in miles>");
	scanf_s("%1f", &miles);

	/*Convert the distance to kilometers.	*/
	kms = KMS_PER_MILE * miles;

	/* Display the distance in kilometers.	*/
	printf_s("That equals %f kilometers.\n" , kms);

	std::system ("PAUSE");
	return(0);
} 


Thanks
Lew

This post has been edited by JackOfAllTrades: 13 June 2010 - 10:51 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Why does it not add up

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Why does it not add up

Posted 13 June 2010 - 10:52 AM

Typo:
scanf_s("%1f", &miles);


should be:
scanf_s("%lf", &miles);


Was This Post Helpful? 3
  • +
  • -

#3 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 278
  • View blog
  • Posts: 1,588
  • Joined: 15-May 08

Re: Why does it not add up

Posted 13 June 2010 - 11:08 AM

Perhaps try
int main()
{
    float miles = 0;
    float kms = 0;
    printf("Enter D in Miles :: " );
    scanf( "%f", &miles );
    kms = KMS_PER_MILE * miles;
    printf( "\nEquals = %0.2f km", kms );
    getchar();
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#4 lew3611  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 22-April 10

Re: Why does it not add up

Posted 13 June 2010 - 12:13 PM

Sar and bbq, I didnt see the difference between the typo and the "what it should be explanation. I need to try to stick to using only C syntax and constructs. Also how were you able to seperate the code from the rest of my topic like you did? I would like to be able to do those types of things. I will also check the alternative code in the morning as well!

Thanks

Sar

This post has been edited by lew3611: 13 June 2010 - 12:15 PM

Was This Post Helpful? 0
  • +
  • -

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Why does it not add up

Posted 13 June 2010 - 12:18 PM

You were using "%1f" instead of "%lf". 1 is not the same thing as l. Take a look here to find out more about printf format parameters.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5672
  • View blog
  • Posts: 22,524
  • Joined: 23-August 08

Re: Why does it not add up

Posted 13 June 2010 - 01:19 PM

Damn, sarmanu...those are some serious "eagle eyes" you've got there! Good job! I gave it a quick look when I added the code tags and didn't see anything.
Was This Post Helpful? 0
  • +
  • -

#7 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 185
  • Joined: 08-July 08

Re: Why does it not add up

Posted 13 June 2010 - 09:21 PM

try:
 const KMS_PER_MILE=1.609;


Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Why does it not add up

Posted 13 June 2010 - 09:28 PM

View Postbuffalobill, on 14 June 2010 - 01:21 PM, said:

try:
 const KMS_PER_MILE=1.609;



How is that better?

Especially if you are suggesting that as a global then I think you will find most people will tell you that's a far worse idea than using the #define syntax.

What is it you are trying to say and why do you believe it is a better answer than what they OP wrote?
Just dumping a random line of code with neither context nor explanation is not a very helpful approach.
Was This Post Helpful? 1
  • +
  • -

#9 lew3611  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 22-April 10

Re: Why does it not add up

Posted 14 June 2010 - 07:50 AM

Thanks Jan I would like to know why that is better?

Lew
Was This Post Helpful? 0
  • +
  • -

#10 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Why does it not add up

Posted 14 June 2010 - 08:36 AM

While buffalobill's remark was brief to the point of confusion, the recommendation was good. In general, the code I see is a mix of C and C++ idioms.

C: stdio.h
C++: cstdio
C: preprocessor defines.
C++: constants
C: printf/scanf, etc.
C++: streams

Consider each one:
.h style headers are deprecated in C++. So...don't use them. Besides, the effect is that they have using namespace std;, which you don't want automatically in a global context.

Preprocessor defines vs constants. Constants are not variables. They are constant. It is OK to have global constants. In fact, try coming up with an example of local constants.

Preprocessor defines have no type safety and compiler checking. They are rough "copy-replace" instructions. Constants, on the other hand, are parsed and handled by the compiler. If you use an IDE, your IDE will likely recognize the constant as well.

printf and scanf are bad for two reasons. One is that they invoke bad engineering. You need to carefully match up the format specifiers with the variable types. Easy with the primitives. Not so much with non-primitive or aliased types. What specifier do you use then? In fact, the specifier may change based on system, and you see where this gets iffy.

Instead, use streams and the i/o operators they support.
Was This Post Helpful? 0
  • +
  • -

#11 lew3611  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 22-April 10

Re: Why does it not add up

Posted 14 June 2010 - 09:16 AM

Oler,

That's just it I have seen the terms that you have in your post, but I just don't understand them to the magnitude and effect that you guys do. Like I said, I am trying to learn Cand get very good in it but I am having to use the C++ IDE to learn it and everything in the textbook that I have is centered around C and not too much of C++. Just to let you know how bad I did in C for my course, I got a "D" for my grade and everything that is coming up next is going to involve understanding C, i.e. all of my engineering courses are about HDL and I will be expected to use C to program MSI's so I am trying to actually continue studying C to get better and know it from top to bottom but I am getting very confused when you guys or even myself, mix and match C and C++. does this kind of explain where I am with the programming, besides me not knowing anything about programming. Other than that I appreciate you guys taking the time to teach me this great skill.

Lew
Was This Post Helpful? 0
  • +
  • -

#12 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: Why does it not add up

Posted 14 June 2010 - 09:32 AM

View Postjanotte, on 13 June 2010 - 11:28 PM, said:

View Postbuffalobill, on 14 June 2010 - 01:21 PM, said:

try:
 const KMS_PER_MILE=1.609;



How is that better?

Especially if you are suggesting that as a global then I think you will find most people will tell you that's a far worse idea than using the #define syntax.

What is it you are trying to say and why do you believe it is a better answer than what they OP wrote?
Just dumping a random line of code with neither context nor explanation is not a very helpful approach.


Well most modern compilers don't even accept that syntax (since C++ does not have a default data type), and for those that DO accept it, KMS_PER_MILE would be an int, and there for would be given the value of 1... i.e. that was really bad advice.

#include <iostream>

const KMS_PER_MILE=1.609;

int main() {

    std::cout << KMS_PER_MILE << std::endl;
    return 0;
}


better advice might has been to suggest:
const float KMS_PER_MILE = 1.609;

which due to type checking is better than the #define and since it is a constant the fact that it is global does not really matter (most compilers will not actually allocate any storage to it directly).
Was This Post Helpful? 0
  • +
  • -

#13 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Why does it not add up

Posted 14 June 2010 - 09:33 AM

Quote

but I am getting very confused when you guys or even myself, mix and match C and C++.
I'm not mix and matching C and C++. That's what you are doing, and I am pointing it out by throwing out a comparison.

Your C++ compiler does not prevent you from using C. C++ compilers will also compile C. You simply need to write C code (and compile as C). You can't accidentally start writing C++ code.

You are learning from a resource, right? So if you have a book that teaches C, you will not be using C++. The only way C++ code creeps in is if you learn from a C++ resource, or you copy paste your way in programming.

If the former is true, it's your responsibility to acquire a book that teaches C. If you do the latter, you still can only blame yourself.

How did <cstdlib> get into your code? How did std::system?
Was This Post Helpful? 1
  • +
  • -

#14 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: Why does it not add up

Posted 14 June 2010 - 09:36 AM

Two things: to force your compiler to enforce C conventions name your file .c or use the command line switch for c compiling only (what compiler are you using?)


Another option is to get Pelles C which is a nice modern (open source) C compiler for windows. IT has a nice IDE and comes with some pretty good help files.
Was This Post Helpful? 0
  • +
  • -

#15 lew3611  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 22-April 10

Re: Why does it not add up

Posted 14 June 2010 - 11:34 AM

Oler, you are right. I have been mixing. My actual resource is Problem Solving and Program Design in C by Jeri R. Hanley/Elliot B. Koffman. This is the textbook for the Programming in C course that I just took. However, some of the conventions are not working with the IDE that I am using, which is Visual Studio 2008. Nick, I don't know the actual compiler that I am using but I do know that it is part of the VS 2008 IDE. Also, the resource for the C++ constructs are coming from the people here at the forum. Generally they say try this or try that with no explanation of what it is actually for at least not in laments terms. But if you guys will help I will surely follow what you instruct me to do. I need to become as close to fluent in the terminology as I can and the actual experience as well. But for some reason I am getting confused and it is making it difficult to grasp what I am supposed to be using these tools for. As far as the std::system...etc goes, I was trying to find a way to keep the actual dos or simulator window from auto closing so that I could actually view the program running or still so that I could see each line of code executing so I could troubleshoot what I was doing and that is the suggestion that some people from this forum gave me to try.

Lew
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »