C
C
CodeInside2015-09-20 12:46:02
C++ / C#
CodeInside, 2015-09-20 12:46:02

What is the logic error in the code?

Task: convert centimeters to meters. I want to do it through pointers.

#include <iostream>
using namespace std;
void main()
{
  double *cm,*m;
  cout << "Centimeters: ";
  cin >> *cm;
  *m = *cm / 100.;
  while(1)
  {
    if(*cm <= 0)
    {
      cout << "incorrectly entered data\nInput centimeters again: ";
      cin >> *cm;
    }
    else
    {
      printf("%d centimeters = %d meters",*cm,*m);
                        delete cm,m;
      break;	
    }
  }

  system("pause");
}

Throws "Unhandled Exception..."

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Armenian Radio, 2015-09-20
@CodeInside

A classic of the genre - writing to an uninitialized pointer.
Read the textbook for pointers better.
double *cm doesn't point anywhere. To indicate, you need to assign. For example, yes.
double* cm=new double.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question