atoi(char) problem - C2664

This is a discussion on atoi(char) problem - C2664 within the C++ Programming forums, part of the General Programming Boards category; Hi all, (code pasted below and attached in .cpp file) This should be any easy/quick one for the veterans. I'm ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    121

    atoi(char) problem - C2664

    Hi all, (code pasted below and attached in .cpp file)

    This should be any easy/quick one for the veterans. I'm designing a program for a Data Structs class, and the problem that is impeding my progress at this point, to keep it short and sweet, is this:

    I have a user input an array that I declare:

    char response[255];

    then I pass one of the characters of the array to a function in this manner:

    l.addTextToLine(response[2]);

    and when I catch it in the function, I do so in this manner:

    linkedList::addTextToLine(char p)
    {
    atoi(p);
    ....................
    }

    No need to code further into the function, because atoi(p) is where I get the error. Source1.cpp(170) : error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
    Did I make the mistake of passing it wrong, or what? I have the #include<string> and #include<cstring> headers included. I have also tried atoi(LPCTSTR(p)); _ttoi(p); GetBuffer(0), everything I could find on the forums. Thanks, guys/gals!
    __________________________________________________ _____________
    Code:
    #include <iostream>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    using namespace std;
    
    class linkedListNode
    {
          public:
                 char value[255];
                 linkedListNode *nextPtr;
                 linkedListNode(char *val, linkedListNode *nPtr = 0)
                 {
                                     strcpy(value, val); nextPtr = nPtr;
                 }
    };
    
    class linkedList
     {
          public:
                 linkedList()
                 {
                             headPtr = tailPtr = 0;
                 }
                 ~linkedList();
                 bool isEmpty()
                 {
                      return headPtr == 0;
                 }
    			 void addSomewhere(int);
                 void addToHead();
                 void addToTail();
    			 void displayList();
                 void deleteFromHead();
                 void deleteNode(int);
    			 void deleteLines(int, int);
    			 void addTextToLine(char);
                 void deleteFromTail();
         private:
                 linkedListNode *headPtr, *tailPtr;
    }; 
                             
    
    linkedList::~linkedList()
    {
        for(linkedListNode *p; !isEmpty(); )
        {
                           p = headPtr->nextPtr;
                           delete headPtr;
                           headPtr = p;
        }
    }
    
    void linkedList::addToHead()
    {
    	char headTxt[255];
    	cout << "Please enter the text to be inserted into the first line: " << endl;
    	cin.getline(headTxt, 255);
         headPtr = new linkedListNode(headTxt, headPtr);
         if(tailPtr == 0)
            tailPtr = headPtr;
    }
    
    
    void linkedList::addToTail() 
      {  
         linkedListNode *temp, *nextTemp;   
         char input[255]; 
         cout << "Please enter the line of text to be added: ";
         cin.getline(input,255);
    	 cin.ignore();
    	 temp = new linkedListNode(input);
         temp->nextPtr = NULL;
    
         if (headPtr == NULL) 
            { 
               headPtr = temp;
            }
         else 
           {
               nextTemp = headPtr;
               while (nextTemp->nextPtr != NULL)
               {  
                     nextTemp = nextTemp->nextPtr; 
               }
               nextTemp->nextPtr = temp;
           }
      }
    
    void linkedList::addSomewhere(int num)
    {
    	char newLineText[255];
    	cout << "OK, enter the text to be added to line " << num << endl;
    	cin.getline(newLineText, 255);
    	cin.ignore();
         int i;
         linkedListNode *tmp, *secondTmp;
    	 strcpy(tmp->value, newLineText);
    
    	 if(num == 1)
    	 {
    		 headPtr = tailPtr = tmp;
    	 }
    	 else 
    	 {
             for(i = 1; i < num; i++) 
             {
               tmp = tmp->nextPtr;
             }
    	 }
    	 secondTmp->nextPtr = tmp->nextPtr->nextPtr;
         tmp->nextPtr = secondTmp;
         //Now how the heck to insert a node?????
    }
    
    void linkedList::deleteFromHead()
    {
         char *p = headPtr->value;
         linkedListNode *tmp = headPtr;
         if(headPtr == tailPtr) //if only one node in the list;
             headPtr = tailPtr = 0;
         else headPtr = headPtr->nextPtr;
         delete tmp;
    }
    
    void linkedList::deleteFromTail()
    {
         char *p = tailPtr->value;
         if(headPtr == tailPtr) //if only one in list;
         {
                    delete headPtr;
                    headPtr = tailPtr = 0;
         }
         else
         {
             linkedListNode *tmp;
             for(tmp = headPtr; tmp->nextPtr != tailPtr; tmp = tmp->nextPtr);
             delete tailPtr;
             tailPtr = tmp;
             tailPtr->nextPtr = 0;
         }
    }
    
    void linkedList::deleteNode(int num)
    {
         linkedListNode *pred, *tmp;
         int i;
         for(i = 1; i < num; i++)
         {
               pred = pred->nextPtr;
               tmp = tmp->nextPtr;
         }
         if(tmp != 0)
         {
                pred->nextPtr = tmp->nextPtr;
                if(tmp == tailPtr)
                tailPtr = pred;
                delete tmp;
         }
    }
    
    void linkedList::addTextToLine(char p)
    {
         int i;
    	 char appTxt[255];
    	 cout << "Enter the text to be appended to line " << p << endl;
    	 cin.getline(appTxt, 255);
    	 cin.ignore();
          linkedListNode *tmp;
         for(i = 1; i <= atoi(p); i++)
         {
               tmp = tmp->nextPtr;
         }
         //strcat(tmp.value, *p); //this to access the current node.value???
    }
    
    void linkedList::displayList()
    {
    	linkedListNode *temp;
     int line = 1;
         //Loop to display the content sof the list as long as the list has data
             while (temp != NULL)
             {  
                  // Display details for what temp points to
                  cout << line << "> " << temp->nextPtr << " ";
                  line++;
                  cout << endl; // Line break
    	          temp = temp->nextPtr;
             }
    	     cout << "This marks the end of the list" << endl;
    }
    int main()
    {
    	linkedList l;
    	char response[255];
    	char input[255];
        do
    	{
    	  l.displayList();
    	  cout << endl;
    	  cout << "Here are your options : " << endl;
    	  cout << "E - Exit program." << endl;
    	  cout << "I+space+number - Insert text before that line number." << endl;
    	  cout << "I - Insert text before current line (without number)." << endl;
    	  cout << "D+space+number(n)+space+number(m) - Delete lines n through m." << endl;
    	  cout << "D+space+number - Delete line number entered." << endl;
    	  cout << "D - Delete current line." << endl;
    	  cout << "A+space+number - Append text to that line number." << endl;
    	  cout << endl << " >> ";
    	  cin >> response;
    	  cin.ignore();
    
    	  if(toupper(response[0]) == 'E')
    	  {
    		  exit(1);
    	  }
    	  else if(toupper(response[0]) == 'I' && !isalpha(response[2]))
    	  {
    		  //insert before current line - implement this function!!!!!!!!!!!
    	  }
    	  else if(toupper(response[0]) == 'I' && isalpha(response[2]) == 1)
    	  {
    		  l.addToHead();
    	  }
    	  else if(toupper(response[0]) == 'I' && isalpha(response[2]))
    	  {
    		  l.addSomewhere(response[2]);
    	  }
    	  else if(toupper(response[0]) == 'D' && isalpha(response[4]))
    	  {
    		  l.deleteLines(response[2], response[4]); //Implement this function!!!!!!!!!!!!!!!
    	  }
    	  else if(toupper(response[0]) == 'D' && (response[2]) == '1')
    	  {
    		  l.deleteFromHead();
    	  }
    	  else if(toupper(response[0]) == 'D' && isalpha(response[2]) && !isalpha(response[4]))
    	  {
    		  l.deleteNode(response[2]);
    	  }
    	  else if(toupper(response[0] == 'A'))
    	  {
    		  l.addTextToLine(response[2]);
    	  }
    	  else
    	  {
    		  cout << "Sorry, that was incorrect input.  Please follow the instructions " << endl;
    		  cout << " below carefully!" << endl;
    	  }
    	}
         while (response != 0);
    }
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    The problem is exactly what the error says. The atoi function cannot convert a char to a const char*. A C style string is a character array, which is passed to the atoi function as a const char*. A single character won't work.

    If you want to convert a single character to its integer equivalent, you would use p - '0' which subtracts the zero character and works because the digits '0' through '9' are all in order.

    This won't work for you, though. If you type A+space+number, you will only read in the A because operator>> stops at whitespace. Also, you want a full number, not a one digit number, so you wouldn't want to pass a single char. What you probably want to do is check for 'A', and then read from cin again into an int variable, then pass that to the function as the line number.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

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