473,804 Members | 3,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

structs ... homework proj

7 New Member
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;
}
Jul 21 '06 #1
6 1746
wandafoda
7 New Member
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....sor ry 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 Recognized Expert Moderator Expert
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
wandafoda
7 New Member
just to clarify

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

thats whats being manipulated.... its going to be a loop
head = insertAtHead(he ad, 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
wandafoda
7 New Member
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...tha nx

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('fi leInput.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(he ad, var1);
}

// head = insertAtHead(he ad, 2);
// head = insertAtHead(he ad, 3);
printList(head) ;
while ( !isEmpty(head) )
head = removeAtHead(he ad);
fclose(pFile);
return 0;
}
Jul 22 '06 #5
Banfa
9,065 Recognized Expert Moderator Expert
------------------------------------------------------------------------------------------------------------------
pFile=fopen('fi leInput.txt',"r ");

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

pFile=fopen("fi leInput.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(&variabl es[1], &pEnd, 10);
Jul 22 '06 #6
wandafoda
7 New Member
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("li stInput.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(&variabl es[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(&variabl es[i], &pEnd, 10);
head = insertAtHead(he ad, 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(he ad);

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
8627
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 = &structA_s; But when I've got a nested structure, I can't make a pointer to that
5
3134
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
2210
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 total calories that the user input together. determine which food has the largest calories. how do i start to modifiy the program inorder to do the things i listed above. thanks #include <stdio.h> #include <stdlib.h>
9
2312
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 server from the 1st machine, but I receive an 'HTTP/1.1 500 Internal Server error" from my Web Server. My userid/password are the same on all 3 machines.
61
3784
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 (myBranch + x) = new Branch(i); // it doesn't x is a loop iterator, i is an int for the constructor to define an array. What am I doing wrong here.
4
1368
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 the structure prototype: struct Books { string ISBN; string title;
2
1632
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. reply soon. thank u. vaishnavi
29
2792
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 overhead (I think). A Struct seems more appropriate. At least it is what I would have used in other languages. But since a Struct *can* hold methods, I wander if I am saving anything. If not, why use it?
2
2415
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 proj and projtemp
0
9710
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9163
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3830
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3000
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.