Thursday, August 18, 2022

[FIXED] How can i get an input of 3 double variables with cin in C++?

Issue

I must take 3 double variables as an input and find their mean. If i input a integer as a input(for example 5) program works. but if i input a fractional number(for example 5.3) it wont take the other 2 inputs and close.

here is my code:

#include <iostream>
using namespace std;
int main()
{
   double y1,y2,y3,ort; 
   cout<<"1. input : \n";
   cin>>y1;
   cout<<"2. input : \n"; 
   cin>>y2;
   cout<<"3. input : \n";
   cin>>y3;
   ort=(y1+y2+y3)/3;
   cout<<"Value : "<< ort << "\n" ; 
   system("pause");
   return 0;
}

Solution

Your program works as expected. At least the mean is computed accordingly, the pause call is not working for me.

1. input : 
1.3
2. input : 
2.3
3. input : 
3.3
Value : 2.3
sh: 1: pause: not found
Press <RETURN> to close this window...

Maybe you have wrong locale set.

You can try add:

#include <locale.h> 
setlocale(LC_ALL,"C")

As I just read your comment if you input 3 its working. but if you input 3,2 its not working., floating points are by programming standards separated with ., not ,. E.g. you have to write 3.2. That is what English speaking countries have also in their standard.



Answered By - Jan Hackenberg
Answer Checked By - Katrina (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.