العودة إلى موقع برمج

مساعدة في كود بلغة c++

#1

ما هو الخطأ عند تشغيل البرنامج واختيار 1 ثم 1 ثم ادخال المعطيات يتم اعطاء الناتج وبجانبه خطا (جمله ELSE)
#include
using namespace std;
int main()
{
double length;
double width;
double area;
int intput;
float choice;
float perimeter;

cout <<“1-rectangle” << endl << “2-Square” << endl;
cout << “Enter a number…”;
cin >> intput;
if(intput ==1)
{cout << “What is the unknown?..” <<endl
<< “1-length” <<endl
<< "2-width "<< endl
<<“3-area” <<endl
<<“4-perimeter” << endl
<< “unknown is…”;
cin >> choice;}
if (choice == 1)
{ cout <<“What is the width?..”;
cin >> width;
cout << “what is the area?”;
cin >> area;
cout << “length =” << area / width;

 }
	 if (choice == 2)
	    { cout << "What is the length?..";
	    	cin >> length;
	    	cout << "What is the area?..";
	    	cin >> area;
	    	cout << "width =" << length / area;}
	    	if (choice == 3)
	   { cout << "What is the length?..";
	   	cin >> length;
	   	cout << "What is the width?..";
	   	cin >> width;
	   	cout << "area =" << length * width;} 
	   	 if (choice == 4)
	   	{ cout << "What is the length?..";
	   	cin >> length;
	   	cout << "What is the width?..";
	   	cin >> width;
	   	cout << "perimeter =" << 2 * (length + width);}
	   	if (intput ==2)
	 { cout <<"What is the unknown?.."<< endl 
	        <<"1-Length of the side" << endl
			<<"2-area" << endl
			<<"3-perimeter" << endl;
			cin >> choice;}
	  if (choice ==1)
	 {cout <<"What is the perimeter?..";
	  cin >> perimeter;
	  cout << "Length of the side =" <<perimeter / 4 ;}
	  if (choice ==2)
	  {cout <<"What is the Length of the side?..";
	  cin >> length;
	  cout << "area ="  << length * length;}
	  if (choice ==3)
	  {cout <<"What is the Length of the side?..";
	   cin >> length;
	   cout << "perimeter ="<< length * 4;}
	  else {cout << "Error Please try again" ;} 

return 0;

}

#2

الكود صحيح ولا توجد مشكلة
عليك فقط نقل قوس الاغلاق الخاص بالجملة الشرطية if(intput ==1) الى نهاية كامل الكود الخاص بها اي الى نهاية السطر قبل الجملة الشرطية if(intput ==2)

ونفس الشيء بالنسبة للجملة الشرطية if(intput ==2) حيث ثم بنقل قوس الاغلاق الخاص بها الى قبل نهاية الكود وستحل المشكلة

1 Like
#3

لم تنجح نفس الخطا…

#4

تم التحقق وهذا يجب ان يحل المشكلة
يمكنك ايجاد الكود الصحيح هنا:


#include<iostream>
using namespace std;
int main()
{
double length;
double width;
double area;
int intput;
float choice;
float perimeter;

cout <<"1-rectangle" << endl << "2-Square" << endl;
cout << "Enter a number...";
cin >> intput;
if(intput ==1)
   {cout << "What is the unknown?.." <<endl
	     << "1-length" <<endl 
 	     << "2-width "<< endl 
		 <<"3-area" <<endl
		 <<"4-perimeter" << endl
		 << "unknown is..";
		 cin >> choice;
	 if (choice == 1)
	 { cout <<"What is the width?..";
	   cin >> width;
	   cout << "what is the area?";
	   cin >> area;
	   cout << "length =" <<  area / width;
	   
	 
	 
	 }
		 if (choice == 2)
		    { cout << "What is the length?..";
		    	cin >> length;
		    	cout << "What is the area?..";
		    	cin >> area;
		    	cout << "width =" << length / area;}
		    	if (choice == 3)
		   { cout << "What is the length?..";
		   	cin >> length;
		   	cout << "What is the width?..";
		   	cin >> width;
		   	cout << "area =" << length * width;} 
		   	 if (choice == 4)
		   	{ cout << "What is the length?..";
		   	cin >> length;
		   	cout << "What is the width?..";
		   	cin >> width;
		   	cout << "perimeter =" << 2 * (length + width);}
}
		   	if (intput ==2)
		 { cout <<"What is the unknown?.."<< endl 
		        <<"1-Length of the side" << endl
				<<"2-area" << endl
				<<"3-perimeter" << endl;
				cin >> choice;
		  if (choice ==1)
		 {cout <<"What is the perimeter?..";
		  cin >> perimeter;
		  cout << "Length of the side =" <<perimeter / 4 ;}
		  if (choice ==2)
		  {cout <<"What is the Length of the side?..";
		  cin >> length;
		  cout << "area ="  << length * length;}
		  if (choice ==3)
		  {cout <<"What is the Length of the side?..";
		   cin >> length;
		   cout << "perimeter ="<< length * 4;}
		  else {cout << "Error Please try again" ;} 
}
	return 0;
}
1 Like
#5

شكرا لك تم الحل ولكن يبقى هناك سؤال؟

لماذا لم تظهر رسالة الخطأ.ليست مهمة ولكن كمعلومه…

#6

ماذا تقصد
حيث انك يجب تحديد رسالة الخطأ ضمن الكود في حال عدم تحقق الشرط الاول بادخال قيمة 1 او الشرط الثاني بإدخال قيمة 2
حاليا الشرطان لايتحققان ويتم الخروج من الكود