C/C++ :: Vector Of Class Outputting Garbage Values
Jun 21, 2014
I have a class that uses std::vector. I push back class objects onto the vector but when I try to cout the data members (with get functions) I get garbage values.
Here is the class:
#ifndef JOBS_H
#define JOBS_H
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
using namespace std;
class Jobs{
[Code] ....
I know I am missing the implementation of several functions but I'm just testing my vector to see if it is working and it isn't. The getBlockValue() function should output 0 for each job. When I push back one object the output is 0. When I push back 2 objects the output is 0. However when I push back 3 objects I get random values. Why is this? I thought vectors were dynamic?
View 8 Replies
ADVERTISEMENT
Apr 8, 2015
I've been working on a homework assignment that randomly generates integers and populates them into an array (array1). The program is then supposed to:
1.) copy those values to a second empty array (array2)
2.) sort the values already in array1 (using an inline function)
3.) enqueue the unsorted integers from array2 into a heap vector
4.) a third empty array (array3) is supposed to be populated with those unsorted integers (by dequeuing them from the heap), sorted in reverse order.
But no matter what I do, I always get garbage values like these:
I've tried using both a standard random number generator:
array1[i] = rand()%100+1;
And the d_random.h file my instructor gave us, but nothing works.
Here's the code from all 3 files:
HeapTester.cpp
Code:
#include <iostream> // Provides cin, cout
#include <cstdlib> // Provides EXIT_SUCCESS, rand, srand
#include "d_random.h"//Provides random number generator
#include "Heap.h"
using namespace std; // Use C++ Standard namespace
//Elements in each array.
const int arrayLength = 15;//100;
[Code] ....
Why I'm getting those garbage values?
View 6 Replies
View Related
Jan 29, 2015
So i have this code:
if (get_brick_at(Position(row, column)) == NULL)
Where get_brick_at is defined like this:
>Brick& get_brick_at(const Position & p) {
return board[p.get_row()][p.get_column()];
}
Now, of course this does not work since the compiler cannot convert from long int to a Position. So how do i know if the return value of get_brick_at is garbage?
I also have a couple of other situations where i want to return some kind of NULL-like value under certain circumstances.
My question is: How do i handle situations like that?
View 5 Replies
View Related
Dec 4, 2013
This is my code: [tag]
Code:
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
[Code] .....
View 4 Replies
View Related
May 13, 2013
I need to create a class vector as a template and define operations on vectors.
And this is what I made.
#include<iostream>
using namespace std;
template<class T>
[Code].....
View 2 Replies
View Related
Apr 26, 2014
I have my main.cpp like this:
#include <iostream>
#include "curve1.h"
#include "curve2.h"
using namespace std;
int main() {
Curve1 curve1Obj;
Curve2 curve2Obj;
[Code]...
Base class Score has two derived classes Curve1 and Curve2. There are two curve() functions, one is in Curve1 and other in Curve2 classes. getSize() returns the value of iSize.
My base class header score.h looks like this:
#ifndef SCORE_H
#define SCORE_H
class Score {
private:
int *ipScore;
float fAverage;
int iSize;
[Code]...
You can see that I have used curve1Obj to enter scores, calculate average and output. So if I call getSize() function with cuve1Obj, it gives the right size that I took from user in enterScores() function. Also the result is same if I call getSize() in score.cpp definition file in any of the functions (obviously).
.....
The problem is when I call curve() function of Curve2 class in main (line 23) with the object curve2Obj, it creates a new set of ipScore, fAverage and iSize (i think?) with garbage values. So when I call getSize() in curve() definition in curve2.cpp, it outputs the garbage. .....
How can I cause it to return the old values that are set in curve1.cpp?
Here is my curve2.cpp
#include <iostream>
#include "curve2.h"
using namespace std;
void Curve2::curve() {
cout << "getSize() returns: " << getSize() << endl; // out comes the garbage
}
Can I use a function to simply put values from old to new variables? If yes then how?
View 3 Replies
View Related
Jun 9, 2013
I want to add this piece of code to a project. I have created a vector from values entered by a user. I want to then subtract each value from each other from the beginning to the end. For example if a user enters 10,4,3 the program will subtract the values in that order 10 - 4 - 3 and output 3 as the solution. I figured how to do it with addition but can't get it to output for subtraction.
#include <iostream>
#include <vector>
using namespace std;
[Code].....
View 5 Replies
View Related
Dec 14, 2014
im passing a vector of a class to a function of another class. But i cant access the data on the classes inside the vector.
Something like that:
class CDummy{
...
public:
string m_name;
[Code].....
Im creating the vector on main() and using push_back with a pointer to an initialized CDummy instance
View 5 Replies
View Related
Apr 17, 2014
Why does this generate an error:
Code: std::copy( vec.begin( ) , vec.end( ) , std::ostream_iterator<double>( " " , std::cout ) ); whereas this works fine:
Code: std::copy( vec.begin( ) , vec.end( ) , std::ostream_iterator<double>( std::cout , " " ) ); I know I can use this instead:
Code: for ( std::vector<double>::iterator it1 = vec.begin( ); it1 != vec.end( ); ++it1 ) std::cout << " " << *it1;
View 5 Replies
View Related
Mar 30, 2013
I am trying to assign a list of values to a vector:
vector<string> words;
words[] = {"one", "two", "three"};
This does not work. How can I accomplish it?
View 5 Replies
View Related
Mar 17, 2014
I wud like to delete multiple values from a vector using indexes as my values r not fixed they may change so using indexes i want to delete
For example: vector<int> v={1,2,3,4,5};
I want to erase 3,4,0 indexes
So resulting vector is 2,3
View 6 Replies
View Related
Apr 5, 2013
I'm trying to print values from a vector of a struct and I don't really know how to do that. Here is a simple code that I have for now to test how to add data to the vector then attempt to print it out.
#include <iostream>
#include <vector>
#include <string>
#include <deque>
#include <fstream>
using namespace std;
struct Employee//employee data
[Code]...
View 2 Replies
View Related
Apr 23, 2013
I cannot wrap my head as to how to access my enum class Azimuth.
Code: #ifndef BOUSOLE_H
#define BOUSOLE_H
#include <iostream>
#include <string>
#include "StringHandler.h"
class Bousole{
[code]...
And here is where I am trying to access my enum for testing/understanding purposes
Code: #include "Bousole.h"
using namespace std;
int main (int argc, char *argv[]){
cout <<"Start bousole" << endl;
Bousole b;
[Code] ....
View 6 Replies
View Related
May 4, 2013
I can`t seem to store multiple values into my vector when a user keys in an input. Lets say a user inputs 1,2 as the first set and later keys in 3,4.
My vector gets overridden by 3 and 4. I'm trying to pass these values into a function to store them into a vector and use them in the function.
Below is my code snippet
int reconstructSecret(int x, int y) {
int getX,getY;
getX=x;
getY=y;
vector<int> myXCordVec;
vector<int> myYCordVec;
[Code] .....
View 6 Replies
View Related
Nov 28, 2012
I am writing a program to play rock paper scissors, and I am trying to get a vector to store the result of a given round of play, and output it as a declaration as to who won. I can't figure out how to store the values in the vector properly, and how to use the vector as an argument to output the result of play.
Code:
#include <iostream>
#include <iomanip>
#include <ctime>
#include <vector>
#include <cstdlib>
#include <string>
using namespace std;
const string name[4] = {" ", "rock", "paper", "scissors"};
const string roundResult[3] = {"computer win", "player win", "tie"};
[Code]....
View 1 Replies
View Related
Dec 29, 2013
I am trying to seed a vector with 52 values ranging from 1 to 52. I already have the code written to seed the vector bu how to keep from repeating the same values from being seeded. This is for a five card stud game.
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<vector>
#include<ctime>
using namespace std;
[Code] ....
View 3 Replies
View Related
Dec 10, 2014
I have two classes, one class requests another class to populate a vector with some data using getAvailableStreams() method:
vector<AutoRefPtr<ManifestStream> > availableStreams;
std::cout << "now asking for streams." << std::endl;
manifest->getAvailableStreams(&availableStreams);
std::cout << "got so many streams: " << availableStreams.size() << std::endl;
The other class passes another pre-populated vector back:
virtual RESULT getAvailableStreams(std::vector< AutoRefPtr<ManifestStream> >* availableStreams) {
availableStreams = static_cast<std::vector< AutoRefPtr<ManifestStream> >* >(myStreams); //myStreams contains 1 stream
I'm also adding a stream here directly just for testing purposes:
ManifestStream stream2;
availableStreams->push_back(AutoRefPtr<ManifestStream>(&stream2));
std::cout << "calling getAvailableStreams. Giving so many streams: " << myStreams->size() << ", " <<
availableStreams->size() << std::endl;
return result_OK;
}
If I run the code, I can see the following logs:
Now asking for streams. Calling getAvailableStreams.
Giving so many streams: 2, 2
got so many streams: 0
Why am I getting 0 streams when I exist the method? I just had 2 streams in availableStreams while I was inside the method, where have they disappeared?
View 6 Replies
View Related
Mar 24, 2014
I'm trying to increment the values in a vector, not the vector size, based on variable input. Basically I have a vector of size 10, and all of its values are initialized at zero. The program counts the frequency of numbers 0-9 in a four digit user input. This is what I have (I want it to work so badly but the compiler says that I'm using a pointer to a function used in arithmetic):
for (int i=0; i < num_slots; ++i) {
++guess_frequency[guess[i]];
}
I just want to know if you can increment values within a vector:
E.g.
change
0 0 0 0 0 0 0 0 0 0
to
1 0 0 0 2 0 0 1 0 0
View 6 Replies
View Related
Jan 21, 2015
I have a function that rotates a vector 90 degrees. Only problem is, i never get negative values. For some strange reason, no matter what i do, the value of x is positive. And even if the x is negative. Once i put it into direction (struct with 2 values x and y) it turns again to positive. Im using VSC++2013 ultimate. At first direction was SDL_Point, so i thought it was SDL problem, but now it seems its something else.
if (c == '-') {
int x = direction.y;
x *= -1;
int y = direction.x;
direction = { x, y };
}
View 8 Replies
View Related
Aug 12, 2012
I'm trying to get my program to read a series of comma delimited values from a file into a vector. However, I am unsure how to actually go about doing this. I've posted my best guess below but it's really just a stab in the dark and I always receive a compiler error.
Code:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
vector <string> v_input;
int main() {
ofstream fout ("Insert File Path Here.txt");
fout << "4, 8, 15, 16, 23, 42";
[Code] ....
View 3 Replies
View Related
May 1, 2013
Code:
// Lab0Inventory.cpp : Starter lab
//Anastasia Glyantseva
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <iomanip>
#include <sstream>
[Code]....
I can't figure out why my makeString gets called first and the pString its called with contains garbage. I want my allocateMem to get called first, but my program is not going to that. What is wrong with the order of my code?
View 3 Replies
View Related
Mar 21, 2013
Im programming client/server app that client provide the file name then the server send it to client then the client will save it ..this is part of code in client
Code:
char buffer[1024];
printf("FIle is being downloaded ...
");
printf("%s
",buffer);
}
[code],...
So i have 2 problems ::
1st one is when i write to file the file permission i cant define it with data type mode_t ,so the file does not open at all after creation...
2nd one is: the data in buffer is less than 1024 ,the data wrote to buffer but with garbage data . How to make the file read only the real data with garbage ??
View 5 Replies
View Related
Mar 23, 2013
I'm working on my program that takes input of the employees' first and last name, their payrate, their deferred from check and also the amount of hours they have worked which then the gross is calculated and also the taxes are calculated by an external function. In the program design it is necessary to put arrays which I have done, but when i compile I receive warning messages
Code: warning: format '%s' expects argument of type 'char*', but argument 3 has type 'double'
Warning: format '%f' expects a matching 'double' argument [-Wformat] which I believe is causing my program to just give me garbage when I run it. What do those warnings mean?
Code:
/* Name: Arturo
Date: 03/22/13
Purpose: To learn
*/
extern void calculate taxes(float gross,float deferred, float *ft, float *st, float ........i);
void ovtHrs(float *hrs_wrk, float *ovt_hrs, float hrs, float *gross, float payrate);
void netPay(float gross, float deferred, float ft, float st, float ssi, float *net);
[Code] .....
View 9 Replies
View Related
Mar 20, 2013
Here's my code:
struct Member {
char *name;
char *address;
char Interests[][10];//<------problem
int numofInterests;
Numbers digits;
[Code] ....
Now the Program:
newMember.Interests[numofInterests];
newMember.numofInterests = numofInterests;
for(int i = 0; i < numofInterests; i++) {
printf("Enter %s's %i interest: ", newMember.name, (i+1));
[Code] ....
it's a array of cstrings, but i can't figure out how keep it from outputting garbage, i'm assuming it's because i didn't end it with a null terminator but when i did, it didn't work.
View 1 Replies
View Related
Apr 30, 2012
I have a program that runs fine but outputs garbage and skips processes when I input a decimal. It compiles fine and has no errors.
Code:
#include <iostream>
#include <float.h>
using namespace std;
int main() {
int x;
int y;
[Code] ....
View 4 Replies
View Related
Jun 27, 2013
Code:
#include <stdio.h>
#include <stdlib.h>
// Define doubly linked list structures
typedef struct link singleLink;
typedef struct trashLink singleTrashLink;
struct link {
[Code] ....
Alright, my code is almost complete, I just can't get the clearFromList function to work correctly.
Here's what the program does, in a nutshell:
- Create two doubly-linked lists, one to hold numbers 0 through i, the other to hold a randomly generated list of numbers no greater than i to be "trashed". The same number cannot be "trashed" twice.
- Go through the main doubly-linked list and "skip over" the numbers that are listed the trash doubly-linked list (i.e. "delete" them without actually freeing them).
- Free both lists at the end.
I still have to free the trash list, but that's easy. I'm just stuck getting the clearFromList function to "skip over" each number in the main list that is added to the trash list.
View 3 Replies
View Related