C++ :: Outputting Data From File Into Structs And Arrays Of Structs

Apr 15, 2013

I am having a lot of trouble being able to get data from a file and input it into given structs and arrays of structs and then outputting the file. We are given a file that contains 96 lines and looks like this:

Arzin, Neil
2.3 6.0 5.0 6.7 7.8 5.6 8.9 7.6
Babbage, Charles
2.3 5.6 6.5 7.6 8.7 7.8 5.4 4.5

This file continues for 24 different people and then repeats with different scores (the second line).
The first number, in this case is 2.3 for both people is a difficulty rating. The next 6 numbers are scores.

We are given this data in order to set up our structs and arrays and my code:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
int main () {
ifstream inFile;
inFile.open("C://diveData.txt);

[Code] .....

View 1 Replies


ADVERTISEMENT

C/C++ :: Store Data From A File Into Array Of Structs

Mar 10, 2015

That;s what i have so far: problem: the output data is not correct.

input file
1301 105515018 "Boatswain" "Michael R." CSE 230 ="R01"
1301 103993269 "Castille" "Michael Jr" CSE 230 ="R03"
1301 103993267 "Castille" "Janice" CSE 230 ="R03"

[Code]....

View 1 Replies View Related

C++ :: Structs And Arrays

Jul 30, 2014

If i want to input (FROM A FUNCTION) information into a struct:

* How do I define/ call/ prototype that function.
* How do I check if that array inside the function is empty?

View 1 Replies View Related

C :: Structs With Double Arrays

Apr 19, 2014

The main is not working properly, I get strange missing parenthesis notices and array errors which I am not understanding. How to identify the errors. Functions are included.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int menu ();
double change_value (double * value);

[Code] ......

View 3 Replies View Related

C++ :: Using Structs And Arrays For Grading?

Jul 3, 2012

Write a program for the following problem. You're given a file that contains a collection if IDs and scores (type int) for an exam in your computer course. You're to compute the average of these scores and assign grades to each student according to the following rule:

If a student's score is within 10 points (above or below) of the average, assign a grade of satisfactory. If a studnt's score is more than 10 points above average, assign a grade of outstanding. If a student's score is more than 10 points below average, assign a grade of unsatisfactory.

The output from your program should consist of a labeled three-column list that shows each ID, score, and corresponding grade. Use a struct to store each student's data and an array of structs to store the whole class. The struct should have a data member for id, score, and grade.

I was given the following file grades:

3313904258647075100
568888487970797094
4700504489737073100
956188698887846398
31999669100908867100
376878578059571560
82917256708274983
7754766293100784158
81469468999493954
2106984796947027100

So far, I have this much, but the output is all wrong. I also haven't tackled the grade part yet.

Code:

//grader.cpp
//Grades

#include <fstream>//required for file streams
#include <iostream>//input/output
#include <cstdlib>//for definition of EXIT_FAILURE
#include <string>
#include <iomanip>
using namespace std;
#define inFileGrades "grades.txt"//grades

[code]....

View 4 Replies View Related

C/C++ :: Function Not Returning Values - Structs In Arrays

Feb 25, 2015

I wrote this code for a homework assignment, everything runs fine but the function void percent_votes (line 66) isn't calculating properly, it outputs 0.00 for each value. I have tried everything I can think of to try and make it work.

Here is the assignment: Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate's name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election.

Here is the code I have written:

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

[Code].....

View 4 Replies View Related

C/C++ :: Array Of Structs - Reallocate Distorting Data Records

Mar 29, 2014

I have an array of structs (PlanetRecord). Each time I realloc, one of the records gets distorted. Here's my memory allocation code:

if(currentArraySize == 0) {
printf("allocating memory");
planetRecords =
( PlanetRecord* ) malloc( sizeof( PlanetRecord ) * BLOCK );
currentArraySize = BLOCK;

[Code] ....

I am increasing my array in multiples of 5 (or in this case, my constant BLOCK). This is the result of my printout (t1-t40 is correct, the rest of the values should be 1):

t1 1 1
t2 1 1
t3 1 31329
t4 31297 1
t5 1 31249
t6 31217 1
and so on .....

I noticed that in my test, realloc was called 7 times. My array has 7 distorted records. Each time I call realloc, it is distorting one of my records.

View 7 Replies View Related

C++ :: Structured Data - Nesting Triangles Using Structs And Doing Some Geometric Calculation

Sep 8, 2013

nestedTriangles.cpp description:

The program takes as input a pair of triangles, specified be giving the coordinates of each trangle's vertices. It then determines if either triangle is "nested" within the other, meaning that one triangle lies entirely within the interior of the other.

Pseudocode:

One triangle lies within another if and only if all three vertices of the first triangle lie within the interior of the second triangle.

Suppose that we have a triangle with vertices A, B, and C, described by the coordinates (xA, yA), (xB, yB), and (xC, yC), respectively. The sides of the triangle are the line segments AB, BC, and CA.

A line passing through two points (x1, y1) and (x2, y2) can be considered to be the set of points (x,y) satisfying the equation

f(x,y) = 0
where f(x,y) is given as
f(x,y) = (x - x1) (y2 - y1) - (y - y1) (x2 - x1)

One of the interesting things about that f(x,y) is that we can use it to determine which "side" of the line an abitrary point (x,y) is on:

If f(x,y) = 0, the point is exactly on the line. All points for which f(x,y) > 0 are on one side of the line, and All points for which f(x,y) < 0 are on the other side So the problem of determining whether a point (x,y) is on the inside of a trangle can be checking the sign of f(x,y) for each of the three lines making up the triangle. A complicating factor is that we don't know, for any given triangle, whether those three signs should be all positive, all negative, or some mixture of the two.

The centroid of a triangle can be computed as the "average" of the x and y coordinates of the vertices:

xcen = (xA + xB + xC)/3
ycen = (yA + yB + yC)/3

This point (xcen, ycen) is definitely inside the trangle (unless the triangle is "degenerate" and has no interior points). The problem of determining whether (x,y) is on the inside of a triangle can therefore be resolved by checking to see if it is on the same side of each of the trangle's line segments as (xcen, ycen).

What I need:

I want to fill in the missing bodies for the functions eval and areOnSameSideOf, which manipulate line segments. I think calling eval from within areOnSameSideOf will simplify the implementation of the latter.

Code:
#include <iostream>
using namespace std;
/**
* 2D Cartesian coordinates
*/

[Code]....

View 2 Replies View Related

C++ :: Structured Data / Nesting Triangles Using Structs And Doing Some Geometric Calculation?

Sep 8, 2013

nestedTriangles.cpp description:

The program takes as input a pair of triangles, specified be giving the coordinates of each trangle's vertices. It then determines if either triangle is "nested" within the other, meaning that one triangle lies entirely within the interior of the other.

Pseudocode:

One triangle lies within another if and only if all three vertices of the first triangle lie within the interior of the second triangle.Suppose that we have a triangle with vertices A, B, and C, described by the coordinates (xA, yA), (xB, yB), and (xC, yC), respectively. The sides of the triangle are the line segments AB, BC, and CA.A line passing through two points (x1, y1) and (x2, y2) can be considered to be the set of points (x,y) satisfying the equation

f(x,y) = 0
where f(x,y) is given as
f(x,y) = (x - x1) (y2 - y1) - (y - y1) (x2 - x1)

One of the interesting things about that f(x,y) is that we can use it to determine which "side" of the line an abitrary point (x,y) is on:

If f(x,y) = 0, the point is exactly on the line.

All points for which f(x,y) > 0 are on one side of the line, and All points for which f(x,y) < 0 are on the other side So the problem of determining whether a point (x,y) is on the inside of a trangle can be checking the sign of f(x,y) for each of the three lines making up the triangle.

A complicating factor is that we don't know, for any given triangle, whether those three signs should be all positive, all negative, or some mixture of the two.

The centroid of a triangle can be computed as the "average" of the x and y coordinates of the vertices:

xcen = (xA + xB + xC)/3
ycen = (yA + yB + yC)/3

This point (xcen, ycen) is definitely inside the trangle (unless the triangle is "degenerate" and has no interior points).

The problem of determining whether (x,y) is on the inside of a triangle can therefore be resolved by checking to see if it is on the same side of each of the trangle's line segments as (xcen, ycen).

I want to fill in the missing bodies for the functions eval and areOnSameSideOf, which manipulate line segments. I think calling eval from within areOnSameSideOf will simplify the implementation of the latter.

Code:
#include <iostream>
using namespace std;
/**
* 2D Cartesian coordinates
*/
struct Point {
double x;
double y;

[code]...

View 1 Replies View Related

C++ :: Binary File And Structs

Sep 5, 2014

What is the difference between reading from binary file between the following way:

struct aStrct{
int number;
}vars[];
while(file.good())
file.read((char*)&vars, sizeof(aStrct))

And

while(file.good())
file.read((char*)&vars[i], sizeof(aStrct))
i++;

View 2 Replies View Related

C/C++ :: Scanning CSV File Into Array Of Structs

Mar 18, 2015

I need to scan a .csv file that contains the following info:

programming,03,60,141,01,W,2015
programming,03,60,141,30,W,2015
Algebra,03,62,102,02,S,2013
Religion,08,98,938,20,F,2014

So i made a struct:

typedef struct CourseInfo {
int courseID;
char courseName[50];
char courseCode[13];
char term[7];
} courseinfo;

Where course code is the 4 numbers after the name together and the term is the letter and year in the last two pieces of info. I got this to work:

int main() {
FILE *p;
p = fopen("input.csv", "r+");
if(p == NULL) {
puts("The file could not be opened");

[Code] ......

But lets say i dont know how many lines i have in my file and i want to count them and then use that size for my array so i tried this by:

int main() {
FILE *p;
int lines = 1;
char ch;
p = fopen("input.csv", "r+");
if(p == NULL) {

[Code] .....

But the second program is not working for unknown reasons. I do not get any errors but its not scanning the info because when i print the info later on it prints out random symbols.

View 11 Replies View Related

C :: Reading Text From A File And Trying To Set Names To Structs

Feb 5, 2013

I am reading text from a file and trying to set names to structs. Here is the input file

Code:
2
3
Quincy Richard Steven
Donna Elizabeth Francine
5 2 9
4 5 7
5 9 6
4 6 5
7 8 9
6 9 1

Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_CHARS 20
// Structure that has 2 properties: name and scores.
struct person

[Code] ....

In the nested for-loop it should read in the name of each men and print it out, like so:

Code: Quincy Richard Steven

However it is printing out:

Code: QuincyRichardStevenDonnaElizabethFrancine

View 9 Replies View Related

C :: Array Of Structs (adding / Deleting Elements To A File)

Sep 20, 2013

I want to write a program to record my neighborhoods's name and address by using an array of structs and file.

my array of structs is

Code:
#define SIZE 30
typedef struct{
char name[30];
char address[100];
}Detail;
Detail neighbor[SIZE];

And I want to make adding,deleting, and searching functions.Something like

Code:

void add();//Add name and address to a file,
//and add more to the same file if I want to.
void del();//Delete or Change some neighbor's name or address
//in the same file(Can I?)
void search();//Search name and show detail

So I started to code adding function first, but I don't know that I need to use pointer to code each functions relations, and I don't know how to check that my input's already exists yet. But I started some code below...

Code:
void add() {
int i=0;
FILE *fp = fopen("neighborhood.txt", "at");
if ( fp != NULL ) {
do{

[Code]......

View 8 Replies View Related

C/C++ :: Array Of Structs To Store The Information From Input File?

Mar 10, 2015

I'm trying to read the data from a file i/o and put them into an array of structs. But when I run the program, it gives me a bunch of "garbage" as an output. my code and see

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

[Code].....

View 7 Replies View Related

C/C++ :: Pointers And Structs

Dec 14, 2014

What the second print statement prints

main() {
struct s1{
char *str;
int i;
struct s1 *ptr;

[Code] .....

I executed the code and the output was some number(for second printf) but it should print a string.

View 3 Replies View Related

C++ :: Nested Classes And Structs

Mar 12, 2014

I'm working on a project involving nested classes and structs like this:

Code: class A {
public:class B {
public:f()
{A::C* iCanDoThis; //no errors.
iCanAlsoDoThis->root->.... //this also works fine.}private:A::C* iCannotDoThis //this is what I would like to do.
Has errors
A* iCanAlsoDoThis;};private:struct C
{..data..};

C* root;};

Is it possible make a pointer to struct C a private member of class B?

View 1 Replies View Related

C :: Using Fwrite And Fread On Structs

Jul 20, 2014

I'm currently working on a program that writes an array of struct to a file and then read back the data from the file to another array of struct. At the bottom is an image of my result.

My goal is to end up with two identical struct arrays but my program fails to do this. My struct have to members: ID and kind (of animals in this case). I declare my first arraystruct africa[] with "monkey" and "giraffe" with their respectively IDnr: 112 and 555. I stream this data to a file and read read them back to the arraystruct get_animal[]. Simply I want the get_animal[] to be identical with the africa[] when the program is over, but that is not so. According to my result(bottom image) it display:

112, monkey (get_animal[0])
112, monkey (get_animal[1])
meaning that get_animal[0] is identical to africa[0] get_animal[1] is also identical to africa[0]

but why? I want get_animal[1] to be identical with africa[1]. meaning I want the result to look like this:

112, monkey
555, giraffe

I've also made the program to print the parameters of my fwrite/fread calls. Why is the 3rd parameter = 1 meaning that only 1 element will be read/written when my program just read/write 2 elements?

Code:

main(){
FILE *fp;
struct animals
{
int id;
char kind[20];
} africa[1] = {{112,"monkey"}, {555,"giraffe"}};
}

[code]....

View 3 Replies View Related

C++ :: Structs And Incrementing Output

Apr 3, 2013

How would I be able to have this display the rest of the households without having to have 13 separate cout lines(one for each household)? In other words, how can I set it up to increment the output?

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

struct Household {
int idNumber;
int income;

[Code] ....

View 3 Replies View Related

C++ :: Passing Structs And Functions All In Same

Mar 8, 2013

I think i am getting confused with passing structs and functions all in the same...When I run through the program (it compiles), the functions that add coins do not add, but rather just replace an old value with a new one.

#include <cstdlib>
#include <iostream>
using namespace std;
struct coinbox {

[code].....

View 1 Replies View Related

C++ :: Array Of Structs With Constructor?

Apr 1, 2013

I can't seem to remember everything I should about constructors. I'm looking for a way to create an array of structs, using a constructor. My code should explain.

struct myStruct
{
private:
int structInt1, structInt2;

[Code].....

View 2 Replies View Related

C++ :: Using Structs To Define A Function

Sep 27, 2013

What I'm trying to do :

struct foo {int x; int y;};
foo function_example(int x, int y) {
foo temp;
temp.x = x;
temp.y = y;
return temp;
}

Works as is, however when I try doing it through seperate class files :

//header file for class example_class
struct foo {int x; int y;};
foo function_example(int x, int y);

//source file for example_class
foo example_class::function_example(int x, int y) {
foo temp;
temp.x = x;
temp.y = y;
return temp;
}

I get an error telling me that foo is undefined, and that declaration of function_example(int x, int y) is incompatible with the declaration of it in the header file.

View 1 Replies View Related

C++ :: Pointers To Array Of Structs

Feb 14, 2013

I have an assignment where I need to use pointers to do a few things and I am a little confused on the syntax of it all. My question is how do you use a pointer to point to an array of structs.

For example

struct info{
char firstName[15];
char lastName[15];
};
main() {
info person[4];
cout << "The third letter of the second persons first name is: "; // ?????
}

how would I define a pointer to point to the third letter of first name the second person in the "person" array.

View 2 Replies View Related

C++ :: Filling A Vector With Structs

Feb 1, 2013

How to fill a vector with structs that are read in from a separate file. Each line in the file would read for example "Doe John M 26" for the name of the person, gender and age. I just need to get pointed in the right direction so I can get this started.

View 2 Replies View Related

C++ :: How To Qsort Array Of Structs By One Of Its Members

Mar 31, 2013

I am versed in qsorting struct members within a struct instance, as in the example below:

Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
struct item_database {

[Code]....

What I CANT do however is to sort an array of structs by one of its members. This is a complete different problem even if it sounds similar.

In other words, the problem is this; Say we have a struct:

Code:
struct item_database {
int key;
string token;
};

Then we declare an array of this struct in the heap:

Code: item_database *idptr = new item_database[700000]; and initialize its values.

How would one go about sorting this heap array of structs by its member function "token"?

I used the following call to qsort (standard library) and call to the compare function but it doesnt work:

Code:
qsort (idptr, 1000, sizeof(string), compare);

Code: int compare (const void* a, const void* b){
if (*(char*)a >= *(char*)b)
return 1;
else
return -1;
}

Or in more lamens terms say we have the following stuct instances:
Key: Token:
1 Hello
2 World
3 How
4 Are
5 You

Then I need those structs to order according to token (alphabetically, increasing or decreasing depending whats in our compare function).....

View 3 Replies View Related

C :: Passing A Pointer To Array Of Structs

Nov 20, 2013

what I am trying to do is to pass to a function the address of an array of structs, so I can later modify the items within the struct, within the array

Code:
typedef struct { //A struct of name auctionint bidder;float bid;} auction;
void myFunction (auction * auctionItem[]){(*aucItem[x]).bid = y;(*aucItem[x]).bidder = z;}
int main(){auction theItems[10];
myFunction(theItems);} Where x, y, and z can be any number.

When I try to run my code the IDE (I'm using Code::Blocks 12.11) does not give me any errors, but it does give me a warning:

warning: passing argument 3 of '<function name>' from incompatible pointer type [enabled by default]

and the note:

note: expected 'struct <struct name> **' but argument is of type 'struct <struct name> *'.Also, when I run the program, it will crash and return garbage.

View 6 Replies View Related

C :: Adding Elements To Array Of Structs

Mar 6, 2015

I have a structure product_array *pa that contains a pointer *arr to an array of structs and count that adds 1 when a new product is added (set to NULL initially). I have to write a function which adds a new product entry to that array. One product entry has *title, *code, stock and price parameters. The array is dynamically allocated and I’m supposed to:

1. Reallocate space for array.
2. Update product_array.
3. Initialize it.

Also, code should be truncated to 7 characters.Products can be added multiple times, so the initial size is unknown.

Code:

void add_product(struct product_array *pa, const char *title, const char *code, int stock, double price)
{
for (int i = 0 ;; i++){
pa->arr = realloc(pa->arr, sizeof(struct product_array));

[code]....

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved