C++ :: Printing Vector Values Preceded By

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


ADVERTISEMENT

C++ :: Write A Program That Replaces Values In A Vector With Their Absolute Values

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

C++ :: Printing Vector Adding New Line

Oct 10, 2013

I have a vector of structs.

struct myStruct{
string text;
int num;
};

vector<myStruct> foo;

And I am attempting to print the text followed by a space, then the number. Like so:

foobar 5

But when trying to print my vector using

ofstream outputFile;
outputFile.open ("file.txt");
for(int i = 0; i < foo.size(); i++) {
outputFile << foo[x].text << " " << foo[x].num << endl;
}

It prints like

foobar
5
moretext
8

With an extra newline and space. How to get rid of it and print it on the same line.

I have checked that text does not include new line character at the end. Also, it seems to print correctly using cout, but not print correctly to output file...

View 1 Replies View Related

C++ :: Printing Contents Of A String That Also Exist In A Vector

Jan 27, 2014

For the last part of this problem, if player 2 loses the game of hangman, I need to display the letters they did get right. In order to do that, I believe that I need to traverse vector v for the elements that exist in the string hiddenword, and then print those characters that match.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;
int letterFill (char, string, string&);

[Code] ....

View 1 Replies View Related

C :: Drawing A Box With Coordinates - Printing Between Certain ASCII Values

Sep 23, 2014

The assignment is:

1) Write a program that asks the user for a a single character and two XY coordinates. The two X and two Y values should all be integers between 0 and 50. The character should be a printable ASCII character with values between and including ' !' (ascii value 33) and '~' (ascii value 126).

2) Your program should then draw a rectangle made up of the user selected character where the upper left corner is at X1; Y 1 and the lower right corner is at X2; Y2. Be sure to print the appropriate number of blank lines (having spaces in the blank rows is OK) in the beginning and pad each row of your rectangle with X1 leading spaces.

The Output is supposed to be similar to this:

(X1,Y1) = (0,0) , (X2,Y2) = (4,4), the character = ^

^^^^
^^^^
^^^^
^^^^

What I am having trouble understanding is printing between certain ASCII values (ASCII has never been discussed in class).

Another thing I am having trouble with is the main part of the assignment. From what we are currently discussing is loops and the assignment is covering nested loops. My code looks similar to this:
Code:

#include <stdio.h>
int main (void) {
int X1, Y1, X2, Y2;
char cRec;
printf("Enter a character: ");
scanf("%c", &cRec);

[Code] .....

My thinking on the assignment is that you want the X1 coordinate to increase to the value of X2 (same for Y1 and Y2). Is this thinking wrong?

View 12 Replies View Related

C# :: Printing All Values Above The Diagonal Line In 2D Array

Apr 11, 2014

I need to print all the values in a square 2d array that are above the diagonal line. So if I have this 6x6 array for instance:

{0,0,0,0,0,0},
{1,1,1,1,1,1},
{2,2,2,2,2,2},
{3,3,3,3,3,3},
{4,4,4,4,4,4},
{5,5,5,5,5,5}

I'd have to print the elements in italics. I've tried this:

for (i = 1; i < a.GetLength(0); i++) {
for (j = i-1; j >=0; j--)
Console.Write("{0,-2}", a[i, j]);
Console.WriteLine();
}

with the array mentioned above but for some reason it keeps printing the elements BELOW the line, when I need it to print those above it. I know that for arrays we usually start loops with a value of 0 but seeing as there should be nothing to print at [0,0] I started the loop at 1.

View 2 Replies View Related

C/C++ :: Reading Hex Values From File And Printing Them In Decimal

Dec 1, 2013

I am trying to read hex data from a file (not just hex values in text format). I was able to read it (02 EC) with "fread" into an char before but I need to change the Hex value into an integer in decimal.

I already read about strtol but I would prefer reading the hex Value into an integer.

Here is my code so far:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int itrack1[1];
int itrack2[1];

[Code] ....

View 11 Replies View Related

C++ :: Banking System - Searching For And Printing Values From TXT File

Mar 21, 2013

So I'm doing a kind of banking system. Atm I've got the user able to create an account and set the balance of it, which is written to a .txt file. However, I would like the user to be able to enter their account no. and the program to cout the account info on the screen. I will be adding more to the program later, but until I work out how to search for values in a .txt file, and print out values on that line I'm a bit stuck.

Code:
Main.cpp -
#include <iostream>
#include "create_account.h"
#include <fstream>
using namespace std;
int menuin;
int main() {
create_account creataccObj;

[Code] .....

Sample .txt file -

// Account_no - Balance //

1001 140500
1002 150600
1003 18600

View 8 Replies View Related

C++ :: Subtracting Values In A Vector?

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

C++ :: Assigning List Of Values To Vector?

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

C++ :: Erase Multiple Values In A Vector

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

C++ :: Print Values From A Vector Of A Struct?

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

C++ :: Can't Store Different Values Into Vector By User Input

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

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

Visual C++ :: Assigning Values To And Extracting Them From A Vector

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

C/C++ :: Printing Partitions Of A Number Using Constraint On Number Of Values?

Apr 15, 2014

I'm taking a C++ computer science course right now, and one of the questions on my latest assignment is this:

"A partition of an integer n is a way of writing n as a sum of positive integers. For example, for n=7, a partition is 1+1+5. Write a program that finds all the partitions of an integer n using r integers. For example, all the partitions of n=7 using r=3 integers are 1+1+5, 1+2+4, 1+3+3, 2+2+3."

I've been struggling with this problem for a couple days now, and how to do it. I understand I need a recursive function to grab variables, and probably an array or vector to store them, but where to begin.

I've been reading documents on partition generating and the concept still eludes me, and any other questions on here or other programming sites using partitions don't seem to have a constraint on them.

View 2 Replies View Related

C++ :: Cards Game - Seeding A Vector With 52 Unique Values

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

C++ :: Method Passes Values To A Vector But It Stays Empty?

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

C++ :: Incrementing Values In Vector Based On Variable Input

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

C++ :: Rotates A Vector 90 Degrees - Values Won't Stay Negative

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

C :: Assigning Values To Arrays / Printing Arrays

Jul 1, 2014

Using a for loop, construct two 100 element arrays, x and y, such that element i of x stores the value sin(2*pi*i/100)) and the corresponding element of y stores cos((2*pi*i/100)). Print the values stored in the elements of x and y as you calculate them.

I have attempted to solve it but I'm not sure why the value 0 is only being printed, maybe I haven't assigned sin(2i/100)) and cos((2i/100)) to the arrays properly?

Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main () {

[Code] .....

View 3 Replies View Related

C++ :: Program To Read Series Of Comma Delimited Values From A File Into Vector

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

C++ :: Using Vector Push Back Function To Output Contents Of Vector (similar To Array)

Feb 9, 2015

How to output vector contents using the push_back function. My program reads in values just fine, but it does not output anything and I've been stuck on why.

here is my code:

#include <iostream>
#include <array>
#include <vector>
using namespace std;
int duplicate( vector < int > &vector1, const int value, const int counter)

[Code].....

View 3 Replies View Related

C++ :: Open File And Read In Rows To String Vector And Return Vector

Jun 7, 2012

I have a cpp app that reads in a number of files and writes revised output. The app doesn't seem to be able to open a file with a ' in the file name, such as,

N,N'-dimethylethylenediamine.mol

This is the function that opens the file :

Code:
// opens mol file, reads in rows to string vector and returns vector
vector<string> get_mol_file(string& filePath) {
vector<string> mol_file;
string new_mol_line;
// create an input stream and open the mol file
ifstream read_mol_input;
read_mol_input.open( filePath.c_str() );

[Code] ....

The path to the file is passed as a cpp string and the c version is used to open the file. Do I need to handle this as a special case? It is possible that there could be " as well, parenthesis, etc.

View 9 Replies View Related

C++ :: Create Class Vector As Template And Define Operations On Vector?

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

C++ :: Recursive Function 2 - Create Vector Of Vector Of Integers

Mar 26, 2013

Lets say that I have a vector of vector of integers. <1,2,3,4> , <5,6,7,8>, <10,11,12,13>

How do I make a function that creates vector of vector of every different integers?

<1,5,10> , <1,5,11>, <1,5,12>, <1,5,13>
<1,6,10> , <1,6,11>, <1,6,12>, <1,6,13>
<1,7,10> , <1,7,11>, <1,7,12>, <1,7,13>
<1,8,10>, <1,8,11>, <1,8,12>, <1,8, 13>
<2,5,10>, <2,5,11>, <2,5,12>, <2,5,13>

and so on...

View 2 Replies View Related







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