G
G
guzya0072020-11-02 15:52:35
C++ / C#
guzya007, 2020-11-02 15:52:35

How do I set a decimal in my code?

How do I get decimal to work in my code as soon as I set variable a to a fractional number . The program immediately calculates incorrectly and terminates. Silly questions . But I have a subject such as Fundamentals of Algorithmization and Programming, I just started to teach. On the remote figs you will understand

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

using namespace std;
int main()
{
  setlocale(LC_ALL,"Russian");
int a,b,S,P;
printf("-------------------------------------\n\
Программа для вычисленияn\n\периметра и площади прямоугольника.\n\
Выполнил: ФИО\n\-------------------------------------\n\ИСХОДНЫЕ ДАННЫЕ:\n");
printf("Длина (м) a=");
scanf("%i", &a); 
printf("Ширина (м) b=");
scanf("%i", &b);
printf("ОТВЕТ:\nПериметр P=%i (м)\nПлощадь S=%i (м.кв)",(a+b)*2,a*b);



getch();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2020-11-02
@wataru

You are using the int type. Fractions require a float or double.
It should be displayed and read not through "%i", but through "%f" or "%lf".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question