473,385 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

need help with my C++ program

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 2161
Banfa
9,065 Expert Mod 8TB
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
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...
2
by: aj902 | last post by:
Hello , I am trying to create a program where all detail, http://www.albany.edu/~csi333/projects.htm
13
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...
4
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...
16
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...
8
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...
4
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...
6
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...
1
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...
1
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.