473,698 Members | 2,051 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need help with my C++ program

1 New Member
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
Feb 18 '06 #1
1 2172
Banfa
9,065 Recognized Expert Moderator Expert
The problem is in the constructor Set::Set(char start, char end), you are only allocating 1 Node structure instead of 1 for each Node that the function is trying to set-up.

You need to add the line ptemp->next = new Node; as the first line of the while loop in the Set::Set(char start, char end) constructor and a line to set the next pointer of the last Node to NULL giving:
Expand|Select|Wrap|Line Numbers
  1. Set::Set(char start,
  2.   char end) {
  3.   char temp = start;
  4.   Node* ptemp = new Node;
  5.   ptemp->element = temp;
  6.   ptemp->next = NULL;
  7.   head = ptemp;
  8.   temp++;
  9.   while (temp != end){
  10.     ptemp->next = new Node;
  11.     ptemp = ptemp->next;
  12.     ptemp->element = temp;
  13.     temp++;
  14.   }
  15.   ptemp->next = NULL;
  16. }
  17.  
Some other points, in the constructor using ptemp and temp as variable names is not good style, in my opinion, variable names should have proper meanings, this makes the code more readable and easier to maintain 10 years down the line.

If you made Node a class (or actually even if you didn't) you could give it a constructor (or 2 say Node::Node(void ) and Node::Node(char value) ) this would add more structure to the code and provide better abstraction:

Expand|Select|Wrap|Line Numbers
  1. struct Node{
  2.   char element;
  3.   Node* next;
  4.  
  5.   Node::Node(void);
  6.   Node::Node(char value);
  7. };
  8.  
  9. Node:Node(void)
  10. {
  11.   element = '\0';
  12.   next = NULL;
  13. }
  14.  
  15. Node:Node(char value)
  16. {
  17.   element = value;
  18.   next = NULL;
  19. }
  20.  
  21. Set::Set(char start, char end) {
  22.   char nextNodeValue = start;
  23.   Node* ptemp = new Node(nextNodeValue);
  24.   head = ptemp;
  25.   nextNodeValue++;
  26.   while (nextNodeValue != end){
  27.     ptemp->next = new Node(nextNodeValue);
  28.     ptemp = ptemp->next;
  29.     nextNodeValue++;
  30.   }
  31. }
  32.  
Note this code still isn't perfect, I have presevered the logic that you are using however by this logic the line Set B('A', 'Z'); will set up nodes for the letters A - Y, there will be no Z node, is this what you intended? Also have you thought about what will happen if start == end in Set::Set?
Feb 20 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3110
by: Spamtrap | last post by:
I only do occasional Perl programming and most things I write are short processes. I have something I'm working on that is scanning a text file with about 15 million lines and trying to extract matches from another text file, which has about 170 entries. The second text file is read into an array. The process then scans through the big file for certain possible patterns - it will find those in about 1 out of 25 lines,, when it finds one,...
2
2866
by: aj902 | last post by:
Hello , I am trying to create a program where all detail, http://www.albany.edu/~csi333/projects.htm
13
2460
by: vgame64 | last post by:
Hi, I have been struggling with writing a program for a few hours. The requirements are that: """You will be writing a program which will determine whether a date is valid in terms of days in that month. We are assuming that the year will be valid 4 digit integer. So you don't have to think much about that(in terms of validation) except for the month of February. If the month is February, then you have to check whether that year is Leap...
4
2209
by: robinsand | last post by:
My apologies to those of you who are more advanced Visual C++ .NET programmers, but I am working on a project for an MBA course that is condensed into an eight-week schedule, and I need help getting a program up and running with proper files and documentation to be handed in for a grade (on Microsoft Visual Studio .NET 2003). I am being graded on how well I incorporate advanced C++ features such as inheritance, polymorphic programming,...
16
2528
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client uses IE to talk with a server. The user on the client (IE) sees an ASP net page containing a TextBox. He can write some text in this text box and push a submit button.
8
2744
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only the sequence nos and it should be such that i can access it through the structure .plz help me .
4
2199
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will implement all three techniques as programs. In these programs, as well as solving the problem, you will also measure how long the program takes to run. The programs are worth 80% of the total mark. The final 20% of the marks are awarded for a...
6
2123
by: naknak | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will implement all three techniques as programs. In these programs, as well as solving the problem, you will also measure how long the program takes to run. The programs are worth 80% of the total mark. The final 20% of the marks are awarded for a...
1
5681
by: peterggmss | last post by:
This is a slot machine game, 2 forms. One is the actual game (frmMachine) and the other is in the background and randomizes the images shown on frmMachine. I need to make frmMachine wait for frmRandom, i tried it with the Sleep API and a Do/Until Loop and neither worked, could you please help me, I have my code below. frmMachine 'Slot Machine, (c) 2006 Peter Browne, GPL Licensed ' 'Peter Browne 'Sheridan Corporate Centre '2155 Leanne...
1
1968
by: raghavshastri | last post by:
You are to write a C++ program to perform a statistical analysis of the blobs in an image. The image will be a grayscale image in PGM format for simplicity. Here is a sample PGM image with 10 columns and 4 rows: P2 # Comment lines # They follow the P2 line. # There could be 0, 1 or more 10 4 255
0
8598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8856
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4613
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2321
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.