[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

quick question - string

Posted on 2005-04-23
6
Medium Priority
?
202 Views
Last Modified: 2012-05-05
hello experts,

I'm very beginner in C++ so I need helpwith two problems.
problem 1:
I have non-void function which returs string:



string TestString(int Integer){
        return "test";
  }

I declared function prototypes and everything works fine until I want to cout this string.

cout << TestString(1);

I want to make this function to read integer from 1-12 and return string  for example:
1-january
2-february
and so on...

problem 2:
how can I connect string and variable together and print it out. I what to make error message consist from string(text) and variables is it possible?

example:

string errormesage = "here some text " AND variable AND " again some text";

thank you

./lubo


        
0
Comment
Question by:Luxana
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 600 total points
ID: 13852337
The following code snippet addresses both of your questions:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;


string TestString(int Integer){
      return "test";
 }

int main () {

cout << TestString(1).c_str() << endl;

int variable = 42;

stringstream errormessage;

errormessage << "here some text " << variable << " again some text";

cout << errormessage.str().c_str() << endl;

return 0;
}

To the 1st one, you need to call 'string::c_str()' to use the correct operator for 'cout', to the 2nd one, the most efficiant and flexible way to do the formatting you suggested is using a stringstream, as demonstrated above. You can use it just like any other stream and access the contents as shown when you're done.
0
 
LVL 10

Author Comment

by:Luxana
ID: 13852367
HI

thanks for help. I started using unix g++ compiler and everything is working there without c_str() and #include <sstream> . ANyway I will try your solution as soon as I get back  to windows because I definitely need to make sure that my program can be compiled with visual C++ as well.

thanks again
0
 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 1000 total points
ID: 13852480
The first part should work without any changes

When u return a string object, actually a temporary string object gets cnstructed and is returned.
And every temporary object in C++ is implicitly const.
Hence, acually, the object that gets subsstituted in the cout << is a const string.

Now, << is actally an overloaded operator and the syntax  is

ostream& operator<<( ostream& os, const string& s );

http://cppreference.com/cppstring/string_operators.html

Now, as u can see, the << operator expects a reference to an ostream type of object on its left hand side (i.e. cout in your case) and a reference to a const string on right hand side (thats the returned string object in your case)
Hence it should work perfectly fine and It does with GNU compiler, as per the C++ standards


Also, regadring your 2nd question, u will  have to use stringstream object as shown above
Below is a reference for the same
http://www.cplusplus.com/ref/iostream/stringstream/
http://www.cplusplus.com/ref/iostream/stringstream/str.html

HTH
Amit


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Assisted Solution

by:efn
efn earned 400 total points
ID: 13852625
As Amit implied, you shouldn't need to call the c_str function to put a string out to a stream.

Possibly your problem was that output to the stream does not appear until you send an endl as in jkr's example.  Writing a '\n' character might also work, but an endl should definitely work.  Until the program writes an endl, the output data sits in a buffer, but may not be visible on the output device or window.

If that's not the problem, I think we would need to see more code to advise you further.
0
 
LVL 10

Author Comment

by:Luxana
ID: 13857546
thanks guys for help this fix the problem: #include <sstream> it is interenstig because g++ compiler get my code work without  preprocessor directive <sstream>. Sys_Prog  was right that I do not need to do any changes so I accept his answer && jrk's && efn as assist .

thanks again

./lubo
0
 
LVL 10

Author Comment

by:Luxana
ID: 13857562
oops I did accepts jrk's answer I will ask page editor to change it.

sorry
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question