//
//
//
#include
#include // Header file needed to use srand and rand
#include // Header file needed to use time
using namespace std;
int a=0,b=0,c=0;
a = rand() % 6;
b = rand() % 6;
c = rand() % 6;
int main()
{
if(b==a){
while(b!=a) b = rand() % 6;
}
if(c==a||c==b){
while(c!=a&&c!=b) c = rand() % 6;
}
char x[5]={'纥','风Vanhogh','阿七','ME','鸽子'}
for(int i=1;i++;i<6){
if(x[i]==x[a]) printf("无偿摇人结果%c\n",x[a]);
if(x[i]==x[b]) printf("点图摇人结果%c\n",x[b]);
if(x[i]==x[c]) printf("出设摇人结果%c\n",x[c]);
}
return 0;
}
[Warning] character constant too long for its type
[Error] 'a' does not name a type
[Error] 'b' does not name a type
[Error] 'c' does not name a type
char x[5]={'纥','风Vanhogh','阿七','ME','鸽子'}只有5个元素
但你下面的循环不对
求余6得到的数值范围是0-5,而x数组的下标范围是0-4啊。如果x只有5个元素,那求余6都要改为求余5才行
另外最后的for循环写错了
for(int i=1;i++;i<6)
改为
for(int i=0;i<5;i++)
另外字符串要用双引号
char a[5]要改为char *a[5]
#include <iostream>
#include <math.h>
#include <time.h>
using namespace std;
#define N 5
int main()
{
srand(time(0));
int a=0,b=0,c=0;
a = rand() % N;
b = rand() % N;
c = rand() % N;
if(b==a){
while(b!=a) b = rand() % N;
}
if(c==a||c==b){
while(c!=a&&c!=b) c = rand() % N;
}
char* x[N]={"纥","风Vanhogh","阿七","ME","鸽子"};
for(int i=0;i<5;i++){
if(x[i]==x[a]) printf("无偿摇人结果%s\n",x[a]);
if(x[i]==x[b]) printf("点图摇人结果%s\n",x[b]);
if(x[i]==x[c]) printf("出设摇人结果%s\n",x[c]);
}
return 0;
}
改一下试试:
首先个数不对,rand()%6产生0到5共六个数的随机数;
然后你char数组内元素只能放单个字符的,如果要放字符串需要改数据格式;
你第22行缺少分号;
再就是你的for循环条件语句有问题。
char x[6]={'a','b','c','d','e','f'};
for(int i=0;i<6;i++){
if(x[i]==x[a]) printf("无偿摇人结果%c\n",x[a]);
if(x[i]==x[b]) printf("点图摇人结果%c\n",x[b]);
if(x[i]==x[c]) printf("出设摇人结果%c\n",x[c]);
}
```c
```
在main函数的开头部分调用srand函数来初始化随机数种子,如此便能让每次运行的随机数不同
修改如下,供参考:
#include <iostream>
#include <cstdlib> // Header file needed to use srand and rand
#include <ctime> // Header file needed to use time
using namespace std;
int main()
{
int a = 0, b = 0, c = 0;
srand((unsigned int)time(NULL)); //修改
a = rand() % 5; b = rand() % 5; c = rand() % 5;
//if (b == a) {
while (b == a) b = rand() % 5; //修改
//}
//if (c == a || c == b) {
while (c == a || c == b) c = rand() % 5; //修改
//}
char* x[5] = { "纥","风Vanhogh","阿七","ME","鸽子" }; //; 缺分号 字符串 修改
for (int i = 0; i < 5; i++) { //for(int i = 1; i++; i < 6) 修改
if (x[i] == x[a]) printf("无偿摇人结果 %s\n", x[a]);
if (x[i] == x[b]) printf("点图摇人结果 %s\n", x[b]);
if (x[i] == x[c]) printf("出设摇人结果 %s\n", x[c]);
}
return 0;
}
改成这样试试
#include<bits/stdc++.h>
using namespace std;
int main(){
int a=0,b=0,c=0;
srand(time(NULL));
a=rand()%5;b=rand()%5;c=rand()%5;
while(true){
if(a!=b)
break;
b=rand()%5;
}
while(true){
if(a!=b&&a!=c&&b!=c)
break;
c=rand()%5;
}
string x[5]={"纥","风Vanhogh","阿七","ME","鸽子"},k;
for(int i=0;i<5;i++){
k=x[i];
if(x[i]==x[a])cout<<"无偿摇人结果 "<<k<<"\n";
if(x[i]==x[b])cout<<"点图摇人结果 "<<k<<"\n";
if(x[i]==x[c])cout<<"出设摇人结果 "<<k<<"\n";
}
return 0;
}