C++ :: Reaching Data Of Different Array Using Pointer In Loop

Jul 31, 2013

I have 3 arrays named: abee1 , abee2, myarray.

I have loop that want to call abee1 , abee2 one each time and copy myarray into it . It means that I reach the name of abee1 to abee2 in the loop for doing this. I made the name of each one by using strings ( abee1 , abee2) in the loop. In the last I want to copy the myarray into the abee1/2 using pointer.

I do not know what to do after the line "string arrayname = "abee" + String;".

#include<iostream>
#include<string>
#include <sstream>
using namespace std ;
int abee1 [4] [4] ;

[Code] ...

View 1 Replies


ADVERTISEMENT

C++ :: Reaching Array From Other Function?

Nov 17, 2013

I have to make an array in one function (NOT MAIN!!!) that does not return the array, but just the average value of its elements to main.

Then, I have to use elements from the same array to calculate other values form them (can also be in main).

How can I reach array from function f (not main) if the function f is not returning array?

View 7 Replies View Related

C++ :: Putting Data Into Array Using For Loop From Input File?

Jul 7, 2014

I'm trying to pass several integers into the array idAr[10] from an input file using a for loop and then having them output on the console. The output should look like this:

#include <iostream>
#include <fstream>
using namespace std;

[Code].....

View 4 Replies View Related

C++ :: Vector Reaching Its Maximum Size

Feb 5, 2014

My program enters the size of the vector from the user and then creates a vector of vectors (lets say SIZE1). In addition the user enters the number of vector of vectors he needs (lets say SIZE2) as follows:

class Vectors {
// member functions goes here
private
vector<vector<int>> vectors;
vector<int>::iterator it;

[Code] .....

With a few calculations and insertions to my vector (vector of vectors)... the program works fine and gives me the results...

However, with huge calculations and insertions the program stops working and gives me this message

"Unhandled exception at at 0x770DC41F in Test.exe: Microsoft C++ exception:std:bad_alloc at memory location 0x001CEADC"

Thus, it seems that the vector reached it's maximum size... I tried to use reserve() but did not work

I read that "By default, when you run a 64-bit managed application on a 64-bit Windows operating system, you can create an object of no more than 2 gigabytes (GB). However, in the .NET Framework 4.5, you can increase this limit"

What do you think would be the best option for me to do (note my program is very long and complex)(I'm currently using Microsoft Visual Studio 2012 32Win application):

1. convert my program to the .NET Framework (C++)

2. convert my program to C# in case c#

3. do any settings on my computer (my workstation has a 3.6GHZ xion processor with 32RAM

4. convert to another version of C++ that does not have any restriction on the size of the array (if available)

Please note that I never worked neither with the .NET framework nor C#

View 3 Replies View Related

C++ :: Reading Input - Reaching End Of File Too Early

Apr 24, 2014

I'm reading input from a file using an ifstream. The program doesn't seem to read any characters but continually returns eof no matter what file I use. Even when I do ifstream.open(filename) nothing changes. This program is intended to be the lexical analyzer of a compiler, so symbols are returned to indicate what kind of input is in the file.

Here's the code:

#include "lex.h"
#include <cstlib>
#include <iostream>
#include <fstream>
//other functions
char Lex::getChar(){
ifstream input;

[Code] ....

I'm sure I messed up the initialization of the ifstream somehow.

View 1 Replies View Related

C++ :: Loop To Read In Data And Check Data

Oct 8, 2013

struct receivers
{string fname, lname, team,;
int receptions, yards, TDs, longest,rec20, fumbles, yac, firstdown;
double, averagepergain, averageperrec
}

View 2 Replies View Related

C++ :: Can A Pointer Be Incremented In A For Loop?

Nov 3, 2013

I have a simple question. Can a pointer be incremented in a for loop? In my program, I use a pointer variable and use it in a for loop like such:

for(*var1 = 1; *var1<100 ;*var1++)
int function(int* var1) {
if(*var1 blah blah)
{do blah blah}
if(blah)
{blah}
}
}

The variable is not incremented by the ++ even when the if conditions are not met and the loop shouldn't break. the *var1 stays at 1.

View 3 Replies View Related

C/C++ :: Comparing Pointer To Variable As While Loop Condition

Aug 11, 2014

I'm writing a delete function for a linked list, and I'm having issues with this bit of code:

void deleteNode(int data){
node* del = NULL;
t = h;
n = h;
while(n != NULL && n->_data != data){
t = n;
n = n->next;
}
}

Or more precisely, this portion:

&& n->_data != data

n is my new node variable, _data is the storage variable in the private section of my class, and data is the information being searched for that needs to be deleted. Everything works without this section of the code. My assumption is that n->_data is somehow wrong, but I don't see how. I've tried everything I can think of- using parenthesis, using the variable rather than the pointer, I've tried expressing the pointer in a different way, I've tried using my t variable rather than n, and I've found examples online that use this exact same expression without any issues.

View 2 Replies View Related

C++ :: Infinite Loop For Pointer Variable In Linked List

Jan 14, 2014

I am getting and infinite loop for loop pointer variable current which points to head and is incremented by current->next in while loop. I use the pointer variable the same way in my display routine and it works. Here is listing of code.

#include "stdafx.h"
#include<iostream>
struct node{
int data;
node *next;

[Code] .....

View 1 Replies View Related

C :: Access Element Of Array Through A Pointer To A Pointer

Dec 25, 2013

i have been fiddling with pointers but I don't understand how the proper syntax is written when I want to acces an element of an array through a pointer to a pointer...The code is all mostly just random bs for learning purposes. I marked the problem "// THIS LINE"

Code:

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DATA 100
int find_average(char *iden, ...) {

[Code]...

View 2 Replies View Related

C :: How To Declare Two Dimensional Array Using A Pointer To A Pointer

Jul 16, 2013

This is a sample program that declares a Matrix as a structure with an array as a pointer to a pointer. The following program is supposed to store a matrix in the structure "_Matrix" and later print the matrix just entered but it fails giving me a "segmentation fault". The sample code is given below

Code:
#include <stdio.h>
#include <stdlib.h>
struct _Matrix {
int row_size;
int col_size;
int **mat;

[Code] ......

View 1 Replies View Related

C++ :: Looping Through 2D Array Using Pointer To Pointer

Jan 3, 2013

I have the following code :

Code:
#ifndef TDYNAMICARRAY_H
#define TDYNAMICARRAY_H
namespace Massive {
template<class T>
T **AllocateDynamic2DArray(int nRows,int nCols)

[Code] .....

I wish to know how to traverse or loop through a dynamic 2D array using pointer to pointer as returned by the code above. Like I would in a static T[20][20] 2D array.

View 8 Replies View Related

C++ :: How To Write A Data Loop

Feb 14, 2013

I need to write a data input for one of my class assignment. Would this be considered a data loop?

while (cin >> number)

View 4 Replies View Related

C++ :: Using Loop To Read In Data From A File?

Oct 30, 2013

My function is only reading the first line of numbers even though I have the loop to keep returning to it in main(). How do I have it read from the file until it runs out of data to retrieve?

File being read

90 83 94
72 65 81

File being sent the data(Only the second average is correct, working on the first)

Student Information
Student IDGradeAverageLetter Grade
1895612
90 83 94
289
90 83 94

#include <fstream>
#include <iostream>
using namespace std;
ifstream infile;
ofstream outfile;
void grades(int&,int&,int&); // Grades from the input file
int myavg(int,int,int); // Calculates the Average of the 3 grades

[Code] .....

View 8 Replies View Related

C++ :: Returning A Pointer To Encapsulated Data Member

Jun 22, 2014

So, I've got this class in SDL Player that has, among other things, an SDL_Texture* to hold an image that represents the player on the screen. I'd assume it's good practice to do get() and set() functions for the class; but because textures are handled via pointers, when I write a get() function I end up returning a pointer to an internal resource; which isn't good practice I hear as it "breaks" encapsulation.

Find my code below:

#ifndef PLAYER_H
#define PLAYER_H
#include "SDL.h"
#include "SDL_image.h"
#include "CTexture.h"
class Player {

[Code] .....

View 1 Replies View Related

C++ :: Pointer-based Data Loading From A (text) File

Dec 4, 2013

I have two classes, productListType and buyerListType, that are each basically linked lists. To create the linked list for an object of productListType, I successfully wrote a non-class function createProductList to read the product data from a text file. The class definition for buyerListType has a data member productBoughtRecord that is of type productListType to aggregate the details of the products purchased by a particular buyer during transactions. I decided to make productBoughtRecord a pointer since the contents of this data member would wax and wane over the course of several transactions, depending on the amount and frequency of payments made by the buyer. I have provided a rough sketch of the class below:

class buyerListType
{
public:
setCustomerInfo( ....., productListType* p);
.
.
.
private:
productListType* productBoughtRecord;
.
.
};

I'm similarly trying to write a non-class function createBuyerList to load the record of customers from a text file. How do I determine what the value of the formal parameter p in member function setCustomerInfo is, in order to be able to set the data member productBoughtRecord? take into consideration that an object of class buyerListType would have multiple buyers with varying amounts of products purchased.

View 11 Replies View Related

C++ :: Memento Pattern Failing Because Of Pointer Data Members

May 24, 2014

All the undos and redos were working fine until the pointer data members came into the picture. The problem is that the values pointed to changed, but the pointers themselves did not. So restoring gives the same pointers but they still point to the new values. I think the solution is that the classes that are the pointer data members themselves need their own Mementos (which would be a lot of work because there are many data member pointers in my program). Is that the only approach?

Here is a sample code to show what I'm talking about. You can compile and run the program to see the problem it has restoring values of pointer data members:

#include <iostream>
#include <string>
#include <memory>
#include <vector>
#include <list>
#include <cstdlib>
#include <ctime>
class FoodHistory {

[Code] ....

View 2 Replies View Related

C++ :: WHILE Loop Error / In Streaming Data File (with Functions)

Nov 2, 2014

I have a working lab project with a loop error. Code posted in second post. Here's the requirements:

*
- program has to be able to handle file failure
- must loop (prefer to use a while loop here), read in the data, calculates values, etc. and then output the results.

*
The input data file includes

1. a 1 or a 0 indicating that there is a set of employee data following

2. the hourly rate of the employee

3. the humber of hours worked

4. the number of dependents

5. a 1 or a 0 indicating whether the employee is full time (1=full time).

Problem: At first I wouldn't loop back... so I moved the curly brace above the return0;

View 2 Replies View Related

C# :: Add Data Grid View Column To Listbox Using For Loop

Apr 18, 2014

I need to take a single column and all the rows in the column from a datagridview to a listbox using a for loop. My code is giving me no errors just not showing any data in the listbox.

private void frmProject6_Load(object sender, EventArgs e) {
// TODO: This line of code loads data into the 'enrollmentsDataSet.Enrollments' table. You can move, or remove it, as needed.
this.enrollmentsTableAdapter.Fill(this.enrollmentsDataSet.Enrollments);

[Code] .....

View 1 Replies View Related

C++ :: Sorting Data Based On Some Metric - Function Pointer Syntax

Nov 20, 2014

I have a piece of code that sorts data based on some metric. The some metric is something I now want to make flexible so I can easily switch between and compare metrics. To do this, I want to pass the function to use as a parameter to the method that does the sorting (and other stuff). However, I'm having problems figuring out the syntax. Also, I think my [temporary] organization of code is violating a lot of basic code design principles.

To make the function pointer passable, I defined the "typename" in the header where the function is located (it is part of a struct, "Data"):

// Below the struct definition of Data
typedef double (Data::*CostF)(unsigned l, double W) const;

The two example functions I want to use are defined in that struct ("Data"):

// Inside the struct definition
inline double someExampleCost(unsigned l, double W) const {
// Returns some basic calculation
}

The function that uses it is part of a different class (that holds a reference to the first class, in case that matters; I feel like I'm doing something odd here, because I'm binding a member function in the definition/passing, but never referencing the object). It looks like this:

// Inside another class ("Foo")
inline void DoSomeStuff(double& ECost, double& TCost, CostF cost) {
// Irrelevant stuff here
std::sort(vector.begin(), vector.end(), [&](unsigned a, unsigned b){
return (*cost)(a, W) < (*cost)(b, W);
});
// More irrelevant stuff here
}

The error shown is "operand of "*" must be a pointer". If I remove the '*':
[code]return cost(A, W) < cost(b, W);

the error becomes: "expression must have a (pointer-to-)function type."

The call to this function is, currently, just in the main function, as I'm just testing before I wrap it into real code. It looks like this:

// In main
Foo bar; // Make an object of the struct that has the "sorting" function
CostF costFunction = &Data::someExampleCost;
// Bind to the Cost function bar.DoSomeStuff(varA, varB, costFunction);

This bit shows no errors by itself. So, my questions:

a) Clearly I'm missing the insight into Function Pointers. I'm comfortable with regular pointer stuff, but I can't wrap my head around FPs, partly due to the awkward syntax.

b) I'm very uncomfortable with the fact that I'm binding a member function of a class, but never try to reference an actual object of that class. This is probably a big part of why it's not working, but I can't seem to bind a function belonging to a specific object. I thought of doing

// In the main again
Data d; // Construct the object, which contains big lookup tables
Foo F(d); // Construct the object, which only holds a reference to a Data object
CostF costFunction = &d.someExampleCost; // Bind to the Cost function of that object

but that doesn't work ("a pointer to a bound function may only be used to call the function").

View 4 Replies View Related

C++ :: Write Data Into Files In A For Loop - Debug Assertion Failed?

Apr 14, 2012

In my program I open two files and write data into each, and close them. this is done in a while loop.

int main() {
while() {
myFile = fopen("file1.txt","w");
myFile2 = fopen("file2.txt","w");
// write data into files using fprintf() in a for loop
fclose(myFile);
fclose(myFile2);
}
}

However, after some iterations the fopen() for myFile2 fails.

Debug assertion failed!
Program....
File ......srcfprintf.c
Line 55

Expression (str != NULL)

I have not been ableto locate the bug in the code that is causing the failure.

View 7 Replies View Related

Visual C++ :: Use For Loop To Read Certain Data From Txt File And Print To Console

Sep 28, 2014

I'm new to programming and i'm trying to do a certain task. I want to use a for loop to read certain data from a txt file and print them to a console. I'm trying to read student names and their grades.

Something like
3 // 3 represents the number of students.
George 97
Sarah 70
Maya 88

The data may vary but it's always in this format.

View 14 Replies View Related

C/C++ :: Display Data From File Into 2D Array - Print Closest Bmi From Data

Mar 28, 2015

#include <iostream>
#include <fstream>
using namespace std;
int main() {
int r = 0;
int c = 0;
int num[17][15] = { 0 };
[Code] ...

// Here is my code for displaying the data from the text file into a 2d array and height next to it, but I am not able to diaplay the height from 60 to 76 next to the row of the 2d array, as shown in the table below. This is my program:

Recently the health authorities have listed a new way to calculate the body mass index (BMI) of an individual.
Values of 20 – 24 are classified as normal, 25-29 as overweight, and 30-34 as heavy.

The table below is a portion of a typical BMI table.

BMI:202122232425262728293031323334
Height:
60102108113118123128133138143149154159164169174
61106111116122127132138143148153159164169175180
.
.
.

MY task is to write a program that does the following things:

1.Produce the table by reading in the data from bmi.txt (on Moodle) into a 2-D array.

2. Display the table neatly on the screen, with row and column headings. The BMI should go from 20 to 34. The height should go from 60 to 76 inches.

3. Prompt the use for their height (in inches) and their weight.

4. Make the program print the closest BMI.

If their weight is lower than the values on the table, use 20 as the BMI.
If their weight is higher than the values on the table, use 34 as the BMI.

5.Loop the program so the user can find more BMI’s.

View 7 Replies View Related

C++ :: Using Loop To Process Array Of Characters Starting From Beginning Of Array?

May 3, 2014

Assume you want to use a loop to process an array of characters starting from the beginning of the array. You want the loop to stop when you read the null terminator character from the array. Fill in the loop test condition that will make this work correctly.

index = 0;
ch = array[index];
while ( _____________________________)
{
// process the character
index++;
ch = array[index];
}

View 1 Replies View Related

C :: Cast Of The Pointer To Array

Dec 7, 2013

In my refference book I have got a example with a part saying to access the a[4][0] element of the array (named a) with pointer this can be written:

*((int*)a+4)

I wonder if the cast is really required. The book says it is required so that the pointer arithmetic can be done properly. However I am not sure about it. When I work with pointers defined by myself I don't use casts similar to this one. Is there a difference between a self defined pointer and array name?

View 14 Replies View Related

C++ :: Copy Array Into Pointer

Jun 20, 2013

I have written this code, and at first glance it does what I want, however I am worried that

a) I am overwriting the array that is apssed from chord.getPattern()
b) Im getting a memory leak that I want to get rid of, and
c) is there generally a /what is the neater way to do it:

Code:
uint8_t* ChordBuilder::invert(uint8_t count, Chord chord) {
temp = chord.getPattern();
chord.invert(true);
//TODO count is how many times to invert. Moves root aswell however

for (uint8_t i = 0; i < count; i++){

[Code] ....

temp is a member variable of ChordBuilder - and is expressed as: Code: uint8_t* temp; I dont want the pattern that chord stores, and passes with getPattern() to change - I fear it is at the moment?

I would rather not use the "new" but I cant think how to get rid of it, however Im not sure where I would need to put the "delete"?

View 7 Replies View Related







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