الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

أرجو توضيح ما الخطء فى هذا الكود

مغلق
بدأه Mistrio_xp في 22 مارس 2007 · 6 رد · 452 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع
#include <stdio.h>

#define H 50
#define F 40
#define T 33
#define Q 25

int main(void)
{
	float			price, x;
	float   		newprice;
	char			code;

	printf("Please insert the price of the the article: ");
	scanf("%f", &price);
	printf("Please insert the pricing code: ");
	scanf("%c", &code);

	switch (code)
	{
		case 'h':
			{
				newprice=price*(H/100);
				x=H;
			}
		case 'f':
			{
				newprice=price*(F/100);
				x=F;
			}
		case 't':
			{
				newprice=price*(T/100);
				x=T;
			}
		case 'q':
			{
				newprice=price*(Q/100);
				x=Q;
			}
	}

	printf("\nThe original price was: %d", price);
	printf("\nThe new price is: %f", newprice);
	printf("\nThe amount of discount is: %d%%", x);



getch();
return 0;
}

حيث انى بعد الكوبيل و ادخال الرقمين يعطى اصفار فالنتائج

تم تعديل هذه المشاركة بواسطة Mistrio_xp في 22 مارس 2007 في 19:04

#2

ضع ;break في نهاية كل بلوك case وذلك لكي لا يستمر التمفيذ لباقي بلوكات الcase

لم انتبه لهذه

printf("\nThe original price was: %d", price);

d% تدل على عدد صحيح وليس عشري

تم تعديل هذه المشاركة بواسطة إسماعيل ابراهيم في 22 مارس 2007 في 20:47

#3

وضعتهم و اصبح الكود هكذا لكن نفس النتيجة :( .. (بالكومبيلر Mingw/GCC و borland c++)

#include <stdio.h>

#define H 50
#define F 40
#define T 33
#define Q 25

int main(void)
{
	float			price, x;
	float		   newprice;
	char			code;

	printf("Please insert the price of the the article: ");
	scanf("%f", &price);
	printf("Please insert the pricing code: ");
	scanf("%c", &code);

	switch (code)
	{
		case 'h':
			{
				newprice=price*(H/100);
				x=H;
				break;
			}
		case 'f':
			{
				newprice=price*(F/100);
				x=F;
				break;
			}
		case 't':
			{
				newprice=price*(T/100);
				x=T;
				break;
			}
		case 'q':
			{
				newprice=price*(Q/100);
				x=Q;
				break;
			}
	}

	printf("\nThe original price was: %d", price);
	printf("\nThe new price is: %f", newprice);
	printf("\nThe amount of discount is: %d%%", x);



getch();
return 0;
}
#4

هناك مشكلة مع استخدام الدالة scanf فهي تبقي حرفا يتلقاه المتغير code لا اذكر بالضبط لكن ابحث عن مشاكل scanf

منذ زمن لم استعمل سي ونسيت المشكلة وحلها.

#5

وجدت الاخطاء (كما قال الأخ إسماعيل ابراهيم) وضعت fflush(stdin) قبل الscanf و وجدت انى وضعت %d للfloat

شكرا لكم اخوانى

والكود كاrefrence

#include <stdio.h>

#define H 0
#define F 25
#define T 30
#define Q 40

int main(void)
{
	float		   price, x, newprice;
	char			code;

	printf("Please insert the price of the the article: ");
	scanf("%f", &price);
	printf("Please insert the pricing code: ");
	fflush(stdin);
	scanf("%c", &code);

	switch (code)
	{
		case 'h':
			{
				newprice=price*H/100;
				x=H;
				break;
			}
		case 'f':
			{
				newprice=price*F/100;
				x=F;
				break;
			}
		case 't':
			{
				newprice=price*T/100;
				x=T;
				break;
			}
		case 'q':
			{
				newprice=price*Q/100;
				x=Q;
				break;
			}
	}

	printf("\nThe original price was: %f", price);
	printf("\nThe new price is: %f", newprice);
	printf("\nThe amount of discount is: %f%%", x);



getch();
return 0;
}

تم تعديل هذه المشاركة بواسطة Mistrio_xp في 22 مارس 2007 في 23:52

#6

ذكر الاخ اسماعيل الملاحظة التالية ..

اقتباس
d% تدل على عدد صحيح وليس عشري

ولكن لم تقم بالتعديل المناسب على الكود ..

بالتالي أعد كتابة آخر ثلاثة أسطر كالتالي

 printf("\nThe original price was: %f", price);
	printf("\nThe new price is: %f", newprice);
	printf("\nThe amount of discount is: %f \n", x);

أما مشكلة الصفر .. للمتغير newprice.. فحلها أن تقسم على عدد عشري 100.0 .. فالظاهر والله اعلم انه في السي ..

عدد صحيح على عدد صحيح يساوي عدد صحيح ..

بالتالي لو كتبت

H=50

H/100 ... الناتج هو صفر وليس نصف .

المشكلة الثالثة والتي ذكرها الاخ اسماعيل ايضا .. وهي بما يخص الscanf .. فلا اعرف ماهو السبب .. ولكن الحل اللي اعرفه

ان تستدعيها مرتين .. :P .

 printf("Please insert the price of the the article: ");
	scanf("%f", &price);
	printf("Please insert the pricing code: ");
	scanf("%c", &code);
	scanf("%c", &code);

	switch (code)
	{
		case 'h':
			{
				newprice= price*(H/100.0);
				x=H;
				break;
			}
		case 'f':
			{
				newprice=price*(F/100.0);
				x=F;
				break;
			}
		case 't':
			{
				newprice=price*(T/100.0);
				x=T;
				break;
			}
		case 'q':
			{
				newprice=price*(Q/100.0);
				x=Q;
				break;
			}
	 }

	printf("\nThe original price was: %f", price);
	printf("\nThe new price is: %f", newprice);
	printf("\nThe amount of discount is: %f \n", x);

logo1.png تطبيق طمأنينة ، نسخة بيتا على أندرويد

عبدالله الشمّري - Al-Shammari

CodingAlone.com

twitter @abshammeri

abshammeri AT gmail.com

github : abshammeri

#7
الشمري كتب:
أما مشكلة الصفر .. للمتغير newprice.. فحلها أن تقسم على عدد عشري 100.0 .. فالظاهر والله اعلم انه في السي ..

عدد صحيح على عدد صحيح يساوي عدد صحيح ..

بالتالي لو كتبت

H=50

H/100 ... الناتج هو صفر وليس نصف .

لم اعرف هذا.. شكراً جزيلاً للمعلومة :)

لكن انا مسحت الأقواس و نجحت

الشمري كتب:
المشكلة الثالثة والتي ذكرها الاخ اسماعيل ايضا .. وهي بما يخص الscanf .. فلا اعرف ماهو السبب .. ولكن الحل اللي اعرفه

ان تستدعيها مرتين .. :P .

او ان تضع fflush(stdin) قبلها :P

هذا الموضوع مغلق.

مواضيع مشابهة