#include<iostream>
#include<time.h>
#include<string>
using namespace std;
int main()
{
int a;
string str = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t";
srand(time(NULL));
cout<<"enter a number";
cin>>a;
cout<<str[rand(a)%20]<<endl;
system("pause");
return 0;
}ويش الخطأ في هذا الكود؟؟ لا يقبل الترجمة
string str = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t";
string str = "abcdefghijklmnopqrst";
cout<<str[rand(a)%20]<<endl;
الدالة rand() لا تستقبل بارمتر
مثلما قال الأخ فهد.
إذا كنت تبحث على حرف عشوائي من قائمة العشرين حرف فيجب عليك حذف الفاصلة لأنها تحسب حرف. أي إجعلها هكذا
string str = "abcdefghijklmnopqrst";
الدالة rand() لا تستقبل أي بارامتر.
أتركها فارغة أي هكذا.
cout<<str[rand()%20]<<endl;
وإذا كنت غير متأكد من العشوائية فيمكنك إدخال الدالة wile للتأكد أي هكذا:
#include <iostream>
#include <time.h>
#include <string>
using namespace std;
int main()
{
int a;
string str = "abcdefghijklmnopqrst";
while(1)
{
cout<<"enter a number";
cin>>a;
cout<<str[rand()%20]<<endl;
}
system("pause");
return 0;
}تم تعديل هذه المشاركة بواسطة Mohamed Amine Tag في 2 أبريل 2010 في 14:46
ما شاء الله عليكم فعلا عباقرة الله يحييكم
