C 我如何在不打印“循环”的情况下运行此操作;“错误”;消息
所以我对编码非常陌生(几天前我开始学习c),我决定试着玩玩一下,看看我是否能应用到目前为止学到的东西。我创建了一个“员工搜索”程序,它会提示用户输入一个姓名,并检查该员工是否存在。我在循环中遇到了一个问题;如果我在终端中键入“Chris”并单击enter,它会显示类似“Employee not found.”“Chris found.”“Employee not found.”这样的信息。我如何让程序确认名称在“database”中,而不重复“error”消息。很抱歉问你这个新手问题。再说一次,我对这个很陌生C 我如何在不打印“循环”的情况下运行此操作;“错误”;消息,c,loops,break,cs50,C,Loops,Break,Cs50,所以我对编码非常陌生(几天前我开始学习c),我决定试着玩玩一下,看看我是否能应用到目前为止学到的东西。我创建了一个“员工搜索”程序,它会提示用户输入一个姓名,并检查该员工是否存在。我在循环中遇到了一个问题;如果我在终端中键入“Chris”并单击enter,它会显示类似“Employee not found.”“Chris found.”“Employee not found.”这样的信息。我如何让程序确认名称在“database”中,而不重复“error”消息。很抱歉问你这个新手问题。再说一次,
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
// declare array
string employee[] = {"Damien", "Chris", "Emma"};
// print intro message and prompt user for name
printf("Welcome to employee search\n");
printf("Please input an employee name: ");
string name = get_string();
// here is where I run into the issue where it'll repeat "employee not found"
for(int i = 0; i < 3; i++)
{
if(strcmp(name, employee[i])==0)
{
printf("%s found\n", name);
}
else
{
printf("Employee not found\n");
}
}
}
#包括
#包括
#包括
内部主(空)
{
//声明数组
字符串employee[]={“Damien”、“Chris”、“Emma”};
//打印介绍消息并提示用户输入名称
printf(“欢迎使用员工搜索”);
printf(“请输入员工姓名:”);
string name=get_string();
//这里是我遇到的问题,它将重复“找不到员工”
对于(int i=0;i<3;i++)
{
if(strcmp(姓名,员工[i])==0)
{
printf(“找到%s\n”,名称);
}
其他的
{
printf(“未找到员工”);
}
}
}
避免在循环内打印。而是使用一个标志来保存状态。比如:
int flag = 0; // Initialize flag to 0 (i.e. assume the name isn't found)
for(int i = 0; i < 3; i++)
{
if(strcmp(name, employee[i])==0)
{
flag = 1; // Set flag to 1 to remember that we had a match
break; // Stop the loop using break. We don't need to check the rest
// as we have found a hit
}
}
if (flag)
{
printf("%s found\n", name);
}
else
{
printf("Employee not found\n");
}
int标志=0;//将标志初始化为0(即,假定找不到名称)
对于(int i=0;i<3;i++)
{
if(strcmp(姓名,员工[i])==0)
{
flag=1;//将flag设置为1以记住我们有匹配项
break;//使用break停止循环。我们不需要检查其余的
//因为我们找到了一个热门话题
}
}
国际单项体育联合会(旗)
{
printf(“找到%s\n”,名称);
}
其他的
{
printf(“未找到员工”);
}
#包括
#包括
#包括
内部主(空)
{
字符串employee[]={“Damien”、“Chris”、“Emma”};
int i=0;
//打印介绍消息并提示用户输入名称
printf(“欢迎使用员工搜索”);
printf(“请输入员工姓名:”);
string name=get_string();//声明和初始化
对于(i=0;i<3;i++)
{
if(strcmp(姓名,员工[i])==0)
{
printf(“找到%s\n”,名称);
break;//如果发现任何员工,它会立即以i的值退出循环//是的,亲爱的,这有点容易
//首先获取任何变量中的输入
字符串名称=”;
//然后在循环中,将数组的每个索引检查为从用户处获得的字符串
coutname;
对于(int i=0;i)找到的是哪一行。"
正在做什么?这是C,即字符串是什么?
?也许可以在cs50.stackexchange.comBy上问这个问题,在循环中设置一个标志,然后报告。而不是在每次迭代中报告……甚至更好,你可能不需要询问就可以在cs50.stackexchange.com上轻松找到答案。这一切都取决于未知的get_string();
函数包含行终止字符。这也是一个很好的例子,说明是否(这显然发生在cs50.h
中)也许正确设置代码格式并添加解释问题被标记为c而不是c++此代码不提供问题的答案,如果它能工作,它将面临相同的问题。也许正确设置代码格式并添加解释添加解释,希望能有所帮助。yewi
超出了for
循环的范围。它在代码块中的t太多了。仍然进行了修改。
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string employee[] = {"Damien", "Chris", "Emma"};
int i = 0;
// print intro message and prompt user for name
printf("Welcome to employee search\n");
printf("Please input an employee name: ");
string name = get_string(); //Declaration and initialisation
for(i = 0; i < 3; i++)
{
if(strcmp(name, employee[i])==0)
{
printf("%s found\n", name);
break; // if any of the employee is found it exit the loop immediately with the value of i<3
}
}
if (i ==3 ) //means the loop has reached end without finding any of employee.
printf("Employee not found\n");
}
// yes dear it a bit easy
// first get the input in any variable
string name="";
// And then inside a loop chek every index of array to the string which you get from the user
cout<<" Enter name ";
cin>>name;
for(int i=0; i<=c.length;i++)
{ if(c[i]==name)
{
cout<<"found";
}
else{ cout<<"not found ";
}
}
// c just like c={"first name","secound_name","blablala"}