【问题标题】:University Assignment And I'm out of logics大学作业我没有逻辑
【发布时间】:2017-04-17 23:11:54
【问题描述】:

我已经尝试了所有方法,但代码不起作用,而且由于我是这个领域的新手,所以我没有逻辑

Here's the assignment description

这里是代码:

#include <iostream>
#include <string>

int main()
{
   char* Array2D[3][2] = {{"Bushra","0000-555555"},{"Ahad","0000-555544"},{"Mehwish","042-5558585"}};

   char* name;
   std::cout << "Enter name to find number in the directory"; 
   std::cout << "\n" << "\n";
   std::cin >> name;

   for(int i = 0; i < 3; ++i)
    {
        for(int j = 0; j < 2; ++j)
        {
            std::cout<< "Array[" << i << "][" << j << "] = " << Array2D[i][j] << "\n";
            std::cout<< "\n";

         if(name == Array2D[i])
{
        std::cout << Array2D[j];
}
else
{
std::cout << "NO RECORD";
}

        }
    }

}

【问题讨论】:

  • 邮政编码请作为编码。
  • 表示你今天参加 Stack Overflow 的导览时没有注意。
  • 摘自help page请求家庭作业帮助的问题必须包括您迄今为止为解决问题所做的工作摘要,以及您遇到的困难的描述解决它。
  • 如何做一个非常糟糕的帖子的好例子!
  • 请澄清“不起作用”。使用了哪些输入?预期输出与实际输入有何不同?

标签: c++ multidimensional-array


【解决方案1】:

Source in IDEOne

现在修改为使用二维数组。
这就是我在 C++ 中使用现代编译器的方式。
你的任务可能不同。

#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using std::string;
using std::vector;
using std::cout;
using std::cin;

int main(void) {

  string phoneBook[3][2] = { { "Jenni", "867-5309"  },
                             { "Police", "911-911"  },
                             { "Operator", "123456" } };

   string searchName;

   cout << "Enter Name to find a number" << std::endl;
   cin >> searchName;

   auto t = std::find_if(phoneBook, phoneBook+3, [&](auto& r){return r[0] == searchName;} );
   cout << ((t == phoneBook+3)? "No Such Name Found" : (*t)[1]);

    return 0;
}

【讨论】:

  • 我必须通过二维数组sir。
  • 'T'没有命名类型,另一个错误是'T'没有在这个范围内声明。
  • 我证明它可以在 IDEOne 网站上运行。如果它不适合您,则说明您使用的是较旧的编译器。
【解决方案2】:

您不能将 operator== 与 C 样式字符串一起使用。
您必须使用strcmp()

如果您改用std::string,则可以使用operator==

编辑 1
问题是== 运算符比较的是指针,而不是指针所指向的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-30
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多