【发布时间】:2019-02-19 12:42:15
【问题描述】:
我正在尝试编写一个程序,如果某个小狗主人的小狗与小狗主人的距离超出了一定距离,它会提醒用户。
具体如下:
首先让用户输入主人的位置,即输入2个整数
a和b。然后要求用户输入主人拥有的小狗数量。这是一个正整数
n。对于 n 只小狗中的每一只小狗
i,程序要求用户输入小狗 i 的位置。这是2个整数x和y,当然这两个都依赖于i。如果计算出小狗 i 与其主人的距离大于 10 个单位,则程序应通过打印 i 来通知用户。
最后,程序应该告诉用户已经打印了号码的小狗的总数。这个数字由变量
count表示,一个正整数。
以下是一个例子
输入: (业主所在地) 2 1 (数量小狗)4 (定位小狗)(15 15),(14 -2),(1 3),(0 4)
输出: 小狗 1 和小狗 2 太远了 总共 2 只小狗离得太远了
当我尝试运行程序时,程序输出一个问号而不是i's。请问我做错了什么?
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main() {
string puppies;
int a,b;
cin>>a>>b;
int n;
cin>>n;
int i,x,y,count=0;
for (i=1;i<=n;i++){
cin>>x>>y;
int dist;
dist=abs(a-x)+abs(b-y);
if (dist>10){
count++;
puppies += i;
}
}
if (count==1){
cout<<"Puppy "<<puppies[0]<<" too far away"<<endl;
cout<<"Total "<< count <<" puppy too far away";
}
if (count>1){
int j;
for (j=0;j<=(count-2);j++){
cout<<"Puppy "<<puppies[j]<<" and"<<" ";
}
cout<<"Puppy "<<puppies[count-1]<<" too far away"<<endl;
cout<<"Total "<< count <<" puppies too far away";
}
if (count==0){
cout<<"No puppies too far away";
}
}
这是复制的输出(与上面相同的情况)
小狗和小狗太远了 共有 2 只小狗离得太远按任意键继续。 . .
这是截图
【问题讨论】:
-
program outputs a question mark instead of the instead of the i's它只输出一个问号?能否请您发布确切的输出。 -
@KamilCuk 已编辑。谢谢
-
@Kira 杰出。杰出。