Sorry guys....another homework question and if you don't wanna help out..i understand.
just to say..im' a chemical engineer and we are required at will to do whatever...thas t why i have to take C++ and would like to also understand what im' doing.....to further my career..
i'll include the program after the description
So my teacher gave the class an algorithm we have to manipulate..... .its a struct and i understand what is going on in her program....but i have to make the program input a file 'fileInput.txt' and it follows this patter
>4
1 2 3 4
the 4 means there are 4 variables...but thats my first problem......i do'nt konw how to make c++ read the '>' as a seperate variable then make the '4' be somthing else...there are no spaces between the '>' and the '4'.
Secondly...i do'nt konw where in the program to define my variables.....i tried making them global variables (i might be wrong on that defintion)...an d i tried defining them in the ' int main() '
i know what to normally do if it wasn't a struct.......bu t when i try to do that in this...i get about 40 errors.
basically its ...
ifstream errors.
if you guys could help me out...thanx
"wandafoda".... ..i'm a dude..but being shady incase my prfo check the internet...sinc e this isn't allowed
here is the program.
#include <iostream>
struct node {
int data;
node* next;
};
node* insertAtHead(no de* head, int newitem) {
node* n = new node;
n->data = newitem;
n->next = head;
head = n;
return head;
}
node* removeAtHead(no de* head) {
node* del;
if ( head != NULL) {
del = head;
head = head->next;
delete del;
}
return head;
}
void printList(node* head) {
node* ptr;
for (ptr = head; ptr != NULL; ptr=ptr->next)
std::cout << ptr->data << ' ';
std::cout << std::endl;
}
bool isEmpty(node* head) {
if ( head != NULL )
return false;
else
return true;
}
int main( ) {
node* head = NULL;
head = insertAtHead(he ad, 4);
head = insertAtHead(he ad, 2);
head = insertAtHead(he ad, 3);
printList(head) ;
while ( !isEmpty(head) )
head = removeAtHead(he ad);
return 0;
}
just to say..im' a chemical engineer and we are required at will to do whatever...thas t why i have to take C++ and would like to also understand what im' doing.....to further my career..
i'll include the program after the description
So my teacher gave the class an algorithm we have to manipulate..... .its a struct and i understand what is going on in her program....but i have to make the program input a file 'fileInput.txt' and it follows this patter
>4
1 2 3 4
the 4 means there are 4 variables...but thats my first problem......i do'nt konw how to make c++ read the '>' as a seperate variable then make the '4' be somthing else...there are no spaces between the '>' and the '4'.
Secondly...i do'nt konw where in the program to define my variables.....i tried making them global variables (i might be wrong on that defintion)...an d i tried defining them in the ' int main() '
i know what to normally do if it wasn't a struct.......bu t when i try to do that in this...i get about 40 errors.
basically its ...
ifstream errors.
if you guys could help me out...thanx
"wandafoda".... ..i'm a dude..but being shady incase my prfo check the internet...sinc e this isn't allowed
here is the program.
#include <iostream>
struct node {
int data;
node* next;
};
node* insertAtHead(no de* head, int newitem) {
node* n = new node;
n->data = newitem;
n->next = head;
head = n;
return head;
}
node* removeAtHead(no de* head) {
node* del;
if ( head != NULL) {
del = head;
head = head->next;
delete del;
}
return head;
}
void printList(node* head) {
node* ptr;
for (ptr = head; ptr != NULL; ptr=ptr->next)
std::cout << ptr->data << ' ';
std::cout << std::endl;
}
bool isEmpty(node* head) {
if ( head != NULL )
return false;
else
return true;
}
int main( ) {
node* head = NULL;
head = insertAtHead(he ad, 4);
head = insertAtHead(he ad, 2);
head = insertAtHead(he ad, 3);
printList(head) ;
while ( !isEmpty(head) )
head = removeAtHead(he ad);
return 0;
}
Comment