473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

string and integer combined

Hello,
I want to combine a string with an integer. I work on SuSE 9.1 with g++. Why
does this piece of code not work?

#include <iostream.h>
#include <stdio.h>
#include <fstream.h>
#include <string>
....
intvar intg = 0;
std::string arraystring = "array" + intvar +".dat";
....

g++ gives the following error message:

error: invalid operands of types `const char*' and `const char[5]' to binary
`operator+'

Thanks for your help,

Bernhard
Jul 22 '05 #1
5 5950
Bernhard Hidding wrote:
Hello,
I want to combine a string with an integer. I work on SuSE 9.1 with
g++. Why does this piece of code not work?

#include <iostream.h>
#include <iostream>
#include <stdio.h>
#include <fstream.h>
#include <fstream>
#include <string>
...
intvar intg = 0;
std::string arraystring = "array" + intvar +".dat";
...

g++ gives the following error message:

error: invalid operands of types `const char*' and `const char[5]' to
binary `operator+'


"array" is of type array of char. When used, it "decays" into a pointer
to char. If you add an int to a pointer, the result is another pointer,
with the int value as offset. So if intvar had the value 3, the
resulting pointer would point to the second 'a' of "array". Then, you
try to add ".dat" to it, which also decays into a pointer. And you
can't add two pointers together. Try this instead:

std::stringstream stream;
stream << "array" << intvar << ".dat";
std::string arraystring = stream.str();

Jul 22 '05 #2
> std::stringstream stream;
stream << "array" << intvar << ".dat";
std::string arraystring = stream.str();


This does not work: g++ says

"aggregate `std::stringstream stream' has incomplete type and cannot be
defined "

Bernhard

Jul 22 '05 #3
Bernhard Hidding wrote:
I want to combine a string with an integer. I work on SuSE 9.1 with g++. Why
does this piece of code not work?
Because it's not standard C++.
#include <iostream.h>
#include <stdio.h>
#include <fstream.h>
#include <string>
I am not even going to say anything about .h headers here.
...
intvar intg = 0;
std::string arraystring = "array" + intvar +".dat";
...

g++ gives the following error message:

error: invalid operands of types `const char*' and `const char[5]' to binary
`operator+'


What effect are you looking for? When you're done figuring that
out, explain me how it would work in this case (and what the output
should be):

#include <string>
#include <iostream>

int main() {
int index[] = { 0, 5, 10 };
const char keywords[] = "HELO\0OPEN\0EXIT\0";
std::string helostr(keywords + index[0]);
std::string openstr(keywords + index[1]);
std::string exitstr(keywords + index[2]);

std::cout << helostr << ' ' << openstr << ' ' << exitstr << '\n';
}

Victor
Jul 22 '05 #4
Bernhard Hidding wrote:
std::stringstream stream;
stream << "array" << intvar << ".dat";
std::string arraystring = stream.str();


This does not work: g++ says

"aggregate `std::stringstream stream' has incomplete type and cannot
be defined "

you need to #include <sstream>

Jul 22 '05 #5
Bernhard Hidding wrote:
Hello,
I want to combine a string with an integer. I work on SuSE 9.1 with g++. Why
does this piece of code not work?

#include <iostream.h>
#include <stdio.h>
#include <fstream.h>
#include <string>
...
intvar intg = 0;
std::string arraystring = "array" + intvar +".dat";
...

g++ gives the following error message:

error: invalid operands of types `const char*' and `const char[5]' to binary
`operator+'

Thanks for your help,

Bernhard


Because of precedence rules for addition of arrays and pointers. Also
because the string class has no constructors for converting from int to
string nor for appending an integer quantity.

In a string concatenation situation, the operator '+' is overloaded to
perform concatenation:
array_string = "array" + " hello";
which is equal to:
array_string = string("array").operator+(string(" hello"));

Applying the above to a string concatenated with an integer would
look like:
array_string = "array" + 4; /* numeric const or int variable */
array_string = string("array").operator+(4);
In order to accomplish what you want, the string class would
need a method of:
string& string::operator+(int);
which doesn't exist.

However, some non-standard string libraries have constructors
that can take integral arguments and convert them to strings.
So, assuming a specialized string class called AnsiString that
has such an operator, you would want:
AnsiString arraystring;
arraystring = "array" + AnsiString(intvar) + ".dat";

Look up istringstream and ostringstream. Another alternative
is to use sprintf.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
6
by: Tony Liu | last post by:
Hi, how to create a 32-bit integer from 2 16-bit integer? Thanks Tony
6
by: juli | last post by:
Hello dear Cor or anyone around!:) This didn't help me(convert) : I have 3 strings in an array : str='11/02/04' ,str='11:23:00" and str=AM. How do I convert str+str+str to a proper datetime...
3
by: Marty Underwood | last post by:
Got a quick question that I cannot get to work at work! I have a field in SQL server that is set as a varchar but contains data like >50 or <2 which is actually a target percentage. I have data...
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
7
by: Brian Mitchell | last post by:
Is there an easy way to pull a date/time stamp from a string? The DateTime stamp is located in different parts of each string and the DateTime stamp could be in different formats (mm/dd/yy or...
40
by: Robert Seacord | last post by:
The CERT/CC has released a beta version of a secure integer library for the C Programming Language. The library is available for download from the CERT/CC Secure Coding Initiative web page at:...
15
by: Ivan Novick | last post by:
Hi, Is it possible to have negative integer literal or only positive? As far as I understand, the code below would be a positive integer literal and the unary negative operator. x = -3.2; ...
2
by: Chicken15 | last post by:
Hi Group. First of all I'm sorry for asking (maybe) such easy questions. But I'm quite stuck now and couldn't come up with a solution by using my C# book or googling. So it would be nice if...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.