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);
}