| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Nodetype
#include<iostream>
using namespace std; struct nodetype { string name; int ballot; nodetype *link; // pointer to the next node }; #include "linked.h" #include<string> #include<iomanip> int main() { nodetype *first, *last, *newNode; string lname, option; int ballots; first=NULL; last=NULL; int count=0; int sum=0; do{ cout<<"Enter name of canidate: "; cin>>lname; cout<<"Enter number of ballots: "; cin>>ballots; newNode=new nodetype; newNode->name=lname; //place lname in name newNode->ballot=ballots; //place ballots in ballot newNode->link=NULL; //make sure link is empty if(first==NULL) { first=newNode; last=newNode; } else { last->link=newNode;//insert the newNode in the previous link field last=newNode; //points to the last node in the list } count++; sum=sum+ballots; cout<<"Type '.' to finish or 'continue' to continue: "<<endl; cin>>option; }while(option !="."); cout<<endl; cout<<sum<<endl; cout<<"There are "<<count<<" "<<"canidates"<<endl; do { if (newNode == NULL) cout << "End of list" << endl; else { // Display details for what newNode points to cout << "Name : " << newNode->name << endl; cout << "Ballots recieved : " << newNode->ballot << endl; cout << "Percentage of ballots : "<<fixed<<setprecision(2)<<(ballots/sum)*100<<endl; cout << endl; // Move to next node (if present) newNode = newNode->link; } } while (newNode != NULL); int t; cin>>t; return 0; } .. ok i got the loop to work, but the main problem now is its only outputting the most recent node instead of outputting all of the listed any suggestions on how to dispaly ALL the nodes? or am i even making a linked list and not just overwriting info? |
|
#2
|
|||
|
|||
|
you are starting with newNode being the most recent node in the list. before your output loop, set newNode to first.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Nodetype |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|