473,387 Members | 1,464 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,387 software developers and data experts.

structs ... homework proj

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...thast 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)...and i tried defining them in the ' int main() '

i know what to normally do if it wasn't a struct.......but 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...since this isn't allowed
here is the program.

#include <iostream>


struct node {
int data;

node* next;
};

node* insertAtHead(node* head, int newitem) {
node* n = new node;
n->data = newitem;
n->next = head;
head = n;
return head;
}

node* removeAtHead(node* 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(head, 4);
head = insertAtHead(head, 2);
head = insertAtHead(head, 3);
printList(head);
while ( !isEmpty(head) )
head = removeAtHead(head);
return 0;
}
Jul 21 '06 #1
6 1732
umm... i don't want the answer....if thats ok with you guys...just a suggestion or hint of waht to do...and then if/when i get it to work..i'll post my solution....sorry again cuz i know this is a seroius programmer bored...but i figure this helps you guys remmeber some of the stuff that you might take for granted..
thanks
Jul 21 '06 #2
Banfa
9,065 Expert Mod 8TB
OK here are some functions you may wish to use

fopen - opens a file
fclose - closes a file
fgets - reads a line of data from a file
strtol - converts a string containing digits into its binary equivilent (i.e. char [] to int)

Remember if you have a string it's is just an array of characters which you can access individually

i.e.

char line[100]

use fgets to read line

if (line[0] == '>')
{
it's the number of numbers
}

Are those enough hints?
Jul 21 '06 #3
just to clarify

head = insertAtHead(head, 4);
head = insertAtHead(head, 2);
head = insertAtHead(head, 3);

thats whats being manipulated.... its going to be a loop
head = insertAtHead(head, var1);

secondly...do i need to use pointers or can i do it how it is. thanx.
I'm still playing around with the stuff in your post banfa..
Jul 21 '06 #4
here is my int main segment
i know there is a mistake in the loop, but could you tell me if i am using the fgets function and strtol function correctly...thanx

The errors i am getting flag at the pFile=fopen that part. so i need help wtih that too

int main( ) {
node* head = NULL;
FILE * pFile;
pFile=fopen('fileInput.txt',"r");
char variables [10000];
fgets(variables,10000, pFile);
char stuff;
stuff= variables[1];
int counter = strtol(stuff);

int i;
int var1;
while(i=0, i<counter, i++)
{

var1= variables[i+2];
head = insertAtHead(head, var1);
}

// head = insertAtHead(head, 2);
// head = insertAtHead(head, 3);
printList(head);
while ( !isEmpty(head) )
head = removeAtHead(head);
fclose(pFile);
return 0;
}
Jul 22 '06 #5
Banfa
9,065 Expert Mod 8TB
------------------------------------------------------------------------------------------------------------------
pFile=fopen('fileInput.txt',"r");

You've got the wrong quotes round the file name use

pFile=fopen("fileInput.txt","r");

------------------------------------------------------------------------------------------------------------------
char variables [10000];

I would say that 10000 is slightly excesive I would have thought 1000 would be ample since commonly people don't put more than 80 - 100 chacaters on the line of a text file.

------------------------------------------------------------------------------------------------------------------
fgets(variables,10000, pFile);

Should work but bad form, what if you change the size of variables, the code will not be robust to that change try this instead

fgets(variables, sizeof variables, pFile);

this will be robust to a change in size of variables as the compiler works the size out for you

------------------------------------------------------------------------------------------------------------------
char stuff;
stuff= variables[1];
int counter = strtol(stuff);

This is completely wrong the definition of strtol is

long strtol ( const char * string, char** endptr, int radix );

Forget the variable stuff you are going to want something that looks more like

char *pEnd;
int counter = strtol(&variables[1], &pEnd, 10);
Jul 22 '06 #6
Wanted to say thanx again banfa.
Incase anyone is interested, here is the final code....i just manipulated the int main portion. it might not be the most concise way to do it, but it worked.

int main( ) {
node* head = NULL;
FILE * pFile; // used to store the variables that are inported
pFile=fopen("listInput.txt","r");

int i=0; //used as a counter

int var1; // will hold the individual values after converted to int

char variables [1000]; // creates an array to store the variables from teh file

fgets(variables,sizeof variables, pFile); // gets header line of the text file

char *pEnd;

int max = strtol(&variables[1], &pEnd, 10);
// converts the second number in the array "variables"
// into an integer to determine the number of elements in the text file




fgets(variables,sizeof variables, pFile); // gets the second row of numbers


//the following loop converts the elements of the array, variables,into integers and puts them into the list

while(i<max)
{

var1 = strtol(&variables[i], &pEnd, 10);
head = insertAtHead(head, var1);
// the following loop gets rid of some redundancy, for instance
// if the first element of the text file was 777
// variables[0] = 777, variables[1] = 77, variables[2] = 7, it also read the space as an element
// and uses the previous value in that spot.
// This loop also increases the value of "max", since 2nd integer might be at i=5.


while(variables[i] !=' ')

{
i++;
max++;


}


i++; // takes i to the point after the blank space

}

printList(head); // prints the list
while ( !isEmpty(head) ) // removes elements from the list
head = removeAtHead(head);

fclose(pFile);// closes the file
return 0;

}
Jul 30 '06 #7

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

Similar topics

9
by: Joolz | last post by:
Could someone please give me an example of how to make pointers to nested structures? Pointers to normal structures are no problem... struct structA { int a; }structA_s; structA *pStructA =...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
26
by: Bail | last post by:
I will have a exam on the oncoming friday, my professor told us that it will base upon this program. i am having troubles understanding this program, for example what if i want to add all the...
9
by: Marc Miller | last post by:
Hi all, I have 2 dev. machines, the 1st is Win 2000 with .NET 7.0 and the 2nd is XP Pro with .NET 2003. My Web Server is Win 2000 Server with IIS 5.0. I can create a new project on my test...
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
4
by: pcnate | last post by:
I've been having some problems with pointers and such.This is homework, so I don't want people writing codeand telling me to use it. I just want some direction on what isn't working. here is...
2
by: vaishnavi | last post by:
hi, i want to do a proj for 2nd sem of mcs. could anyone tell me some project topics in java ? some proj topics in servlet or rmi..... or related with db.... pls send me list of the topics....
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
2
by: kkshansid | last post by:
$projid=$_GET; $q="INSERT INTO proj select * from projtemp where projid ='".$projid."'"; $qins=mysql_query($q); is resulting false is there any error? there are 2 tables in a database with name...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.