Thread: Read from Console

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    Read from Console

    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

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Have you tried the code? Does it work?

    Jim

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I'm developing a c++ code which reads a line from the console like this:
    So why is all the code you've written in C then?

    And no, your code will not work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 06:45 AM
  2. Problems with a simple console game
    By DZeek in forum C++ Programming
    Replies: 9
    Last Post: 03-06-2005, 02:02 PM
  3. Console Functions Help
    By Artist_of_dream in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 03:44 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM