wey97
Nov 22nd, 2000, 12:27 AM
If anyone can explain why this code doesn't work:
#include <iostream.h>
//******************************* Node class *********************************************
class Node{
char data;
Node *next;
public:
Node() : data(0), next(0){}
Node(char e) : data(e), next(0){}
friend class myString;
friend ostream& operator << (ostream& os, const Node& x);
friend ostream& operator << (ostream& os, const myString& str);
};
ostream& operator << (ostream& os, const Node& x){
os << x.data;
return os;}
//******************************** myString prototypes ******************************************
class myString{
Node *head;
public:
myString();
int addRear(char ch);
friend ostream& operator << (ostream& os, const myString& str);
friend istream& operator >> (istream& is, myString& str);
};
//*********************************** myString Functions ***********************************
myString :: myString() : head(0) {}
int myString :: addRear(char ch){
if (! head) { // adding to empty list
head = new Node(ch);
return 1;}
Node *temp = head;
while (temp -> next) // find end of list
temp = temp -> next;
Node *newone = new Node(ch);
temp -> next = newone;
return 1;}
ostream& operator << (ostream& os, const myString& str) {
Node *temp = str.head;
while (temp) {
os << *temp;
temp = temp -> next;}
delete temp;
return os;}
istream& operator >> (istream& is, myString& str){
char ch;
while(ch != '\n' && ch != ' '){
cin.get(ch);
if(ch != '\n' && ch != ' ')
str.addRear(ch);}
return is;}
//*********************************** MAIN *******************************************************
void main (void){
myString a , b;
int op;
cin >> op;
switch(op){
case 1:
cin >> a;
cout << a;
break;
case 2:
cin >> b;
cout << b;
break;
}
}
And why this code works for case 1 but not case 2:
#include <iostream.h>
//******************************* Node class *********************************************
class Node{
char data;
Node *next;
public:
Node() : data(0), next(0){}
Node(char e) : data(e), next(0){}
friend class myString;
friend ostream& operator << (ostream& os, const Node& x);
friend ostream& operator << (ostream& os, const myString& str);
};
ostream& operator << (ostream& os, const Node& x){
os << x.data;
return os;}
//******************************** myString prototypes ******************************************
class myString{
Node *head;
public:
myString();
int addRear(char ch);
friend ostream& operator << (ostream& os, const myString& str);
friend istream& operator >> (istream& is, myString& str);
};
//*********************************** myString Functions ***********************************
myString :: myString() : head(0) {}
int myString :: addRear(char ch){
if (! head) { // adding to empty list
head = new Node(ch);
return 1;}
Node *temp = head;
while (temp -> next) // find end of list
temp = temp -> next;
Node *newone = new Node(ch);
temp -> next = newone;
return 1;}
ostream& operator << (ostream& os, const myString& str) {
Node *temp = str.head;
while (temp) {
os << *temp;
temp = temp -> next;}
delete temp;
return os;}
istream& operator >> (istream& is, myString& str){
char ch;
while(ch != '\n' && ch != ' '){
cin.get(ch);
if(ch != '\n' && ch != ' ')
str.addRear(ch);}
return is;}
//*********************************** MAIN *******************************************************
void main (void){
myString a , b;
int op;
cin >> op;
switch(op){
case 1:
cin >> a;
cout << a;
case 2:
cin >> b;
cout << b;
}
}
If you can explain this, I will give you $0.01 U.S. currency.
(Hint: The last code doesn't use 'break' in the switch/case statement.)
#include <iostream.h>
//******************************* Node class *********************************************
class Node{
char data;
Node *next;
public:
Node() : data(0), next(0){}
Node(char e) : data(e), next(0){}
friend class myString;
friend ostream& operator << (ostream& os, const Node& x);
friend ostream& operator << (ostream& os, const myString& str);
};
ostream& operator << (ostream& os, const Node& x){
os << x.data;
return os;}
//******************************** myString prototypes ******************************************
class myString{
Node *head;
public:
myString();
int addRear(char ch);
friend ostream& operator << (ostream& os, const myString& str);
friend istream& operator >> (istream& is, myString& str);
};
//*********************************** myString Functions ***********************************
myString :: myString() : head(0) {}
int myString :: addRear(char ch){
if (! head) { // adding to empty list
head = new Node(ch);
return 1;}
Node *temp = head;
while (temp -> next) // find end of list
temp = temp -> next;
Node *newone = new Node(ch);
temp -> next = newone;
return 1;}
ostream& operator << (ostream& os, const myString& str) {
Node *temp = str.head;
while (temp) {
os << *temp;
temp = temp -> next;}
delete temp;
return os;}
istream& operator >> (istream& is, myString& str){
char ch;
while(ch != '\n' && ch != ' '){
cin.get(ch);
if(ch != '\n' && ch != ' ')
str.addRear(ch);}
return is;}
//*********************************** MAIN *******************************************************
void main (void){
myString a , b;
int op;
cin >> op;
switch(op){
case 1:
cin >> a;
cout << a;
break;
case 2:
cin >> b;
cout << b;
break;
}
}
And why this code works for case 1 but not case 2:
#include <iostream.h>
//******************************* Node class *********************************************
class Node{
char data;
Node *next;
public:
Node() : data(0), next(0){}
Node(char e) : data(e), next(0){}
friend class myString;
friend ostream& operator << (ostream& os, const Node& x);
friend ostream& operator << (ostream& os, const myString& str);
};
ostream& operator << (ostream& os, const Node& x){
os << x.data;
return os;}
//******************************** myString prototypes ******************************************
class myString{
Node *head;
public:
myString();
int addRear(char ch);
friend ostream& operator << (ostream& os, const myString& str);
friend istream& operator >> (istream& is, myString& str);
};
//*********************************** myString Functions ***********************************
myString :: myString() : head(0) {}
int myString :: addRear(char ch){
if (! head) { // adding to empty list
head = new Node(ch);
return 1;}
Node *temp = head;
while (temp -> next) // find end of list
temp = temp -> next;
Node *newone = new Node(ch);
temp -> next = newone;
return 1;}
ostream& operator << (ostream& os, const myString& str) {
Node *temp = str.head;
while (temp) {
os << *temp;
temp = temp -> next;}
delete temp;
return os;}
istream& operator >> (istream& is, myString& str){
char ch;
while(ch != '\n' && ch != ' '){
cin.get(ch);
if(ch != '\n' && ch != ' ')
str.addRear(ch);}
return is;}
//*********************************** MAIN *******************************************************
void main (void){
myString a , b;
int op;
cin >> op;
switch(op){
case 1:
cin >> a;
cout << a;
case 2:
cin >> b;
cout << b;
}
}
If you can explain this, I will give you $0.01 U.S. currency.
(Hint: The last code doesn't use 'break' in the switch/case statement.)