C++ :: Extract Chunks Of Numbers From A Stream - Lambda Functions Causing Errors

Jul 1, 2014

I am trying to write a function that extracts chunks of numbers from a stream (ie if i had "ui33ui24ui23hjdwejf" it would extract the chunk 33), and its finished, but it wont compile. errors here: [URL] ...... its only one line in an include file, but im stumped. anyways, this is the most updated version of the code: [URL] .....

View 1 Replies


ADVERTISEMENT

C++ :: Vector Causing Memory Errors On Destruction?

Sep 10, 2013

std vector seems to cause errors when destructing here some simplified code:

#include <stdlib.h>
#include <stdio.h>
#include <vector>
class Foo{

[Code]....

View 5 Replies View Related

C++ :: Unable To Extract Unsigned Values From Input Stream

Jan 16, 2015

I am trying to extract unsigned values from an input stream. I expect the extraction to fail when an invalid character is extracted. It fails correctly when I try to extract an unsigned int from "abc", but when I try to extract an unsigned in from "-1", the extraction succeeds, and the max unsigned int value is extracted (as if -1 were cast to unsigned int). I would expect the '-' to cause the extraction of an unsigned value to fail.

The code I am using is below.

#include <iostream>
#include <sstream>
#include <string>
#include <limits>
int main() {
unsigned int value = 8;
std::string negString = "-1";

[Code]...

Is this standard behavior for an istream extractor?

I am trying this in both Linux (gcc 4.4.3) and in windows with Code::Blocks (whaterver came with CB 13.12, apparently gcc 4.7.1)

View 11 Replies View Related

C++ :: Generalized Sort Function Using Lambda Functions

Jan 15, 2014

How to improve my customSort function. It is to accept a lambda function as parameter which customizes your sort in any special way you want.

#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <functional>

enum personType {teacher, student, baby};

[Code] ....

For example, std::vector<Person> people = {Mark, MrMac, Lola, MsWhite, Cindy, Zainab, Mikey, Fred, Zolal} is to be sorted so that teachers will be placed first, then students, and then babies, but with the exception that people with names starting with Z be placed before anyone else, followed by people with age equal to 8. All this is set up by the ordered list:

person.name.at(0) == 'Z',
person.age == 8,
person.type == teacher,
person.type == student,
person.type == baby

So the output gives

Zolal
Zainab (baby)
Cindy
Ms. White
Mr. Mac
Fred
Mark
Mikey (baby)
Lola (baby)

Teachers are supposed to be placed before students, which in turn placed before babies, but Cindy's age is 8, and so is placed before the teachers, and Zolal and Zainab start with Z, so is placed before her. So far so good, but now I want the further sorting result that all people within their subcategories be sorted in their own way, e.g. Zainab should come before Zolal alphabetically, Ms. White should precede Mr. Mac because she is younger, etc... How do I further generalize my customSort function so it carries out those further criteria as well?

View 9 Replies View Related

C++ :: Different Random Number Stream For Different Functions?

Mar 4, 2014

I am wondering is there a way to make function A uses random number stream say starting from srand(1) and function B uses random number stream say starting from srand(2) in the same program.

View 3 Replies View Related

C++ :: Overloading Output Stream And Pure Virtual Functions?

Aug 7, 2013

I'm working with inheritance and pure virtual functions, and I want to overload an output stream operator. However, every time I run the program I get this: 0x7fff00ee98c0.

I'll include a base class and a derived class so you can see what I'm talking about.

Base:

#include <iostream>
using namespace std;
#ifndef _Insurance_h_
#define _Insurance_h_

[Code]....

The application is something like this (I'm assuming the user has already inputted the name, salesperson, make, model, etc):

#include "Auto.h"
#include <iostream>
using namespace std;
#include <vector>
vector<Insurance *> sales;

[Code] .....

View 4 Replies View Related

C++ :: Program To Extract Special Code From A List Of Numbers

Nov 1, 2013

I am a beginner and I ALWAYS have the toughest time doing I/O files. It's extremely frustrating. It "seems" it should be so simple. The program should find a code from a list of numbers. These numbers are from 0 - 9, and after each number is a space in the file. Your job is to extract a special code containing only 10 of those numbers. For the number to be part of the code, it should be divisible by 2. After extracting 10 numbers divisible by 2 for the code, write those 10 numbers to the file to form the expected code.

Input file is ("question.txt")
Output should be ("code.txt")

Should this contain a "for loop" or If/else ?

Here's what I did . .

/
// int numbers, total, counter;
ifstream inFile;
inFile.open ("question.txt");
outFile.open ("code.txt");
if (!inFile)

[Code] ....

View 2 Replies View Related

C++ :: Program To Extract Numbers From A File That Is Always Updating With New Data

Mar 14, 2013

How to write a C or C++ program that extracts certain numbers out of a section after the 4, 5 and 6 space.

For Example:

The output file will produce:

[ [1900 00 0 -2.000000 0.650000 0.006000 0.020000 2.274000 0.010000 54 0 0.7 10.8 1 0 0 0.000000 17.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 18];...

So I want to extract the numbers 0.650000, 0.006000, 0.020000 because those are the 4,5,6 space after [ [

Another problem I have is that the output file I showed will always being updated constantly for instance as follow:

[ [1900 00 0 -2.000000 0.650000 0.006000 0.020000 2.2054000 0.010000 10 0 0.7 10.8 1 0 0 0.000000 17.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 13];...

[ [1900 00 0 -1.000000 0.650000 0.006000 0.020000 2.694000 0.010000 9 0 0.7 10.8 1 0 0 0.000000 17.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 10];...

[ [1900 00 0 -2.000000 0.6023000 0.04000 0.050000 2.2454000 0.010000 5 0 0.7 10.8 1 0 0 0.000000 17.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 12];...

And it will keep updating like this and I want to update the previous numbers I extracted with now the new numbers after the 4,5, and 6 space.

So I have to write a code to let it know when it encounters [ [ start looking for the 4,5,6 space and get those numbers and once it reaches ;... that's the end of that section so re-look for the next section of numbers after the 4,5,6 space.

The file will be a text file or matlab file that is always being updated with these sections of numbers. Can I make this a #include<nameoffile.h> or does .h not work with a file that is always being updated.

View 2 Replies View Related

C++ :: Read Stream Of Numbers From A File And Writes Only Positive Numbers To Second File

Mar 27, 2013

Write a program which reads a stream of numbers from a file, and writes only the positive numbers to a second file. The user should be prompted to enter the names of both the input file and output file in main(), and then main() will open both files. Another function named process() must then be called to read all the numbers from the input file and write the positive numbers to the output file. Note that you must pass the open stream variables for each file as arguments to the process() function, and that you need to (always) double check that the files opened successfully before using them.

This is what I have so far but its not working out!

#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
int process(ifstream &inf, ofstream &outf);

[Code] ....

View 1 Replies View Related

C :: Splitting A File Info Multiple Chunks

Apr 19, 2013

I have a tab separated file that consists of 10 columns. (classical txt file).the third column is the one interesting to me it consists of some strings like :

Code:

2313 234ffer string1 sd33 ...
2313 234ffer string1 sd33 ...
2313 234ffer string2 sd33 ...
2313 234ffer string1 sd33 ...
2313 234ffer string2 sd33 ...
2313 234ffer string1 sd33 ...
2313 234ffer string3 sd33 ...

according to the value in the third column i wish to split my text file sot that i have files string1.txt string2.txt and string3.txt such that each file contains the whole line. how to do it in c++ with hash tables (maps) and all those fancy tools all ready and waiting for me to use them but in c i am crippled.

Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "strmap.h"

[code]...

My idea was to first go through the file, hash all values in tht third column so i get a unique set of elements and then for each distinct string(element) in the third column create a file pointer, then go once more through the file and as I encounter a specific string(element) type, write the whole line into the appropriate file.

View 9 Replies View Related

C++ :: Calculate Average Of A Stream Of Positive Numbers - Loop Program?

Feb 25, 2013

Write a program that calculates the average of a stream of positive numbers. The user can enter as many positive numbers as they want, and they will indicate that they are finished by entering a negative number. For this program, treat zero as a positive number, i.e., zero counts as a number that goes into the average. Of course, the negative number should not be part of the average. You must use a function to read in the numbers, keep track of the running sum and count, compute the average, and return the average to the main() function. Note that you must use a loop to do this, as you don't know how many numbers the user will enter ahead of time. I am having problem writing a loop program for it..

View 1 Replies View Related

Visual C++ :: File Stream Change Its Address During Writing Text Contents To The Stream

Apr 29, 2013

I wrote a program to write text contents to file stream through fputs, the file stream address was changed in the middle of writing text content to the stream (11% text content have been put into the file stream), that cause the file stream pointer can be evaluated problem and raise exception on stream validation code in fputs library function, my question is what things could go wrong to make file stream pointer changed its address to something else or a NULL pointer if the file stream have not been flushed and closed.

View 5 Replies View Related

C :: Sscanf Causing Segmentation Fault

May 30, 2014

I have a problem in my code and after hours of looking I really cannot find out what is the problem.I use scanf to to parse lines of a file, let me copy only the important part:

Code:

/*File read...everything is fine*/
int seq_1, seq_i2;
int len;
while(fgets(&line[strlen(line)], line_length, file)!= NULL) {
}

[code]....

if (num_sc ==4) //the wanted format, do something So I am allocating all variables, and giving to sscanf pointers, so I really don't get where the segmentation fault appears.I checked with the debugger, the first line of the file is read, but at the second it crashes at the line of sscanf! The two lines don't have the format I want in sscanf but therefore I check if this number is 4 and then do other stuff to the data...

View 9 Replies View Related

C++ :: Function Causing Infinite Recursion Crash

Oct 24, 2014

This function is apparently causing infinite recursion, but I have no clue what's causing it:

Code:
int pow( int base, int exp ) { int somevariable = pow(base,exp-1);
if (exp == 0) {
return 1;
}
else {
return base * pow(base,exp-1);
}
}

View 10 Replies View Related

C++ :: Destructor And Inheritance Causing Program To Crash After Running?

Dec 13, 2013

I have a class which dynamically allocates memory for three data arrays, and as such in the destructor I told it to delete those data arrays.

However, when I've created a new class, and inherited the previous class - it will always crash AFTER running the program, unless I don't have the previous destructor present.

View 3 Replies View Related

C# :: Picture Cropping Code Causing Error In Main Program

Jan 24, 2014

Ok, this thing works as far as the getting the image and drawing the rectangle but for the life of me I cannot figure out why it is causing an in the program.cs.

public partial class Crop : Form {
public Crop(Image BaseImage) {
InitializeComponent();
if ((Bitmap)BaseImage != null) { pbOriginal.Image = BaseImage; }

[Code] ....

BaseImage is pulled in from the main form's picture box.

View 14 Replies View Related

C++ :: Using Lambda As Encapsulated Sub-function

Mar 6, 2014

I have this method called "walk" and what it's supposed to do is, walk through a bunch of triangles one step at a time (the're all connected [not to each other, but in a mesh]).

To do so, I need to perform a containment test.

Normally, I guess I'd just write a separate function but what if I wanted to be weird and write my inclusion test as a lambda in my walk method? Literally the only function that needs this code is my walk() procedure and I need to call the test an arbitrary amount of times.

Is this frowned upon? Would this be the jarring type of code I've been warned about? Or should I just say yolo and do what I want?

View 4 Replies View Related

C# :: Cannot Convert Lambda Expressions

Mar 31, 2015

I get the following error:

Cannot convert lambda expression to type system delegate because it's not a delegate type (on invoke).

The Second error is: Client.PrivateChat.txtReceive is inaccessible due to its protection level..

private PrivateChat pChat;
private void client_Received(Client sender, byte[] data) {
this.Invoke(() =>
{
for (int i = 0; i < clientList.Items.Count; i++) {
var client = clientList.Items[i].Tag as Client;
if (client == null || client.Ip != sender.Ip) continue;

[Code] .....

View 4 Replies View Related

C/C++ :: Cannot Get Largest And Smallest Numbers Using Functions

Feb 6, 2015

#include<iostream>
using namespace std;
int computeTotal();
int findSmallest();
int findLargest();

[Code] .....

View 4 Replies View Related

C++ :: Expanding Variadic Template Arguments In Lambda

Feb 1, 2013

I'm trying to expand a template + argument parameter list inside a lambda function like this:

template <typename Class, typename ...Args>
static void create(Args ...args) {
// Perform pre-thread creation work.
std::thread([=]()

[Code] ....

But this does not work:

The compiler error is "error: parameter packs not expanded with ‘...’:|"

However, when I do the following:

template <typename Class, typename ...Args>
static void create(Args ...args) {
// Pre-thread work.
auto tthr = [](Args ...ar) -> void {

[Code] ....

It works just fine. That shows that lambda threads are able to take variadic arguments...

So here is my question; what is the correct capture clause for capturing the variadic object correctly?

View 3 Replies View Related

C++ :: Thread With Lambda Function - Swap 3 Variables

May 13, 2014

I am trying to realize a simple code with thread and lambda function.

My goal is to swap 2 variable . I launch 2 thread,

The first:
Put his value in a shared variable and notify it .
wait until an event on a condition variable occur.
read from shared value .

The second wait until an event on a condition variable occur.
Wake up
read from shared value .
Put his value in a shared variable and notify it.

This is the code

thread t1([&p1]()->void{
m.lock();
nt++;
if(nt==1) {
//first thread
unique_lock<mutex> u1(m); ***

[Code] .....

Why it say error "abort() has been called " on the istr ***

View 2 Replies View Related

C# :: Join Tables In LINQ With Lambda Expressions

Dec 5, 2014

i have stuck in a join and i cant figure out where the problem is, i have those tables

public class Themes
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }

[Code].....

View 12 Replies View Related

Visual C++ :: Constructor With Functional / Construction Via Lambda?

Feb 17, 2014

I have a class which upon construction has a part which should be executed when the constructor has finished "enough" to make the call valid.

Take the following simplified class example (the actual problem is much more elaborate):

Code:
#include <functional>
class base {
public:
base(int i, std::function<void()> func) : mi(i)

[code]....

Explicitely creating a derived class allows me to properly implement the lambda to call a member of the class.

I can't seem to figure out how to do this for an explicit instantiation of the base class.

Can this be done ? Or is this a shortcoming of VS2012 or something the standard doesn't handle ?

View 12 Replies View Related

C++ :: Passing Lambda As Template Parameter Not Working Correctly

May 30, 2013

I've was trying out a function template to automatically get the type of a lambda, but it seems that it won't compile

I've tried two different ways:

1.
template<class HASHER>
auto make_unordered_map(size_t bucketCount, HASHER const && hf)
-> unordered_map<string const, HASHER>&& {
return unordered_map<string const, int, HASHER>(bucketCount, hf);
} auto x = make_unordered_map(1, [](string const& key)->size_t { return key[0]; });

2.
template<class HASHER>
auto make_unordered_map(size_t bucketCount, HASHER const && hf2)
-> unordered_map<string const, int, decltype(hf2)> {
return unordered_map<string const, int, decltype(hf2)>(bucketCount, hf2);
} auto x = make_unordered_map(1, [](string const& key)->size_t { return key[0]; });

The test code are located here:

1. [URL] ....
2. [URL] ....

They are both based on the code that is stated to work in those examples. I.e.:

auto hf = [](string const& key)->size_t { return key[0]; };
unordered_map<string const, int, decltype(hf)> m (1, hf);

View 13 Replies View Related

C++ :: Lambda Accepts No Arguments But It Accesses Increment By Value And Current By Reference

May 24, 2013

In this code:

// Ex10_15.cpp Using lambda expressions
#include <algorithm>
#include <iostream>
#include <iomanip>

[code].....

The lambda accepts no arguments, but it accesses increment by value and current by reference, the latter being used to store the next value to be set. This lambda has the side effect that current will be updated when generate is finished. The following lambda expression is similar, but without the side effect:

[=]()mutable->T{T result(current);
current += increment;
return result;}
"

I dont exactly understand what side affect it is talking about. Wouldn't you want generate to update current? I understand how the second code fixes it though, just takes everything in the enclosing scope by value.

View 2 Replies View Related

C :: How To Extract Data From Txt File

Jan 13, 2015

I managed to read txt files and printing them on the console window. I was given a task to select and sample a certain amount of data.

Example of txt file:

Voltage (V),Current (I),Power (W)
50,2,100,
51,2,102,
52,2,104,

etc.. How can I display only the column of Voltage and Power?

View 1 Replies View Related







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