C++ :: Optional Parameter In String Stream
Sep 2, 2013
I'm reading in a data set using an ifstream, then fetching line by line to a stringstream:
std::ifstream sfile(filename);
std::string test;
std::getline(sfile, test);
std::stringstream sline(test);
(I'm not sure why I used an intermediate string; it's pretty much legacy-code at this point, which I just reuse every time. Still works, so why change it!)
The problem is I'm using two types of data sets now, and the difference is one (optional). Most data files just have an arbitrarily large number if the second must be ignored, but others have nothing.
In the normal case, I'd simply use sline >> d >> L; to extract the parameter values. However, I'm not sure how this line will behave if the second parameter is omitted. Will it read nonsense? How do I check whether or not the parameter was set or not?
View 7 Replies
ADVERTISEMENT
Apr 15, 2015
If I’m trying to define an optional one to many relationship I need to reference the primary key of the table
e.g
modelBuilder.Entity<TCC_ArchivedIntegrationExceptions>()
.HasRequired(t => t.TCC_TransactionHistory)
.WithOptional(t => t.TCC_ArchivedIntegrationExceptions);
Where TCC_ArchivedIntegrationException is the optional key.. but those models are inheriting their key from a base entity
So do I have to create the key explicitly in the model or is there another way around this?
View 1 Replies
View Related
Apr 7, 2014
I am currently trying to read in a file that takes in the input in the following form.
Code:
HANK>25 BOB>31 AL>54
BILL>41 ABE>63 JEFF>50
I have tried the following solution:
Code:
#include<ifstream>
#include<iostream>
using namespace std;
struct node
{
string name;
int age;
);
int main ()
{
[code]....
The problem is a.name, it's extracting the entire string before the space character causing p.age to contain "BOB" and so on. I've tried using p.get(p.name, sizeof(p.name), '-') with '-' as the delimiter and p.getline() using a character array instead of a string. How would it be possible to only copy the string into a.name before the '>' character?
View 1 Replies
View Related
Oct 17, 2013
This is my code:
#include <iostream>
#include <string>
#include <istream>
#include <sstream>
using namespace std;
int main() {
string groups[3] = {};
[Code] ....
It outputs jibberish. I can do what I need to do but I would need to declare more variables and write more cout's, isn't there a way to add these elements to a stringstream or streambuffer? My goal is to write this program and make it as comprehensive as possible but also with very few lines.
View 1 Replies
View Related
Oct 2, 2013
I have problem with this code:
#include <windows.h>
#include <stdio.h>
#include <gdiplus.h>
#include "ScreenCap.h"
#include <wchar.h>
#include "base64.h"
#include <sstream>
using namespace Gdiplus;
int GetEncoderClsid(WCHAR *format, CLSID *pClsid) {
[Code] ....
This function return me a long series of Y ended with other chars but not the base64 string.
I think the problem is std::string encodedstring = base64_encode(buffer, dwBufferSize); but I can't figure out the problem.
The base64 function is: [URL] ....
View 2 Replies
View Related
May 14, 2013
I am working on a project which is a fairly simply BAC calculator. I am attempting to grab data from a text file, which contains state abbreviation, maximum BAC before aggravated, and minimum license suspension. The text file looks like
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <stdio.h>
#include <ctype.h>
#include <iomanip>
#include <algorithm>
#include <cstdlib>
#include <sstream>
using namespace std;
float MALE_RATE = 0.68, FEMALE_RATE = 0.55, LEGAL_LIMIT = 0.08;
//percent of body weight which holds alcohol
[Code] ....
The code I am struggling with is the getline and stringstream which is near the bottom.
The output I am getting is
The maximum BAC before aggrevated DUI in AL is .15
The minimum penalty in AL is 90 day suspension of license
I get that for every state I type. I know it is reading the first line, but something in my code does not allow it to read the other lines
View 1 Replies
View Related
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
Oct 7, 2012
I am working on a project were I have to read line form ( PLC ) programmable logic controller generated text file with lines like this
Circuit Value Current 2.33 4.32 5.55
there could be up to 3000 lines per txt file
I am using string stream to parse the line, for the sake of good programming I which to check weather first three values are string and last three values are actually floats raise or throw an exception if they are not ....
View 4 Replies
View Related
Aug 19, 2014
In the thread "Making a argument optional to function", it is stated that to set default values for arguments of a function you can simply do so in the function definition, like:
int myfunc(int a, int b, int c=3) {...}
This then automatically puts c to 3 in the function body if a call like myfunc(1,2); is made, if I understood correctly. However, this does not seem to hold for class functions. For example, something like:
class classy {
public:
int class_func(int, int, int); // class function prototype
}
int classy::class_func(int a, int b, int c=3) {...}
fails to compile. What I would like is to be able to call class_func outside of this class (by including it as a header in another macro), optionally specifying c. If c is not specified in the call, it should use a default value.
View 2 Replies
View Related
Jan 21, 2014
I have two puzzling issues I am dealing with.
Issue 1: I am using a stringstream object in a block of my program that needs to be visited repeatedly depending on a user's selection from a menu. I want the contents of this stringstream object to be cleared any time control gets to this part of the program. I have tried the clear and flush functions to no avail.
Issue 2: I am reading data from a source text file that would be regularly changed during the course of program run. After the program run is over, I am supposed to save the results(which is basically the source text file AND all updates) in a destination file. This destination file would then serve as the source file when next the program is run. In other words, I want a scenario where my results overwrite the original contents of the source file; implying that my source and destination files are now one, pretty much. How can I do this?
View 7 Replies
View Related
May 9, 2014
Debug Error
ProjectsFinal ProjectGrItemDebugGrItem.exe
R6010 - abort() has been called
I was going over this with a friend and it seems as though getline() is not reading anything in and thus throwing the abort error. I'm not sure why this is because I've included the textfile, with the correct name of course, in both the regular file location and the debug folder. I ask for user input and the user then inputs the name of the file they want, I do some required things behind the scenes and display the results for them in a cmd window. I've included pastebin files for both my header and cpp files because it is far to large for one post I shall, however, post the full code in the comments.
Quick Code
The problem occurs on line 159. I'm assuming once this line is fixed, line 163 will have the same problem.
// Read regular price
getline(nameFile, input, '$');
vectorList[count].regPrice = stof(input.c_str());// Casts string to a float
// Read sale price
getline(nameFile, input, '#');
vectorList[count].salePrice = stof(input.c_str());
Pastebin Links : [URL] ....
View 2 Replies
View Related
Oct 19, 2013
I want to alter a string inside a function, but it is not working.Here is the function:
Code:
#include <stdio.h>
void test (char *ch){
ch[0] = 0;
ch[1] = 1;
ch[2] = '';
}
[code]...
View 2 Replies
View Related
Mar 20, 2012
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void interpose(char s[], char a[], int pos) {
char b[80];
[Code] ....
The program should take 2 string parameter and 1 int parameter for combining. (without any str functions, the functions should be written by coder)
My problem is when i enter int value program crashes.
For example,
str1='Good';
str2=' Morning';
int x= 5;
The output will be 'Good Morning'
View 2 Replies
View Related
Feb 21, 2014
why I'm getting an error with this code? I'm trying to pass a string as a parameter and use it to open a file:
class txt{
private:
string tempstring;
vector<string> strings;
char filename[10]; //not using this right now
[code]....
but I get this error:
[Note] no known conversion for argument 1 from 'const string {aka const std::basic_string<char>}' to 'const char*'
I thought strings were just const character arrays
View 3 Replies
View Related
Feb 19, 2013
I know my product.cpp constructor is wrong but how can I set my private variables.
//PRODUCT.h
#ifndef PROJECT1_PRODUCT_H
#define PROJECT1_PRODUCT_H
[Code].....
View 2 Replies
View Related
Feb 18, 2013
Modify your code by adding your own tests to see if your functions work right. Include at least 6 separate tests, of your choosing.
For example, test the compare function with the first parameter as a blank string -- then with the 2nd as a blank -- then both. Test compare with the first string shorter than the second -- then the other way around. Test your copy function with long strings.
I am struggling with how to use the compare function with a parameter as a blank string. I tried leaving the first parameter blank but doing ("",text) but I don't think that is the correct way of doing this.
#include <cstring>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int myStrLen(const char[]); // return length of null-terminated string stored in char array
[Code] ....
View 2 Replies
View Related
Sep 26, 2013
How to write a printPattern() function?
The function takes in a parameter i.e. a char pointer. It handles the parameter as a Cstyle string i.e. a NULL terminated char array. It does not make use of the stringclass or its associated functions. In other words, the function examines every char element in the array until it encounters the terminating NULL character.
Starting from this int main :
int main() {
char string1[] = "Application of C++";
printPattern(string1);
}
[Code].....
View 1 Replies
View Related
Feb 3, 2013
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void count();
void main() {
char c[100];
cout<<"Enter a string upto 99 characters(spaces included):"<<endl;
[Code] ....
The thing is, when I don't create a prototype of count and straightaway define it before void main() my program runs, but it gives error in this program that "extra parameter in call to count()" But what to do then?
View 2 Replies
View Related
Sep 28, 2014
My errors are at the end of the program in two function calls within the definition of the InsertByValue function. g++ does not seem to recognize NumArray as a valid parameter.
#include <iostream>
#include <assert.h>
using namespace std;
const int CAPACITY = 20;
/* Displays the content of an int array, both the array and the size of array will be passed as parameters to the function
@param array: gives the array to be displayed
@param array_size: gives the number of elements in the array */
void DisplayArray (int array[], int array_size);
[Code] ....
View 1 Replies
View Related
Dec 4, 2013
I am developing new project in Qt with existing MFC project . SO in MFC I have a function which uses SYSTEMTime and return CString.
example
CString getTimestampString( void )
{
SYSTEMTIME systemTime;
CString datestr;
[Code]....
PS -> I cant able to make any changes in lib_know as this library is being used by many other projects..
View 1 Replies
View Related
Jul 2, 2013
Here's my function definition
bool validateNumber(string& text, int min = 0, int max = -1, bool useMin = true,
bool getValid = true)
The code takes the string text, and checks the make sure that the input is valid and safe to convert and use as a number. However, sometimes there is not min, and sometimes there is no max. The lack of min is done by using the parameter useMin, while the lack of max is done by max < min.
My predicament is the following call:
validateNumber(text, -2);
Now, max will be used, even though I don't want it. Ideally, I would want to do something like... int max = (min - 1), ... but that doesn't work. I also can't check to see if the parameter hasn't been changed (that I know of), because the following call would make it look like it hasn't
validateNumber(text, -2, -1);
So the question is, is there a way to do what I want, without having to add in a bool useMax parameter? Or is this my only option? I don't want to do that for simplicity, but if I have to, I have to.
View 3 Replies
View Related
Dec 4, 2013
Why does my loop loops 3 times when i have only 2 line of data in my file? From the cout << s.size , when i run it will show 1,2,3.
Information in txt file
Code:
local john 10/2/1990
international tom 2/5/2000 output Code: local john 10/2/1990
international tom 2/5/2000
international tom 2/5/2000 Code: ifstream in(filename);
[Code] ....
View 4 Replies
View Related
Feb 24, 2013
I'm trying to display an image from a stream data. But there is no image when getting image::from stream.
It's my source code:
Code:
IStream* pstream = NULL;
if(SUCCEEDED(CreateStreamOnHGlobal(NULL, TRUE, &pstream))) {
ULONG lreal = 0;
pstream->Write(chIncomingDataBuffer, iEnd, &lreal );
[Code]...
There is no image from data.
View 5 Replies
View Related
Mar 4, 2013
I'm having some trouble with copying one I/O stream into another. I've put the first one into an array but I cannot get my second prompt to copy the .txt file the first prompt sees and outputs to the console. When I try and grab the info from the .txt file my first prompt sees I only see blank space in my .txt file.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <fstream>
using std::ifstream;
using std::ofstream;
[Code] .....
View 1 Replies
View Related
Feb 6, 2013
Do stream iterators, such as std::istreambuf_iterator<char>, read a chunk of bytes internally, or do they read the stream one byte at a time?
Because I am thinking to write a BufferedFile class which uses an std::vector<char>.
View 3 Replies
View Related
Oct 13, 2013
#include<fstream.h>
#include<conio.h>
#include<string>
class CLS {
public : string name;
[code]....
I have used the above code to write the class instanc to a file "Text.txt"...But it seems that "f.write((char*)&c, sizeof(CLS));" is not working properly with string. The data can easily be written using stream object!
#include<fstream.h>
#include<conio.h>
#include<string>
class CLS {
public : string name;
[code]....
When I tried to read the data on the file...., it gave an error "Thread stopped...violation.."
View 7 Replies
View Related