将 strcmp 与字符串数组一起使用

Using strcmp with string arrays

我正在制作一个测验程序,但我遇到了字符串数组和函数 strcmp 的问题

我想我可能不得不使用指针,但我不知道将哪个指针指定为指针

#include <iostream>
#include <cstring>
#include <stdio.h>
#include <string.h>
#include <cmath>

using namespace std;
int main(){
char question1[5][1000]={"Question #1\n\nWhat is the formula for the area of a square?\n\nA.) Area = (Side)(Side)\nB.) Area = Side + Side\nC.) Area = Base + Height\nD.) Area = (pi)(Radius)(Radius)\nAnswer: ",
"Question #2\n\nWhat is the perimeter of a rectangle with length = 4 and width =8?\n\nA.) Perimeter = 4 + 8 = 12 units\nB.) Perimeter = (4)(8) = 32 units\nC.) Perimeter = 2(4) + 2(8) = 24 units\nD.) Perimeter = [2(4)][2(8)] = 128 units\nAnswer: ",
"Question #3\n\nWhat is the circumference of a Circle with Diameter = 10 units?\n\nA.) Circumference = (pi)(5)(5) = 78.5 units\nB.) Circumference = 2(pi)(5) = 31.41 units\nC.) Circumference = (pi)(10)(10) = 314 units\nD.) Circumference = 2(pi)(10) = 62.8 units\nAnswer:",
"Question #4\n\nWhat is the measurement for the side of a square with Perimeter = 80?\n\nA.) 20 units\nB.) 80 units\nC.) 40 units\nD.) 50 units\nAnswer: ",
"Question 5\n\nWhat is the radius of a circle with area = 64pi square units?\n\nA.) 2pi units\nB.) 4pi units\nC.) 6pi units\nD.) 8pi units\nAnswer: "};
char answer1[5][2]={"A","C","B","A","D"};
char answer;
int score,i;
for(i=1;i<5;i++){
cout<<question1[i];
cin>>answer;
if(strcmp(answer,answer1[i])==0)
{
    score++;
}
}
cout<<"Your score is "<<score;
}

answer 单个字符 而不是字符串。将它传递给需要字符串的函数时,不能将它用作字符串。对 strcmp 的调用应该会导致编译器对此发出警告。

直接比较字符,例如answer == answer1[i][0].

或者将类型改为字符串(最好是std::string,否则是char的数组)。


顺便提一下,您没有初始化变量 score。这意味着它的值将是 indeterminate 并且使用它(就像在 score++ 中一样)将导致 undefined behavior.

您需要显式初始化它:

int score = 0, i;

变量answer声明为单字节字符。

char answer;

不能存储字符串。

因此您不能将标准函数 strcmp 用于类型为 char 的对象。

你可以直接写

if ( answer == answer1[i][0] )

但最好将数组answer1声明为

const char answer1[] = { "ACBAD" };

const char *answer1 = { "ACBAD" };

并使用以下 if 语句

if ( answer == answer1[i] )

注意变量score没有初始化

int score,i;

你必须初始化它。而且循环中的索引应该从0开始。

int score = 0;
for( int i = 0;i < 5; i++ ){
//...

使用像 5 这样的幻数也是个坏主意。您可以引入一个命名常量。例如

const size_t N = 5;
char question1[N][1000] = { /*...*/ };
//...
for( size_t i = 0; i < N; i++ ){
//...

好吧,亲爱的OP,请允许我直言不讳。那不是 C++,那是带有 iostream 的 C 代码。此外,第一个问题从未打印出来,还发现了其他错误等。

None其中有一道题,如果你愿意学。 Here 是标准的 C++ 工作版本。请对照你的代码研究一下。

// clang++ prog.cc -Wall -Wextra -std=c++17
#include <iostream>
#include <array>
#include <string_view>

 using namespace std;
 // required for sv literal 
 using namespace std::literals;

 // compile time std array of string literals
 // each transformed to string_veiw's
 // by using the `sv` std defined literal
 constexpr array questions {
  "Question #1\n\nWhat is the formula for the area of a square?\n\nA.) Area = (Side)(Side)\nB.) Area = Side + Side\nC.) Area = Base + Height\nD.) Area = (pi)(Radius)(Radius)\nAnswer: "sv,
  "Question #2\n\nWhat is the perimeter of a rectangle with length = 4 and width =8?\n\nA.) Perimeter = 4 + 8 = 12 units\nB.) Perimeter = (4)(8) = 32 units\nC.) Perimeter = 2(4) + 2(8) = 24 units\nD.) Perimeter = [2(4)][2(8)] = 128 units\nAnswer: "sv,
  "Question #3\n\nWhat is the circumference of a Circle with Diameter = 10 units?\n\nA.) Circumference = (pi)(5)(5) = 78.5 units\nB.) Circumference = 2(pi)(5) = 31.41 units\nC.) Circumference = (pi)(10)(10) = 314 units\nD.) Circumference = 2(pi)(10) = 62.8 units\nAnswer:"sv,
  "Question #4\n\nWhat is the measurement for the side of a square with Perimeter = 80?\n\nA.) 20 units\nB.) 80 units\nC.) 40 units\nD.) 50 units\nAnswer: "sv,
  "Question #5\n\nWhat is the radius of a circle with area = 64pi square units?\n\nA.) 2pi units\nB.) 4pi units\nC.) 6pi units\nD.) 8pi units\nAnswer: "sv
   };

   // same compile time structure as questions
   // but for answers
   constexpr array answers {"A"sv,"C"sv,"B"sv,"A"sv,"D"sv};

  int main()
 {
   char answer {};
   int score{} ;
   // answer counter starts from 0
   size_t j = 0;
    // standard C++ 'sequence' loop
    for ( auto & question : questions ) {
      cout << question;
      cin >> answer;
         if( answer == answers.at[j][0] ) score++;
         j++ ;
    }
      cout <<"Your score is "<< score;
      return 42 ;
  }

有人甚至可能会说标准 C++ 很简单,看看这个。一个人必须使用它。

附录

更好的设计是在单一结构上创建和操作,将答案和问题放在一起。一些横向思维总是有帮助的。

// clang++ prog.cc -Wall -Wextra -std=c++17
#include <iostream>
#include <array>
#include <string_view>
#include <utility> // std::pair

using namespace std;
using namespace std::literals;

constexpr std::array a_q_pairs {
    pair{ 'A', "Question #1\n\nWhat is the formula for the area of a square?\n\nA.) Area = (Side)(Side)\nB.) Area = Side + Side\nC.) Area = Base + Height\nD.) Area = (pi)(Radius)(Radius)\nAnswer: "sv } ,
    pair{ 'C', "Question #2\n\nWhat is the perimeter of a rectangle with length = 4 and width =8?\n\nA.) Perimeter = 4 + 8 = 12 units\nB.) Perimeter = (4)(8) = 32 units\nC.) Perimeter = 2(4) + 2(8) = 24 units\nD.) Perimeter = [2(4)][2(8)] = 128 units\nAnswer: "sv},
    pair{ 'B', "Question #3\n\nWhat is the circumference of a Circle with Diameter = 10 units?\n\nA.) Circumference = (pi)(5)(5) = 78.5 units\nB.) Circumference = 2(pi)(5) = 31.41 units\nC.) Circumference = (pi)(10)(10) = 314 units\nD.) Circumference = 2(pi)(10) = 62.8 units\nAnswer:"sv},
    pair{ 'A', "Question #4\n\nWhat is the measurement for the side of a square with Perimeter = 80?\n\nA.) 20 units\nB.) 80 units\nC.) 40 units\nD.) 50 units\nAnswer: "sv},
    pair{ 'D', "Question 5\n\nWhat is the radius of a circle with area = 64pi square units?\n\nA.) 2pi units\nB.) 4pi units\nC.) 6pi units\nD.) 8pi units\nAnswer: "sv }
 };

int main()
{
 char user_answer {};
 int score{} ;

 // single loop
 // requires no change on adding/removing answers/questions pairs
 // no need to code index checking
 for ( auto & a_and_q : a_q_pairs ) 
 {
    auto & answer = a_and_q.first ; // char
    auto & question = a_and_q.second ; // string_view
    cout << question ;
    cin >> user_answer;
    if( user_answer == answer ) score++;
  }
   cout <<"Your score is "<< score;
   return 42 ;

}

精心设计的数据结构和在其上运行的简单算法。也许我会把那个 Niklaus Wirth book 擦掉?

必填项WandBox is here.