Hello all
I'm developing a c++ code which reads a line from the console like this:
a=18
b=TRUE
c=Hello
-I want to check the first character.
-If it's "a" then i'll put the integer value after the equal on a variable that is an argument of the function. I want to use this variable outside the function (not a global variable)
-If it's a "b" then it's boolean
-If it's a "c" then its a string.
Console example:
a=34
Call my function (maybe something like this):
int &i;
bool b;
char s[50];
ReadConsoleLine(&a, &b, c);
The first character is an "a", therefore i'm going to change the variable "a" to 34.
I have this, code....
Will this work?
Any thoughts?
Code:
void ReadConsoleLine(int a, bool b, char *c)
{
char str [50];
char tp[5];
char * strtoken;
scanf ("%s",str);
strtoken = strtok (str,"=");
while (strtoken != NULL)
{
strtoken = strtok (NULL, "="); //gets the text after the equal
}
strncpy (tp,str,1); //gets the first character
if(strcmp(tp, "a") == 0)//integer
a = atoi(strtoken);
else
{
if(strcmp(tp, "b") == 0)//BOOL
b = (strcmp(strtoken, "False");
else
{
if(strcmp(tp, "c") == 0)//String
strcpy(c, strtoken);
}
}
}
Will this code work?
Thanks in advance