You are here:

C++/Programming

Advertisement


Question
I have the following program in C++... A user enters a length in INCHES and i convert it to the listed units. I am trying to get it to work for CM first. and it wont... here is what i have so far:


#include <stdio.h>
#define CM=(2.54*inch)
void main(void) {

float inch,cm,mm,m,yrd,mls,km,ft;

printf("\nEnter Length In Inches: ") ;
scanf("%d",&inch);

if (inch<0){
   printf("Length CANNOT Be Negative");
}

if (inch>=0){
   
   
   printf("%d","CM");
}}    

Answer
Dear Karl,
Thankyou for your question. Well i've just
seen your code and first of all i would like
to clarify some errors you've made.

You defied the constant cm = 2.54 * inches

This statement binding is at compile time. But
internally you've declared the variable inches
as local variable, whose life starts when you
enter the main block and when the compiler translates
this statement at compile time, the value of inches is
not known, so it puts some garbage value in the cm
variable. If you want this statement to work, you shoud
declare "Inches" as global variable and also initilize
it with some initial value.

On the other hand, you don't need to defind cm or
inches as a constant, you can simple start its working
inside the main block. Well thats my version of code
take a look and i guess that will help you clarify your
concept.

#include<iostream.h>
#include<conio.h>
#include <stdio.h>

//#define CM=(2.54*inch)

void main(void)
{
  clrscr();
  float inches, centimeter;

  //Enter the length in inches
  cout<<"Enter the length in inches: ";
  cin>>inches ;

  if( inches < 0 )
  {
     cout<<"\nLength cannot be in negative number";
  }

  else
  {
     centimeter = 2.54 * inches ;
     cout<<"\nLength in centimeters = "<< centimeter<<endl;
  }

  getch();
}

Hope this helps.Dont be afraid to ask a question, if you
encounter any difficulty.

Regards,
Professional

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Professional

Expertise

I can answer any question about functions,pointers,structures,object oriented programming basics of classes and data structures.My strong field is structured programming.

Experience

I have got 2 years experiece under C . I am able to answer about the structured concepts pointers to a little extent,OOP concepts. I have also experience in data Strucutres like Linked List, Stacks , Queues, Heaps, B Trees, Red Black Trees. I will try to satisfy with my knowledge. I am the Student of an expert here Martin, what i have learnt today, i just owe my every knowledge to him. He is the greatest.

©2016 About.com. All rights reserved.