Click here to Skip to main content
15,914,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<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;
}

:((
Posted
Updated 8-Feb-11 21:01pm
v4
Comments
Sergey Alexandrovich Kryukov 9-Feb-11 1:59am    
Does not look like a question. Is it a Tip?
--SA
Sandeep Mewara 9-Feb-11 2:01am    
Can you elaborate your question? Use 'Improve question' link to edit it.
Niklas L 9-Feb-11 4:32am    
strcmp() returns 0 if the strings are identical. You will need "if (strcmp(a, b) == 0)".
Peter_in_2780 9-Feb-11 20:33pm    
PLEASE don't change the whole substance of your question after you get answer(s). There is nothing now in your question that relates to what I answered yesterday.

If what you are trying to do is this:
switch(p) {
case "abcd":
 ...
case "pqr":
 ...
}

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
 
Share this answer
 
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{
    ;
}
 
Share this answer
 
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);
	}
}


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);
	}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900