Hi All.. New here..

I'm not going to lie. This is an assignment for c++ subject in my school. I'm basically an amateur in c++. I can understand all the basic stuffs in c++, but have some problems with the pointer.. and my lecturer here.. well. She's quite poor and really she skips lots of stuffs and teaches simple things and asks us to do hard things.

I just post a summary from the assignment and my tries on that.. i need your help. Please. Not the exact solution. Just tell me an overview/ how to do it. It will be really helpful for me. And trust me i will remember your help forever. biggrin.gif

Ok here's the summary of 1st Q..

1) split a string using delimiters.

like..

19, St.John's street, Valley Lane becomes

unit no = 19
street address = st.jhons street
town = Valley Lane


and 18/24, greenwich
becomes unit no = 18, street address = 24, town = greenwich

delimiter canbe (/ , space, tab)

Here's my attempt ( a poor one)

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

int main ()
{

  char str[200];
  char * add;
  cout<<"Enter a String"<<flush<<endl;
  cin>>add;
  while (add != NULL)
  {
    cout<<"string is:"<<add<<endl;
    add = strtok (NULL, " ,.-");
  }
  system ("pause");
  return 0;
}

2) linked list..

(we only need to use linked list and struct for this)..

there is a student struct with 3 attributes (studentId, name, major)..

and we need to enrol the student into subjects.. a student only can take 4 subjects at a time. the subjects has 4 attributes (section number, start time, start date and room number)..

there's another thing called course here, where the subjects will be offered. which means course ---> subjects ---> students

this is really confusing because i would have finished it had it been only one structure on it, now i'm confused because i don't know whether i have to use one struct for students (a must) or two for students and subjects..

Do i have to go through a normal linked list method or have to adapt doubly linked list method???

ok here's my effort..

#include <iostream>

using namespace std;

struct Student
{
       char name [30];
       char studentId [30];
       char major [30];
       Student *next;
};

struct CourseSection
{
       char sectionNumber [20];
       int startTime;
       int startDate;
       int roomNumber;
};

struct Node
{
       Student stud;
       CourseSection cs;
       Node *next;

void add (char name[], char studentId[], char major[]);

int main()
{
    struct student n1;
    n1.name == "JOHN";
    n1.next->name;
    cout<<"Name:"<<n1.name<<endl;
    n1.studentId == "ITV678";
    n1.next->studentId;
    n1.major == "Engineering";
    n1.next->major;
    add (name, studentId, major)
    cout<<"Student Id:"<<n1.studentId<<endl;
    cout<<"Major:"<<n1.major<<endl;
   
    system("pause");
    return 0;

void add (char name[20], char studentId[20], char major[20])
{
courseSection *temp1, *temp2;
temp new student;
cout<<"Enter the name of the student:"<<flush;
cin>>temp->student.name;
temp->next = NULL;

}


};

i know it's easier when i use class instead of structures.. but the assignment requires (forces) structure to be used..

(i've gone though the assignment/homework rule here.. well i don't want the exact solution.. just give me an idea how to start with that. or some examples)..

thanks in advance..

Recommended Answers

All 5 Replies

You probably should have split this into two different threads because both the problems have different solutions. So I'm only going to comment on the first problem.

line 11: This is declaring an unitilized pointer which points to some random location in memory. You have to allocate memory to it before it can be used in a line such as line 13. Its far simpler to declare it as a character array, such as char add[126]; , which will hold up to 125 characters plus string's null terminator.


line 13: The >> operator will not allow you to enter spaces, so if you want a sentence then cin >> add will not work. What you want is cin.getline(add, sizeof(add)); Now you need to declare another pointer so that add is undisturbed.

char *ptr = strtok(add, ","); // split the strig at the comma
cout << "unit num = " << ptr << "\n";
ptr = strtok( NULL, ",");
cout << "street address = " << ptr << "\n";
// etc. etc

thanks sir..

i did this imemdiately, but it doesn't even compile and the windows error message pops out (send, don't send report message)

#include <stdio.h>
#include <string.h>
#include <iostream>

using namespace std;

int main ()
{
  char * add[200];
  cout<<"Enter a string:"<<flush;
  cin.getline(add[200], sizeof(add[200]));
  char *ptr = strtok(add[200], ","); 
  cout << "unit num = " << ptr << "\n";
  ptr = strtok( NULL, ",");
  cout << "street address = " << ptr << "\n";
 
  system ("pause");
  return 0;
}

char * add[200];

The above declares an array of 200 char *'s. Drop the * from the above to get a variable that will hold a string with up to 199 char plus the null char.

The call to getline would be:

cin.getline(add, 199);

Actually, I forget whether the second variable represents the full capacity of add, or capacity minus 1 (to leave room for the null char).

If you call strtok() with multiple delimters and the strings have variable formats, then you can end up with unexpected results. For example, if you call strtok() with the space char and the comma char as delimiters and try to parse the following string

19 St. Germain, Norwich

then I believe you will end up with the following tokens

19
St.
Germain
an empty string
Norwich

though I admit I haven't run a program to prove it.

thanks learner..

but when i don't declare the size for the address array on the first line, it gives me errors (invalid conversion) when i enter values through cin.getline(add, 199).

thanks learner..

but when i don't declare the size for the address array on the first line, it gives me errors (invalid conversion) when i enter values through cin.getline(add, 199).

You need to declare the size, but you want an array of 200 char not an array of 200 pointer to char. so make it: char add[200] and not char * add[200];

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.