Help with input handling.

This is a discussion on Help with input handling. within the C++ Programming forums, part of the General Programming Boards category; For my final, I have to do an organizational chart using linked lists. Users have to be able to add ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    28

    Help with input handling.

    For my final, I have to do an organizational chart using linked lists. Users have to be able to add people to the list, delete people, change information, blah blah blah. The problem is the way the user enters his information. Commands are of the format (command letter)(number), or for example, "insert at line three" looks like "I3".

    Right now, I am using strings to store input, and doing a switch statement based on the first character. I am then passing the second character to the relevent function.

    My switch statement:
    Code:
    void handleinput(string input)
    {
    	char command=0;
    	
    	command = input[0];
    	switch (command)
    	{
    	case 'I':
    		chart.insert(input[1] - '0');  //only works for single digits
    		break;
    	case 'C':
    		chart.change(input[1] - '0');
    		break;
    	case 'D':
    		chart.del(input[1] - '0');
    		break;
    	case 'L':
    		chart.List(cout,0);        
    		break;
    	case 'X':
    		chart.cut(input[1]-'0');
    		break;
    	case 'E':
    		chart.eliminate(input[1]-'0');
    		break;
    	case 'N':
    		chart.New();
    		break;
    	case '?':
    		chart.help();
    		break;
    	default:
    		return;
    	}
    }
    As you can see from my comment, there is a problem: this method only works for numbers 1-9. C23 would currently be the same as C2, because I'm only looking at the second character.

    I don't know how to tokenize these strings, if it's even possible. There is NO space or other character between command and number.

    If you know of a more elegant way to go about this, please help me.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Try stepping through the string until you reach a digit. Read everything from that digit on into a new string and atoi it for working with.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    28
    I thought of that, but I'm unsure as to how to check if something is a digit. Ascii values?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Include <cctype> and use the isdigit function.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    28
    Thank you kindly.

    EDIT: Thanks for the help, it's running a lot better than before. I do have an additional problem now though, and it's probably a beginner's error, but I can't see it. When the number becomes two digit now, it adds the person at the end, but it runs through the loop an additional time and adds a person at position = 0 (top of the chart).

    My loop:
    Code:
    char command=0;
    	command = input[0];
    	char numbers[40];
    	int counter = 0;
    
    	switch (command)
    	{
    case 'I':
    		for (counter = 1; counter < input.length(); counter++)
    		{
    			if ( isdigit(input[counter]) )
    			{
    				input.copy(numbers,input.length() - counter, counter);
    				cout<<"atoi(numbers) = "<< atoi(numbers)<<endl;
    				chart.insert( atoi(numbers) );
    			}
    		}
    		break;
    Last edited by Will; 05-08-2002 at 11:47 PM.

  6. #6
    Unregistered
    Guest
    char input[9] = "I413";
    char command = input[0];
    cout << command << endl;
    char sNum[8];
    for(int i = 0; i < strlen(input); i++)
    {
    sNum[i] = input[i + 1];
    }
    int num = atoi(sNum);
    cout << num;

    if num could be > 32500 (or something like that) then need atol90 rather than atoi().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  2. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  3. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 11:19 PM
  4. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM
  5. Handling input from cin
    By Zarkon in forum C++ Programming
    Replies: 1
    Last Post: 12-17-2001, 08:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21