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
ADVERTISEMENT
Oct 5, 2013
I need to read a text file which has various lines containing integers. I need to write those integers separately in a vector. Example, the first line of the text file contains 3 9 8 7 6 so vector[4]=3, vector[3]=9, vector[2]=8 and so on. Next read the second line 4 1 2 3 4 5 and write to another vector vector[5]=4, vector[4]=1...
I tried the code below but it will write from the second line, the whole line in one vector index.
int str; // Temp string to
cout << "Read from a file!" << endl;
ifstream fin("functions.txt"); // Open it up!
string line;
// read line count from file; assuming it's the first line
getline( fin, line );
[Code]...
View 1 Replies
View Related
Nov 3, 2013
I'm working on a program that prints every even number from 100 to 200. How i would be able to print 10 numbers per line?
Code:
#include <iostream>
using namespace std;
int main()
{
int x;
for (x=100; x<=200; x++)
cout <<x++<< endl;
system("pause");
return 0;
}
View 2 Replies
View Related
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
Jan 14, 2014
i got 2 arrays, how would i print 10 numbers per line, so that would be 5 numbers from each array.
array1[40];
array2[40];
array1 array2// array1 array2// array1 array2 // array1 array2 // array1 array2
array1 array2// array1 array2// array1 array2 // array1 array2 // array1 array2
array1 array2// array1 array2// array1 array2 // array1 array2 // array1 array2
...... and so on
how can i put this in a loop?
and so on but there are 80 elements so i cant go one by one.
View 5 Replies
View Related
Jul 10, 2013
I'm trying to print Chinese/unicode characters to a line printer(EPSON LQ-2090) using the writePrinter method in c++.
ANSI characters print fine, but when I throw the buffer of widechar Chinese characters at it, they come out like garbage/ANSI chars.
while debuging string shows chinese characters but in memory it shows ANSI characters not chinese and these ANSI characters are get printed on printer.
Note that if I change the DocInfo datatype parameter to "TEXT" instead of "RAW" then also the Chinese characters donot print.
Is there a way to get Chinese or unicode characters to print correctly?
View 6 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
Apr 10, 2014
I have a basic question regarding 2d vectors. The following code makes a 2d vector and fills it with a matrix of integers. The vector tempVector3 gets added as a new row to the matrix. But what if I wanted to add the tempVector3 as a new column instead. How would this be done in the simplest way?
#include<iostream>
#include<vector>
int main(){
std::vector<std::vector<int>> numbers;
std::vector<int> tempVector1;
tempVector1.push_back(2);
[Code] ....
View 5 Replies
View Related
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
Feb 1, 2013
I am adding int type into vector called "vi" several times like this.
std::vector<int> vi;
void addFunc(int *a, int cnt) {
vi.erase(vi.begin(), vi.end());
vi.clear();
for(int i=0; i<cnt; i++)
vi.push_back(a[i]);
}
and I call the addfunc N times every sec.
And sometimes it has runtime error on vi.push_back line.
(called stack says "_CrtIsValidHeapPointer")
View 5 Replies
View Related
Oct 10, 2013
how to add a list of information from a file to a vector of a class. Here is my code:
Champion_Info.h
#ifndef CHAMPION_INFO_H_INCLUDED
#define CHAMPION_INFO_H_INCLUDED
#include <vector>
#include <string>
[Code].....
View 6 Replies
View Related
Jan 30, 2013
How would I add my own function to the vector class?
View 3 Replies
View Related
Jan 16, 2013
Consider the following piece of Code:
int ReadNumbers() {
int num;
vector<int> x;
cout << "Enter Numbers" << '
[Code] ....
The while loop is expected to terminate when the user provides an Invalid Input. But this while loop behaves unexpectedly when the user provides a 'Newline' input (by pressing Enter) and becomes an infinite loop. How can I prevent this from happening? Also I've heard that expecting invalid inputs isn't good code design. Is this true? If yes, then how can I solve my question without expecting Invalid Inputs?
View 10 Replies
View Related
Apr 20, 2014
I'm having a problem filling a vector from a file. Basically, it is adding an empty element at the end. I'm new to Qt and haven't worked with file streams much so how to stop the stream before it adds the extra element.
void gui::get_data() {
mileage.clear();
QFile file(file_label->text() + ".txt");
QTextStream in(& file);
float m;
float g;
QString d;
[Code] ....
But, if I add another element to the vector and write that the file look like this.
//file after adding element
132654 0 02132014
132654 0 02132014
0 0
132998 22 02202014
I have it set to append at the moment so that is why the first line is repeated. I figure the problem is with if(in.atEnd()). I could fix it by deleting the last element right after adding it, but that seems like more of a hack than anything else.
View 3 Replies
View Related
Sep 2, 2013
I'm having problems fully comprehending how to do this task (I'm only going to include my function code since that's the basis of my problem). How should I go about getting my vector to print off 12 items per line. Here's my current function.
void printVec(const vector<int>& v) {
for(unsigned i=0; i < 12;i++)
cout<<v[i]<<" "<<endl;
}
View 2 Replies
View Related
Mar 7, 2014
How can a mulitline string be read line by line
ex: str = "PERIOD="week"
DAY="day"
TIME="time"";
View 2 Replies
View Related
Mar 5, 2013
I need to write a ANSI program to print out each command line argument on a separate line using a for-loop. also it need to print the name of the executable .so far I have
Code:
#include <stdio.h>
int main(int argc, char **argv) {
int i;
printf("")
[code]....
View 1 Replies
View Related
May 7, 2013
l need to write a program which writes out its command line arguments in reverse order one per line. The output from the program should look like this:
% a.out Two roads diverged in a yellow wood
wood
yellow
a
in
diverged
roads
Two
View 9 Replies
View Related
Aug 17, 2014
I am trying to read a file line by line and then do something with the informations, so my method looks like this:
Code:
void open_file(char *link) {
FILE *file = fopen(link, "r");
if (file == NULL) {
fprintf(stderr, "Could not open file.
");
exit(EXIT_FAILURE);
[Code] ....
1) The first complain of valgrind is at the line where I use fgets and its telling me (invalid write of size x), but I have allocated my line to 56000 and the read line is shorter, why is there a write size error then :S?
2) at the line where I realloc where I try to shrink the space he's telling me: Address .... is 0 bytes inside a block of size 56000, But I know i need only this space so why is there a write over space error :S??
View 9 Replies
View Related
Sep 25, 2013
So I'm reading a file line by line and storing it backwards into a list. So if the file has has this format...
1
2
3
4
The code should store each line in a list as such...
4, 3, 2 ,1
Instead the code will store the last variable in all nodes. So the final list will look like this...
4, 4, 4, 4
Here is my code...
struct node *head = NULL;
int i;
while(read(in, &i, sizeof(int)) != 0) {
struct node *temp = malloc(sizeof(*temp));
temp->line = &i;
temp->next = head;
head = temp;
}
View 4 Replies
View Related
Jul 15, 2013
I need to read lines from one file and copy them line by line into another file using dynamic memory allocation. It compiles but gives me a seg fault. Why/How?
Code:
int main(){
FILE *fp1;
FILE *fp2;
FILE *i;
fp1=fopen("file1","r");
fp2=fopen("file3","w+");
[Code] ....
View 2 Replies
View Related
Feb 11, 2014
There are numbers of lines connected to each other. I've extracted the line start point and end points but i am confused how to compare the end point of one line with the start point of adjoining line.
View 2 Replies
View Related
Feb 3, 2014
I was loaded a bmp file in my mfc window and stored rgb data in a file.My image size is 320x240.
Is possible to pick a single point (location) from that bmp image (Not the whole window)?
i like to plot a line graph using the pointed single line data using the stored file data?
here R is X Axis and G & B is Y Axis.
If i have 2D array of data means how can i plot the line graph?
View 4 Replies
View Related
Nov 5, 2014
I'm using fgets which will read a single line at a time, but unlike fgets I don't want it to return the new line char ( ) ?I want to be able to use printf to display the lines next to each other.
View 7 Replies
View Related
Jul 5, 2013
I have a text file (test.txt) with the following data:
01,05,25,20130728
01,06,25,20130728
01,07,25,20130728
01,08,25,20130728
01,05,25,20130728
01,05,25,20130728
01,05,45,20130728
01,05,65,20130728
01,05,85,20130728
01,05,35,20130728
01,05,25,20130728
01,05,35,20130728
I want to read this to one string called line. So far I have this code:
string line;
ifstream myfile ("/home/Test.txt");
if (myfile.is_open()) {
while (myfile.good()) {
getline (myfile, line);
for (int a = 0; a <= 335; a++) {
cout <<line.at(a);
} }
myfile.close();
}
so far its only printing the first line and then throwing an instance of 'std::out_of_range'
View 2 Replies
View Related
Apr 12, 2015
Im trying to read from a file, line by line, with no specified number of values in the file. Check out my code:
int main() {
string x;
ifstream fin;
int count = 0;
char ch;
fin.open("CWC_Master.txt");
if(!fin)
[Code] .....
Now, this works great! However, its skipping some lines. And I dont know why. For example: Lets say that the input file is:
superman toy
sm2335
19.99
batman toy
bm5532
25.99
aquaman toy
am6786
26.00
Where it should output the above, instead it outputs every other one. Like:
superman toy
19.99
batman toy
25.99
aquaman toy
26.00
How can I fix my code so that it SIMPLY(i say simply because I am still a beginner coder) can read line by line?
View 7 Replies
View Related