C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 18th, 2006, 12:06 PM
temptgan temptgan is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 1 temptgan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 26 sec
Reputation Power: 0
Need help with my code

hi there
can anyone help me with my code? it can be compiled successfully in vc++6.0, however it fails to run.

here's the code:

//set.h

#include <iostream>
using namespace std;

struct Node{
char element;
Node* next;
};

class Set {
public:
Set();
Set(char start, char end);
void printSet();

private:
Node* head;
};

//set.cpp
#include <iostream>
#include "set.h"

Set::Set() {
head = NULL;
}

Set::Set(char start, char end) {
char temp = start;
Node* ptemp = new Node;
ptemp->element = temp;
ptemp->next = NULL;
head = ptemp;
temp++;
while (temp != end){
ptemp = ptemp->next;
ptemp->element = temp;
temp++;
}
}

void Set::printSet(){
cout << "[";

if (head != NULL){
Node* pprint = head;
cout << pprint->element;
pprint = pprint->next;
while (pprint != NULL) {
cout << ',' << pprint->element;
pprint = pprint->next;
}
}
cout << "]";
}

//main.cpp
#include "set.h"
using namespace std;

void main() {
Set B('A', 'Z');
B.printSet();
}



//basically i want to store A~Z in a linked list and print it out, thanks

Reply With Quote
  #2  
Old February 18th, 2006, 03:29 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
One thing I can see is to not to inclde header file iostream.h again in your set.cpp file since it will be called automatiacally by set.h.

See def. of "set.h" . Either remove iostream.h from set.cpp or remove its declaration in the set.h

Reply With Quote
  #3  
Old February 19th, 2006, 08:27 AM
Icon's Avatar
Icon Icon is online now
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 739 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 12 h 30 m 32 sec
Reputation Power: 4
Including iostream multiple times will not hurt your program, it will only be included once.

In your constructor you try to create a list from start to end right? (consider the class list from the stl library by the way..). In the while loop in your constructor you are not creating new nodes. So when you do
ptemp = ptemp->next
ptemp->next is NULL, so ptemp is now NULL.
ptemp->element = temp
will now break because ptemp is a NULL pointer.
So you need to add
ptemp->next = new Node;
Before
ptemp = ptemp->next


You also have an off-by-one bug in the while loop, the end character will not be added.

Good luck!

Last edited by Icon : February 19th, 2006 at 08:29 AM.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Need help with my code


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT