该程序有问题,我不知道为什么????(我认为这是针对cmp的) [英] this program has a problem and i dont know why ????(i think it is for cmp)
问题描述
<pre lang="xml">// 2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char str[]="input";
string p,s;
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
int code;
while (pch != NULL)
{
printf("%s\n",pch);
pch = strtok (NULL, " ,.-");
if(strcmp(pch,"input")){
pch = strtok (NULL, " ,.-");
s=pch;
cout<<"enter:";
cin>>s;
}
}
getch();
return 0;
}
:((<
:((
推荐答案
如果您要这样做的是:
If what you are trying to do is this:
switch(p) {
case "abcd":
...
case "pqr":
...
}
简短的答案是你做不到.要引用该标准,表达式必须是整数类型或存在明确转换为整数类型的类类型".字符串(或char *或char [])不是整数类型,不能明确地转换为一个.
如果愿意,请投票并接受.
彼得
the short answer is that you can''t do it. To quote the standard, "The expression must be of integral type or of a class type for which an unambiguous conversion to integral type exists." A string (or char * or char[]) is not an integral type and cannot be unambiguously converted to one.
If you like, vote and accept.
Peter
Peter_in_2780是正确的,不能那样做.您可以使用此:
Peter_in_2780 is right, it can''t be done like that. You could use this:
if(!strcmp(p,"abcd")){
;
}
else if(!strcmp(p,"hello")){
;
}
else if(!strcmp(p,"world")){
;
}
else{
;
}
正如Thaddeus所建议的,您可以编写一个很大的if/else if行,或者在某些情况下,使用带回调的for循环会是一个更好的选择.
As Thaddeus suggested, you can write a huge if/else if line, or in some cases a for loop with callbacks would be a better choice.
typedef enum {
SCN_ABCD, //"abcd"
SCN_HELLO, //"hello"
SCN_WORLD, //"world"
} StringChoiceNumbers;
typedef struct {
StringChoiceNumbers eCase;
LPCSTR szCase;
} StringChoice;
StringChoice scOptions[] = {
{ SCN_ABCD, "abcd" },
{ SCN_HELLO, "hello" },
{ SCN_WORLD, "world" },
};
void ProcessStringOption(StringChoiceNumbers eOption) {
switch (eOption) {
case SCN_ABCD:
//case "abcd"
break;
case SCN_HELLO:
//case "hello"
break;
case SCN_WORLD:
//case "world"
break;
}
}
//in some function that needs a switch(p) on strings
for (int i = 0; i < ARRAYSIZE(scOptions); ++i) {
if (strcmp(scOptions[i].szCase, p) == 0) {
ProcessStringOption(scOptions[i].eCase);
}
}
从处理的角度来看,它速度较慢,但是可以提高代码的局部性.
如果您需要调用函数中的大量变量,则可能无法使用它.
也可以不使用任何enum
位来完成此操作,而使用LPSTR数组而不是创建结构,只需为每个字符串分配一个纯整数就可以简化它.
From a processing point of view it is slower, however it improves code locality.
You may no be able to use this if you need a large number of variables from the calling function.
This can also be done without any of the enum
bit and use an array of LPSTR instead of creating a structs, and just assign a plain integer to each string to simplify it a bit.
LPCSTR szOptions[] = {
"abcd",
"hello",
"world",
};
void ProcessStringOption(DWORD nOption) {
switch (nOption) {
case 0:
//case "abcd"
break;
case 1:
//case "hello"
break;
case 2:
//case "world"
break;
}
}
//in some function that needs a switch(p) on strings
for (int i = 0; i < ARRAYSIZE(szOptions); ++i) {
if (strcmp(scOptions[i], p) == 0) {
ProcessStringOption(i);
}
}
这篇关于该程序有问题,我不知道为什么????(我认为这是针对cmp的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!