In the following code, I'm getting only string a as output on my mingw compiler but I get both strings as output on an online compiler. Why am I not getting appropriate output on my system?
#include<iostream>
#include<cstring>
using namespace std;
int main(){
string a="Welcome";
string b="HelloWorld";
for(int i=0;i<=b.length();i++){
a.push_back(b[i]);
}
cout<<a<<endl<<b;
return 0;
}
Output-
WelcomeHelloWorld
one thing you can do is, you can use cout.flush(). If it does not work you can do a+b or you can use substr()
#include<iostream>
#include<cstring>
using namespace std;
int main(){
cout.flush();
string a="Welcome";
string b="HelloWorld";
cout<<a+b<<endl<<b;
// cout<<a+b.substr(0,9);
return 0;
}
I think cout.flush() sometimes help when getting unwanted output. So, once you can try this also before printing the result.
Related
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s="PIZZA";
int le=s.length();
for(int i=le-1;i=0;i--){
cout<<s[i];
}
}
What is the error here? I am not getting any output.
You 'd mean i >= 0 in the for loop.
Otherwise you never enter it. i = 0 results to 0 which results to false.
Please do learn how to use the debugger, you will solve most of your problems with it. Unrelated: Don't use using namespace std globally, avoid reverse-iterating for loops.
the condition of the loop is wrong,we want loop from last index of string into first the whole range so you should use i>=0 instead of i=0
worked code :
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string str = "PIZZA";
for(int i=str.length()-1;i>=0;i--)
cout<<str[i];
}
but you'd better know that with this code we only print string from end not Reverse IT !! to reverse we use this code :
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string str = "PIZZA";
string rev="";
for(int i=str.length()-1;i>=0;i--)
rev+=str[i];
cout<<"Reverse = "<<rev;
}
concat items from last of the string into new one !
You would have gotten your answer without asking us if you had turned on compiler warnings:
Why should I always enable compiler warnings?
That would have given you:
<source>: In function 'int main()':
<source>:9:21: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
9 | for(int i=le-1;i=0;i--){
| ~^~
Compiler returned: 0
So the compiler is saying: "You are performing an assignment, then using the result as a boolean value/condition. Are you sure that's what you want to do?"
Now, other answers told you what to replace i=0 with (it's i>=0, or possibly i != -1). But - with the warning above, I'm pretty sure you could have reached this conclusion yourself.
#include <iostream>
using namespace std;
int main(){
cout<<"Hello World!"<<endl;
}
I recently installed netbeans 12.0 and when I compile the code above, it says some weird words and numbers and a letter 'H'.
Also the words using, namespace, cout, and endl are underlined and when I hover to it, it says:
unable to resolve identifier + 'word'
This is the output:
PSID=1493
NBMAGIC=1492
H
RUN SUCCESSFUL (total time: 101ms)
As #user4581301 specified that the programs runs and immediately quits so you are not able to see anything. So, we need something to pause the program.
#include <iostream>
using namespace std;
int main(){
cout<<"Hello World!"<<endl;
system("pause");
}
So thing is I can copy paste unicode characters like chess pieces directly to terminal( I'm using debian jessie linux) but whenever I write c++ code to do that, I get these � instead
here is my code
enter code here
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
setlocale(LC_ALL,"");
wchar_t piece='♗';
wcout<<piece;
}
I tried to use the hex or decimal code of the characters but it does not work
I also use vim to edit and it does show the characters while I'm typing.
There's no specification of what encoding should be used for wchar_t. I need to use mbstowcs function to convert that character. Like this, for example:
#include <iostream>
#include <clocale>
#include <cstdlib>
using namespace std;
int main(void) {
setlocale(LC_ALL, "");
wchar_t piece;
mbstowcs(&piece, "♗", 1);
wcout << piece << endl;
return 0;
}
assuming your source file encoding matches the encoding of your locale.
Oddly enough what worked was going at it normally and putting the special character into a string it's so ridiculously simple I didn't even think to use it.
#include<iostream>
using namespace std;
int main()
{
string piece="♗";
cout<<piece;
}
I'm working on my first file handling in C++. I have written a simple program, but the problem is ofstream is now working in Dev C++. Any suggests?
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main(){
ofstream file_;
file_.open("mytext.txt");
file_ << "This is my first text file ";
file.close();
std::cin.get();
return 0;
}
As mindriot said in his comment, this is a compile error, use instead:
file_.close ();
Check if a string is palindrome
I was using the link above to try to solve this problem (among many others, Ive been trying to solve it various ways all day with no dice, this is my first C++). All other examples are usually in an array format, and I can't make assumptions as to the length of a word.
I'm trying to make a program to detect if a word is a palindrome or not. I have a text file with one word per line, and want to test each word, line by line, if it is a palindrome, and if so to print it to the screen, and if not, to ignore it and not print it.
I figured the best way to locate the palindromes was to reverse the word line by line and match it to the original, and if they are the same (==), then to print it. Here is what I have so far:
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;
int main(int argc, char *argv[]) {
std::string line;
std::ifstream infile("wordlist.txt");
}
string reverse(string line){
if (line == string(line.rbegin(), line.rend())) {
cout << string;
}
}
All help is appreciated
I guess your question is a homework question and you would like to get some information on how to complete the C++ coding.
You look not to know how to read file contents in C++.
Here's a link of how to do it:
Read file-contents into a string in C++
I am not very sure about what you specifically would like to be answered. If your question is a homework question, here's some info of how to ask:
https://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions
#include<iostream>
#include<algorithm>
#include<string.h>
#include<fstream>
using namespace std;
int main() {
string line="", line_rev="";
ifstream infile;
infile.open("wordlist.txt");
do{
infile>>line;
line_rev=line;
reverse(line_rev.begin(), line_rev.end());
if(line==line_rev)
cout<<line<<endl;
}while(getline(infile, line));
//if(infile.is_open()){cout<<"open"<<endl;} //to check if file is open or not
//else{cout<<"unable to open"<<endl;}
return 0;
}
This is the solution. i dont know why you are writing "string reverse(string line)" out side the main() function.