C/C++ :: Pass A String As A Parameter And Use It To Open A File?
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
ADVERTISEMENT
Jul 14, 2012
I have a folder "pics" in g: drive. There I want to create some text.txt file. Thus, the final path is "g:pics ext.txt".
How can I do that?
View 3 Replies
View Related
Feb 6, 2015
//This program demonstrates a function with three parameters
#include <iostream>
using namespace std;
//function prototype
void showSum(int, int, int);
int main(int x) { //Take notice int x parameter
int value1, value2, value3;
[Code] ....
I ran the program from command prompt:
C:WindowsSystem32>"E:CS Ia.exe"
Enter three integers and I will display their sum: 0
0
0
0
Also, btw, x = 1
C:WindowsSystem32>echo %ERRORLEVEL% // return value (normally 0)
124234
[Code] .....
My problem is I do not understand what is going on when I try to pass an argument to the program. (Since it is defined int main(int x)).
View 2 Replies
View Related
May 2, 2013
I have been working on this all day, and its due in like an hour and a half. I have done everything the program wants except the last part. Here is the assignment:
Write a program that inputs 10 integers from the console into an array, and removes the duplicate array elements and prints the array. You may assume that all the integers are between 0 and 100, Write at least 1 function in addition to the main function, and pass an array into that function as a parameter. e.g.
Please enter your 10 numbers: 1 2 3 4 5 6 7 8 9 10
The array contains: 1 2 3 4 5 6 7 8 9 10
Please enter your 10 numbers: 1 1 3 3 3 6 7 8 9 9
The array contains: 1 3 6 7 8 9
Please enter your 10 numbers: 1 1 1 1 1 1 1 1 1 1
The array contains: 1
The bolded part is what I cant get to work. I have tried this and it keeps telling me I have not identified the items when I have.
Here is my code:
#include "stdafx.h"
#include <iostream>
using namespace std;
[Code]....
View 1 Replies
View Related
Oct 20, 2013
How can I pass a function as a parameter? I have a class that I'm trying to reuse and one of the methods in this class need to take three parameters, two ints and a function. In other words I want to be able to call a custom function every time this method is invoked when used in other classes. The function I want to call will not return any values, its a void function.
In my Class:
void Utility::someFunction(int var1, int var2, void customFunction) {
int num1 = var1;
int num2 = var2;
[Code] .....
View 9 Replies
View Related
Feb 12, 2014
I need to pass a variable to a dialog box.
Code:
Doc* pDoc;
Dialog dlg;
int input = dlg.DoModal();
When I call dlg.DoModal() I need to somehow pass the pDoc into the dialog box. Everything I need the variable for is taking place inside the oninitdialog function. Is there anyway to pass the variable to that function?
View 2 Replies
View Related
Dec 8, 2014
I have some code here where I try to declare a struct then pass it as a parameter into a function to do something to it:
Code:
struct _user {
char * initial[3];
int pos;
} user;
int initial_add (struct user * initial_list, int initials, char * buffer) {
[Code] ...
I get the error :
server2.c:15: warning: "struct user" declared inside parameter list
server2.c:15: warning: its scope is only this definition or declaration, which is probably not what you want
[Code] ....
View 6 Replies
View Related
Sep 19, 2014
Due to the nature of this requirement, I've made a very minimal example, which would adequately solve my issue, without resorting to use of pointers or copy constructors.
Basically I'm trying to pass an object as a reference to the template function, rather than a copy as it's seeing. I'm needing to do this without editing Obj::Call to accommodate a reference as its first parameter, as it'd break other calls.
You'll notice in the following code the object will be destroyed upon passing, while the object defined is still in-scope due to the infinite end loop.
#include <iostream>
#include <string>
using namespace std;
class Obj {
public:
string name;
[Code] ....
In the past I tried ref(), which appeared to stop this happening, however it created a blank copy of the object instead.
View 1 Replies
View Related
Jun 13, 2013
void extf(int a) { }
template<typename P>
struct A {
// 1
template< void (*F)(P) >
static void call(P arg) {
[Code]...
Why it is not working? What would be a proper way to pass function pointer as a template parameter?
View 6 Replies
View Related
Jul 24, 2014
[URL]
class CMyclass
{
public:
CMyClass(BOOL bUseWhichMemManager);
void* operator new(size_t);
void operator delete(void*);
};
I create two memory manager called CMemManager1 and CMemMangaer2, using different algorithms to allocate buffer. Now I want to control which memory manager to be used, when calling new.
I try to add a parameter bUseWhichMemManager to the constructor, but in overrided new function, there are no way to access the parameter. Is there a way to pass more parameters to new operator, such as:
void* operator new(size_t size, BOOL bUseWhichManager);
View 1 Replies
View Related
Apr 20, 2013
I have this code:
const BYTE original[2][4] = {
{0x00, 0x00, 0x00, 0x00},
{0xFF, 0xFF, 0xFF, 0xFF}
};
void function(const BYTE** values){
[Code] ....
You might notice that the above code doesn't compile, this is the error:
cannot convert parameter 2 from 'BYTE [2][4]' to 'BYTE *'
1>
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Even after some search I couldn't really find an answer to my problem, how do I pass the const BYTE array which I declared above in the function as a parameter (or what structure do I need to set for the function as a parameter)?
View 10 Replies
View Related
Nov 8, 2013
In my project, GreedyKnap::calKnap(int nItem, int nCapacity, float fWeights, float fProfits);
This function include two array member pass as parameter. how can i do this?
View 1 Replies
View Related
Jan 27, 2014
I'm creating simple console application using Code::Blocks to allow me to pass parameters from other application to replace string within text/registry file before execute the registry merge. Passing parameters to console already success. Now I only have problem with reading file. Example of first line in the registry file is as below.
Windows Registry Editor Version 5.00
However when read into string and output to console using 'cout', it will be show as below with spaces in between.
W i n d o w s R e g i s t r y E d i t o r V e r s i o n 5 . 0 0
Below is my code.
ifstream f("install.reg");
string s((istreambuf_iterator<char>(f)), istreambuf_iterator<char>());
cout << s;
View 6 Replies
View Related
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
Feb 2, 2015
I have a question similar to the one here: [URL] .....
The main difference is I would like to pass a method of derived class as a parameter to some other method in its template base class.
template <typename BaseType>
class Base {
public:
typedef void (Base::*Callback)(int A);
[Code] .....
The above is an example which does not compile. My compiler complains that the two BaseMethod() calls in DerivedMethod() are invalid uses of non-static member function.
Is this not possible to do, or is my syntax simply wrong? All I want is to be able to pass as an an argument to a method in the base class from the derived class some callback as a variable for the base class to invoke later.
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
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
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
Sep 12, 2013
How can I pass the name from the user input to the constructor ?
Code:
#include <iostream>
#include <string>
using namespace std;
class myClass {
[Code]....
View 6 Replies
View Related
Mar 6, 2015
how to pass a string from vb6 into the gsm module ... i am posting vb6 code ... below code passes
check1.caption=11BNELE0455 to MSCOMM2.output...
Static col As Integer
col = 3
lbl: If excelws.Cells(10, col).Value = "" Then
excelws.Cells(9, col).Value = Format(Now, "dd/mm/yyyy")
If (check1.Value = 1) And (Timer1.Enabled = True) Then
excelws.Cells(10, 1).Value = check1.Caption
[Code]...
now i want to access this string a=0455 from c-code of arduino..how can i access this.. below is c-code for arduino uno.what is wrong with it.it is giving me error:invalid conversion from 'int' to 'const char*'.
//decalaration part of my code
#include <SoftwareSerial.h>
char inchar[80][5]; // Will hold the incoming character from the GSMshield
SoftwareSerial SIM900(7, 8);
String textForSMS;
[Code]...
View 1 Replies
View Related
Feb 20, 2013
I am having trouble finding the solution to printing the contents of a variable. Here is my code:
void OnSize(HWND hwnd, UINT flag, int width, int height) {
std::wstringstream ss (std::wstringstream::in | std::wstringstream::out);
ss << flag;
std::wstring myStr = ss.str();
MessageBoxW(hwnd,myStr, L"Window Resized",NULL);
}
Note, I have #defined UNICODE at the beginning. The compiler tells me this for myStr.
std::wstring myStr
Error: No suitible conversion function from "std::wstring" to "LPCWSTR" exits
View 2 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
Jan 24, 2013
Why do we not pass the address of the string during printf or scanf functions like we do for Integer or float variable types?
View 2 Replies
View Related