I am trying to create a program (very simple) to gather a users desired x points and y points, store them into a list, and out put them like (x,y).
But, I can only get one line of out put. On top of that, the 'x' value is always the same. The 'y' value is always the last value inputted. And for some reason I only get one output, when i should get n amounts (depending on how many values the user entered.)
#include <iostream>
#include <list>
#include <cassert>
#include <iterator>
usingnamespace std;
/////////////////////
//DEFINING THE NODE//
/////////////////////
struct newNode
{
int xInt;
int yInt;
newNode *next;
};
////////
//MAIN//
////////
int main()
{
newNode *origon;//first node
newNode *leader;//moves through nodes
int x = 0;//SET X TO 0 INCASE USER DOES NOT ENTER AN X VALUE;
int y = 0;//SET Y TO 0 INCASE USER DOES NOT ENTER AN Y VALUE;
cout << "Enter an x value (enter -999 to add your y values): " << endl;
cin >> x;
//////////////////////////////
//THIS IS FOR THE X VALUES //
//////////////////////////////
while (x != -999)//as long as the users input isn't -999, keep storing data
{
origon = new newNode;//create a new node in the link
origon->next=0; //set that link to 0 (or null)
origon->xInt = x; //set origion to make the first node = the user input(x)
//now the leader points to first node
if (leader != 0)
{
while(leader->next !=0)
{
leader = leader ->next;
}
}
leader ->next = new newNode;//creates a new node at the end of the list
leader = leader ->next; //moves to it (new node)
leader ->next = 0; //sets it to 0 (null)
cin >> x; //gets user data, if -999 moves onto y, else start over
}
cout << "" << endl;
cout << "Enter your y values. Press -999 to out put data." << endl;
cin >> y;
////////////////////////////
//THIS IS FOR THE Y VALUES//
////////////////////////////
while (y != -999)//as long as the users input isn't -999, keep storing data
{
origon = new newNode;//create a new node in the link
origon->next=0; //set that link to 0 (or null)
origon->yInt = y; //set origion to make the first node = the user input(y)
leader = origon; //now the leader points to first node
if (leader != 0)
{
while(leader->next !=0)
{
leader = leader ->next;
}
}
leader ->next = new newNode;//creates a new node at the end of the list
leader = leader ->next; //moves to it (new node)
leader ->next = 0; //sets it to 0 (null)
cin >> y; //gets user data, if -999 moves onto y, else start over
}
cout << "" << endl;
cout << "Your points: " << endl;
//////////
//OUTPUT//
//////////
leader = origon;
while (leader ->next != 0)
{
cout << "(" << leader ->xInt << "," << leader ->yInt << ")" << endl;
leader = leader->next;
}
//I did not allow the link to print the last piece of data because
//it contains the -999 values, which the user inputted to gather their
//respective data.
cin >> x;
}
Should I be using a for loop? Is my data being missed somewhere in my code? I am thoroughly confused (plus trying to learn this at breakneck speed in college.)
Leader isn't initially allocated. So when it gets to line 46 on the first iteration is just writes to where-ever leader is pointing too. Which is undefined memory.
My Assignment is this- (NOTE I dont want it done for me, I need to learn)
Write a program that creates a linked list of points. For this assignment we define a point as a set of integers, X and Y. This linked list should insert the points ordered by the x value. The user is prompted to enter as many points as they desire. Each point is a new node in the linked list. After the user has entered all the points they wish to enter, the program is to output the linked list data (i.e. X and Y integers) as it appears in the linked list.
What do you mean the STL has linked-list in it? How does this work?
Ah, so I can use iterators. This will make my code much cleaner, and easier to manage. I started over, but managed to get lost in my do..whiles and if...else statements. I am glad to know their is an easier way to stroe infromation.
I rewrote my code, but now the code only reads the last entry for x and y, and if I have more than one entry for x and y i get an infinite loop. Maybe you could see whats wrong here?
#include <iostream>
#include <list>
usingnamespace std;
//define my list
struct nodeType
{
int xInt;
int yInt;
nodeType *next;//points to the next node
};
nodeType *startPoint = NULL;
nodeType *temp;
nodeType *temp2;
int main()
{
temp = new nodeType;
cout << "Enter as many points as you like (x,y): " << endl;
////////////
//X VALUES//
////////////
cout << "Please enter an x value (enter 999 to move onto y): ";
cin >> temp->xInt;//user input for x, sotre it in xInt list
while (temp->xInt != 999)
{
temp->next = NULL;
if (startPoint == NULL)
{
startPoint = temp;
}
else
{
temp2 = startPoint;
while (temp2->next != NULL)
{
temp2 = temp2->next;
}
temp2->next = temp;
}
cout << "Please enter an x value (enter 999 to move onto y): ";
cin >> temp->xInt;
}
////////////
//Y VALUES//
////////////
cout << "" << endl;
cout << "Please enter an y value (enter 999 to start display): ";
cin >> temp->yInt;//user input for y, sotre it in yInt list
while (temp->yInt != 999)
{
while (temp->xInt != 999)
{
temp->next = NULL;
if (startPoint == NULL)
{
startPoint = temp;
}
else
{
temp2 = startPoint;
while (temp2->next != NULL)
{
temp2 = temp2->next;
}
temp2->next = temp;
}
}
cout << "Please enter an y value (enter 999 to start display): ";
cin >> temp->yInt;
}
//////////////
//PRINT LIST//
//////////////
temp = startPoint;
do
{
if (temp == NULL)
{
cout << "End of the List" << endl;
}
else
{
//how to display
cout << "(" << temp->xInt << "," << temp->yInt << ")" << endl;
temp = temp->next;
}
}
while (temp != NULL);
}
You should simplify your code.
Entering all of the X values, then all of the Y values can be problematic. Not to mention what if someone doesn't enter a proper numeric value?
Here is some code that shows a better way to read the input from the user. Converting it to a string properly and throwing an exception (which you should handle) if the value isn't a proper number.
I've also provided the pseudo-code for the linked-list component. You should be able to utilise your existing code the pseudo-code to get a solution.
#include <string>
#include <sstream>
#include <iostream>
usingnamespace std;
struct node {
int X;
int Y;
node *next;//points to the next node
};
// Convert from String to Int properly
int strToInt(string value) {
int iRet = 0;
stringstream myStream(value);
if (!(myStream >> iRet))
throw string("Invalid integer");
return iRet;
}
int main () {
string input = "";
node *nHead = 0;
node *nCurrent = 0;
node *nNew = 0;
while(true) {
// Get X value
cout << "Enter X value (q to quit): ";
getline(cin, input);
if (input == "q")
break;
int iX = strToInt(input);
// Get Y value
cout << "Enter Y value: ";
getline(cin, input);
int iY = strToInt(input);
// nNew = new node();
// nNew->next = null
// nNew->X and Y = iX and iY
// If nHead == 0 then nHead == nNew
// if nCurrent != 0 then nCurrent->next = nNew
// nCurrent = nNew
};
// Print
// node *pIterator = head
// while (pIterator != 0)
// print values
// pInterator = next
return 0;
}
Zaita thank you very much. I am finding that what is covered in my text book may be informative, practical coding however requires more structure. Thank you very much for the pseudo-code, this little bit actually got me on the right track, (now I am learning stacks, and I must say I like stacks, they are so easy to use!)
Ok, so i got everything working, but now, for some reason my '=' is not being overloaded (it is in my linkedList.h file which is included in main()) Maybe I am doing something wrong, but my book has this here word for word.
#include <iostream>
#include <list>
#include <cassert>
#include <ostream>
#include "linkedList.h"
usingnamespace std;
struct node
{
node *info;
node *link;
int x, y;
};
node* getXandY();
int main()
{
return 0;
}//end main
node* getXandY()
{
node *newNode, *first, *last;
int input;
int count = 0;
cout << "Enter as many points as you like: (x,y)" << endl;
first = NULL;
do
{
//x integers
cout << "Enter an X value: ";
cin >> input;//read info
int x = input; //store info (x) into input
newNode = new node();//allocate memory for the type node
assert(newNode != NULL);//terminate program is no memory space
newNode->info = input;//copy value into input
newNode->link = NULL;
if (first == NULL)
{
first = newNode;
last = newNode;
}
else
{
last->link = newNode;
last = newNode;
}
//y integers
cout << "Enter an Y value: ";
cin >> input;//get info
int y = input;//store in for (y) into input
newNode = new node;//allocate memory for the type node
assert(newNode != NULL);//terminate program is no memory space
newNode->info = input;//copy value into input
newNode->link = input;
if (first == NULL)
{
first = newNode;
last = newNode;
}//end if
else
{
last->link = newNode;
last = newNode;
}//end else
}//end do
while (input != 999);
for (int i = 0; i < count; i++)
{
cout << "(" << /*place code here*/ << "," << /*place code here */ << ")" <<endl;
}//end for
getchar();
}
You ask the user to input x and y values (of type integer) - and on
lines 44 and 67 you try to assign these values to the info member of a node.
The info member is a pointer to node (node *info) - maybe you mean the x and y members??????
Well, it only fixed one error, I meant to change that but forgto, but the error is coming from this line of code:
newNode->link = NULL;
and the error is
\main.cpp(68) : error C2440: '=' : cannot convert from 'int' to 'node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
found i was trying to re state how much input was. Stupid error.
But line 68 does not say newNode->link = NULL it says newNode->link = input; because you are trying to assign the value the user has just entered to the link pointer member - hence the error.