执行完所有if else代码后如何打印最后一个条件

发布于 2025-01-11 03:14:26 字数 630 浏览 2 评论 0原文

我想检查输入是否在字符串名称中,然后打印 给定的名称。如果给定的名称不在字符串名称中,则打印“您的名字不在这里”,但每次运行代码时我都会收到“您的名字不在这里”。

#include<stdio.h>
#include<cs50.h>
#include<string.h>

int main(void)
{
string userinput;
string names [] = {"david", "mark"};
string numbers[] = {"123456789","987654321"};
userinput = get_string("name: ");

for(int i = 0; i<2 ;i++)
{
    if(strcmp(names[i], userinput) == 0)
    {
        printf("your number is %s ", numbers[i]);
    }
}
printf("your name is not here\n");
}




 **ACTUAL OUTPUT**

 name: david
 your number is 123456789 your name is not here

I want to check the input if the input is in the string names then print the number of the
given name. If the given name is not in the string names then print "your name is not here" but I'm getting "your name is not here" every time I run my code.

#include<stdio.h>
#include<cs50.h>
#include<string.h>

int main(void)
{
string userinput;
string names [] = {"david", "mark"};
string numbers[] = {"123456789","987654321"};
userinput = get_string("name: ");

for(int i = 0; i<2 ;i++)
{
    if(strcmp(names[i], userinput) == 0)
    {
        printf("your number is %s ", numbers[i]);
    }
}
printf("your name is not here\n");
}




 **ACTUAL OUTPUT**

 name: david
 your number is 123456789 your name is not here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花落人断肠 2025-01-18 03:14:26

一个非常简单的解决方案是创建一个变量来跟踪名称是否已找到:

int found = 0;
for(int i = 0; i<2 ;i++)
{
    if(strcmp(names[i], userinput) == 0)
    {
        printf("your number is %s ", numbers[i]);
        found = 1;
        // Break so we don't have to iterate over the rest
        // of the items if we already found our name.
        break;
    }
}
if (!found) printf("your name is not here\n");

A very simple solution would be creating a variable to keep track if the name has been found:

int found = 0;
for(int i = 0; i<2 ;i++)
{
    if(strcmp(names[i], userinput) == 0)
    {
        printf("your number is %s ", numbers[i]);
        found = 1;
        // Break so we don't have to iterate over the rest
        // of the items if we already found our name.
        break;
    }
}
if (!found) printf("your name is not here\n");
空城之時有危險 2025-01-18 03:14:26

这自然是非常简单的代码,但是您应该尽早开始考虑程序设计。您的代码执行三件不同的、独立的事情:

  1. 提示输入/接受用户输入 (I/O)。
  2. 搜索输入是否与某个值匹配(搜索算法)。
  3. 呈现结果 (I/O)。

理想情况下,将第 2) 部分和第 3) 部分分开,以免将 I/O 与算法混淆,因此:

#include <stdbool.h>
...

bool found = false;
for(int i=0; i<2; i++)
{
  if(strcmp(names[i], userinput) == 0)
  {
    found = true;
    break;
  }
}

if(found)
{
  printf("your number is %s \n", numbers[i]);
}
else
{
  printf("your name is not here\n");
}

如果我们愿意,我们可以将这些部分拆分为单独的函数。例如,如果字符串数量扩大,我们将希望实现比这种“暴力”循环更好的搜索算法。

This is naturally very simple code, but you should already start to think about program design early on. Your code does three different, separate things:

  1. Prompt for input/take user input (I/O).
  2. Search if input matches a value (search algorithm).
  3. Present the results (I/O).

Ideally keep parts 2) and 3) separate not to mix up I/O with algorithms, so:

#include <stdbool.h>
...

bool found = false;
for(int i=0; i<2; i++)
{
  if(strcmp(names[i], userinput) == 0)
  {
    found = true;
    break;
  }
}

if(found)
{
  printf("your number is %s \n", numbers[i]);
}
else
{
  printf("your name is not here\n");
}

We could split up these parts in separate functions if we wish. For example if the amount of strings is expanded, we'll want to implement a better search algorithm than this "brute force" loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文