why string concatenation getting strange result in c++? [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am trying to concatenate two strings in C++:
"G1-2" + "-%02d.jpg"
and I am getting the following result:
G1-2-1537817269.jpg
Why isn't the result like this: "G1-2-%02d.jpg"

Wild guess!
You're printing the concatenated string by
printf(str);
where str is "G1-2-%02d.jpg"
printf("G1-2-%02d.jpg");
^^^^
// but, where is corresponding integer in the following?
As you can see there's a %02d in the string and printf will seek for a integer argument. It can not find it and undefined behavior occurs. In the best situation it prints out a random value with the string.
If my guess is true, then try to print the string in this form:
printf("%s",str);
or use double % as Chis mentined:
"G1-2-%%02d.jpg"

Related

How to pass a string into glutCreateWindow() function? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am reading a name from a file which I then use to name the window. However, it will not allow me to do that since it requires an ASCII character string . I can however do something like this directly
glutCreateWindow("StackOverflow").
Isn't this also a ASCII character string? Why am I able to do this but not something like this:
string x = "stack";
glutCreateWindow(x);
Is there a way to cast "x" to meet my needs?
Use the std::string::c_str() method:
std::string x = "stack";
glutCreateWindow(x.c_str());

declaring 2d char array and passing through strings? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Can someone explain what's happening in this line of code and how it's valid.
char output[][32] = {"Not present in trie", "Present in trie"};
It's declaring an array of n arrays of type char[32], where n is deduced from the number of initializers in the initializer list (in this case 2).
When the variable is initialized, the contents of the provided string literals are copied into the array (string literals can be used to initialize char arrays in C and C++).

Terminate a c++ string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Is there a way to terminate a C++ string at any arbitary location. This is very easy in C as we can just insert a null character wherever we want. But how can the same be achieved in C++ String.
For example, Let's consider the following example,
string str = "This is Stack OverflowXXXX";
Now I want to terminate this string so that I would get "This is Stack Overflow".
Yep! Use string::erase:
str.erase(k);
will erase all characters from position k forward. There's no way to "undo" this to get those characters back, though.
Hope this helps!

Regex Binary multiple of 4 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I write a Reg-ex Expression to check whether a string is a binary multiple of 4? I am not good at making DFA and finding expressions.
A multiple of 4 in binary is any binary number that ends with 00, so this regexp should do it:
^(?:[10]*00|00?)$
If you mean a multiple of 4 in decimal, I wouldn't do that with a regexp, except perhaps to verify that it's a number. Then I'd parse it and check whether number % 4 is zero.

Getting Spaces not a space as output in c++ program [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
In a c++ program how to get an output like the following one:
* *
Basically, for example how to get three spaces between these two asterisks as an output?
#include <iostream>
int main() { std::cout << "* *"; }