C :: Setting Values For Array Of Structs

Dec 18, 2013

How do I set all of these string and double value(s) to 0?

Code:

#define MAXcharacters 12
#define MAXaccounts 100
struct Records {
char userid[MAXcharacters + 1];
char password[MAXcharacters + 1];
double balance;
};

[code]...

View 3 Replies


ADVERTISEMENT

C :: Setting Values To Pointers

Apr 5, 2014

This function below takes a pointer as an argument. What I expect to happen is, since expr++ has higher precedence than *expr, that is, the primary expression operators have higher precedence than the unary operators, pointer arithmetic should occur where we increment to the second address pointed to by dbuf, and then we should dereference the value at that address. Given that logiv, when i print dbuf[3] it should print the value pointed to at the 4th address in dbuf. However, the value it returns is 0x0 not 0x3. Why doesn't it dereference the value 0x3?

Code: void dfill(unsigned char *dbuf)
{
dbuf = (unsigned char*)malloc(4);
memset(dbuf, 0, 4);
*dbuf = 0x0;
*dbuf++ = 0x1;
*dbuf++ = 0x2;
*dbuf++ = 0x3;
printf("dbuf val: 0x%x
", dbuf[3]);
}

View 1 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++ :: Setting Values Using A Text File?

May 19, 2014

in the following code how would i set a txt file up to where i can have say name = whatever in it and the program will find it and set it to the corresponding global value.

#include <fstream>
#include <iostream>
#include <string>

[Code]....

View 1 Replies View Related

C++ :: Simple Image Output - Setting RGB Values And X-Y Coordinates

May 21, 2012

I'm wanting to create an image, all I need is to be able to set RGB values and X-Y coordinates, I'm not wanting to read or manipulate images. Any easy to use library or another simple method of doing this?

View 1 Replies View Related

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 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++ :: 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++ :: Setting Up Grid For Array And User Initialization Of Array

Apr 7, 2014

Trying to get code to look like

Row 1: ^^^
Row 2: ^^^
Row 3: ^^^

const int siz = 3;
for (int t=0; t<siz;t++) {
cout << "Row" << t+1 << ":";
cout << endl;

[Code] ....

Also was wondering if i wanted to separately ask the user to choose a location to add a char who would i do that ?

( im thinking * cin << arry[][]; ? not sure but ask for them separately
array []1
array []2
= array [][]

View 3 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

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++ :: Setting Array To Int With Value

Mar 1, 2013

Lets say i have an array with the values 1, 5, 9, and 3. is there anyway to make this so i can have an int with the value 1593 based on those numbers in the array?

View 8 Replies View Related

C :: Array Of Structures And Passing Pointers To Structs

Feb 24, 2013

Background: I'm writing a convolutional encoder (and decoder, eventually) for a microprocessor (PIC24), for which I'm using structs and pointers to move from state to state. So far as I'm aware, everything I'm using in the PIC involves nothing other than ANSI C.

I have a little experience with structures, having written a linked-list program for a class a couple years back, but nothing since and never used structure arrays. I have the feeling I'm missing something basic here, which is what's so frustrating. The most confusing error (and I suspect the root of most of them) is the 'state undeclared', which I just can't figure.

The errors I'm getting are:

encoder.c:11: warning: 'struct memstate' declared inside parameter list
encoder.c:11: warning: its scope is only this definition or declaration, which is probably not what you want
encoder.c: In function 'state_init':
encoder.c:22: error: two or more data types in declaration specifiers
encoder.c:25: error: 'state' undeclared (first use in this function)
encoder.c:25: error: (Each undeclared identifier is reported only once

[Code]....

Code:

Code: //Includes
#include <stdlib.h>
//------------------------------------------------------------------------------
//Creates state machine and passes back pointer to 00 state
void state_init(struct memstate* startpoint)
{
extern struct memstate
{
char output0; //output if next input is 0

[code]...

NB: I'm aware that at the moment, this code will do nothing except spin round that do-while loop. Once it's actually compiling I'll drop in some simple button-based test code so it'll check for the correct output.

View 9 Replies View Related

C :: Reallocate A Dynamically Allocated Array Of Structs

Jul 15, 2013

Code:

void readFile(struct course *d, char* filename){
FILE* fp;
char buffer[100];
int i = 0, array_size = 100;
struct course *temp;

[code]....

I will be using this to read data from a file. I start with an array of 100 structures being passed to the readfile function. Once it reads 100 lines (i == array_size), I want to double the array size until I have finished reading the file.

Two questions.

1)My initial thought was that I needed to keep track of the lines read with my variable, i. However, is there a better way?

2)My program is crashing right now at the call to double_array_size function. What is wrong with my code? Never dealt with dynamically allocated array of structures and functions.

I read online that I should change my code in the following manner.

Code:

void readFile(struct course *d, char* filename) {
FILE* fp;
char buffer[100];
int i = 0, array_size = 100;
struct course *temp;

[code]....

I can paste the "error messages" if you like, but it is a page full of stuff I have never seen. glibc detected, Backtrace, Memory Map, and a bunch of numbers and hexadecimal stuff like addresses.

View 12 Replies View Related

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 :: Setting 1D Pointer To Array

Oct 27, 2013

I have the following situation:

Code:
void myFun(float *pfMyPtr) {
float Val[] = {0.234, 0,432, 0.322, 0762, 0.984};
pfMyPtr = Val;
}
int main() {
float *pfPtr;
pfPtr = (float*) calloc (5,sizeof(float));
myFun(pfPtr);
}

I would like pfPtr to contain the values of array Val. What am I missing here?

View 5 Replies View Related

C++ :: Setting All Elements Of Array To 0

Sep 18, 2013

I'm trying to set all the elements of my array to 0 but nothing seems to be working.

.h
#ifndef ServerGroup_h
#define ServerGroup_h
class ServerGroup {
public:
ServerGroup(const int&);

[Code] .....

I want to set each element of the array in servers to 0 based on what is passed into size by numArray.

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 - 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/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++ :: Setting Entire Array To NULL

Dec 12, 2013

Okay so I've declared an array like this.

Foo *Blocks[100][100][10000] = {0};

And as far as my understanding goes This creates an array with every member set to NULL.

And then later on in my code some of these get given values using:

Blocks[a][b][c] = new Foo;

And then when I want to unload I would think that I could just go

Blocks = {0};

But obviously this doesn't work.

So I was wondering if there was a way of doing This, with out creating a loop and changing every one to NULL manually.

View 8 Replies View Related

C :: Setting Character In Malloc Char Array

Oct 16, 2013

I am attempting to change a character in a character array.In the code below, there are three attempts to do this. Only the first one will succeed. The last two both segfaults. If I understand correctly, str_one is declared in the heap, and could therefore be manipulated; and in contrast, str_two is declared in the stack and is therefore immutable, thus the segfault, when update it is attempted. However, I understand that using malloc, one is able to assign a pointer and allocate space in heap memory. Thus, I should be able to manipulate the assigned variable str_three. Doing so, however, results in a segfault.

Code:

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

int main (int argc, char const* argv[])
{
char str_one[4092] = "This is string number one";
char * str_two = "This is string number two";

[Code]...

View 2 Replies View Related

C/C++ :: Compare Values Stored In First Array To User Inputted Values In Second Array

Oct 19, 2014

Goal: Write a program that compares the values stored in the first array to the user inputted values in the second array.

In order to fix this error: [URL]...

I had to change my array initialization to one with a star in front of it:

char a1[]={"a","d","b","b","c","b","a","b","c","d","a","c","d","b","d","c","c","a","d","b"};
to:
char *a1[]={"a","d","b","b","c","b","a","b","c","d","a","c","d","b","d","c","c","a","d","b"};

I also changed my 2nd array to one with a star in front of it: char *a2[20];

What does this mean exactly? Putting a star in front of an array?

Also, I am now getting an "unhandled exception" when I try to get input for my 2nd array:

cin>>a2[i];

View 3 Replies View Related

C++ :: Create Array Of Playing Cards / Assign Values And Suits Then Shuffle The Array

Nov 24, 2014

I got this program to create an array of playing cards and assign the values and suits and shuffle the array. I'm at the point where I need to output the cards but I need to burn the first card by making it output "**" instead of the card. my cards[] is a constant so I can's assign the first card as such.

void showCards(const int cards[], int numCards, bool hideFirstCard) {
if (cards[0]) {
hideFirstCard=true;
cards[0] = '**';
} for(int a = 0; a <= numCards; a++) {
cout >> showCard(cards[a]);
} }

View 1 Replies View Related







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