expected expression in c++ [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
No output for cout
i writed this code in c++ for my uni , but i have an error in return 0 , the code don't work . i am using xcode to develop
#include <iostream>
#include <string>
using std::string;
int main( void )
{
string portF("PORTOFINO IM SOMMER 2012");
std::cout<<portF<<
portF.erase(0,5);
portF.insert(3,"IT");
portF.erase(7,3);
portF.insert(13,"SEMESTER");
portF.append("!");
std::cout<<portF<<
return 0;
}

std::cout<<portF<<
should be
std::cout<<portF;
Note you've made the same error twice. A semicolon is what ends a statement. When you put an insertion operator instead of it, compiler expects another expression (and that's what it is telling you).

use:
std::cout<<portF;
instead of,
std::cout<<portF<<
1. You haven't added a semicolon after the statement.
2. you are using one extra << operator

#include <iostream>
#include <string>
using std::string;
int main( void )
{
string portF("PORTOFINO IM SOMMER 2012");
// You have to end this statement with semi colon
std::cout<<portF;
portF.erase(0,5);
portF.insert(3,"IT");
portF.erase(7,3);
portF.insert(13,"SEMESTER");
portF.append("!");
// Similarily here
std::cout<<portF;
return 0;
}

Related

Reversing a string with C++

#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.

How to insert data into an array of single structure data type [duplicate]

This question already has answers here:
Single quotes vs. double quotes in C or C++
(15 answers)
Closed 1 year ago.
Im trying to put data into an array
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
struct table {
int number;
double rate, hour;
string name;
} test[5];
int main()
{
test[0]={2,0.0,1.1,'m'};
test[1]={2,0.0,1.1,'m'};
return 0;
}
I know the syntax is wrong for this test[0]={2,0.0,1.1,'m'};. please correct it.
You have the wrong type of quotes: 'm' is a char but you need a std::string. Change it to "m" and your code compiles.

Why did I get error by using strchr() in C++? [duplicate]

This question already has answers here:
C++ string equivalent for strrchr
(4 answers)
Closed 6 years ago.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main(){
string a="asdasd";
if(!strchr(a,'a')) cout<<"yes";
return 0;
}
I just began to learn C++ programming and I don't know why I got error in this line
if(!strchr(a,'a')) cout<<"yes";
But if I tried to code it like this, it would run very well.
if(!strchr("asdasd",'a')) cout<<"yes";
I know it is a stupid question but I really don't know why.. sorry..
The library function strchr is for use with C-style strings, not the C++ string type.
When using std::string, the closest equivalent of strchr is find:
#include <iostream>
#include <string>
int main(){
std::string a="asdasd";
if(a.find('a') != std::string::npos) std::cout<<"yes";
}

"gets() was not declared in this scope" error [duplicate]

This question already has answers here:
why g++ shows "gets()" not declared ,even after including <cstdio>
(3 answers)
Closed 3 years ago.
With the following code, I get the "gets() was not declared in this scope" error:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
// string str[]={"I am a boy"};
string str[20];`
gets(str);
cout<<*str;
return 0;
}
The function std::gets() was deprecated in C++11 and removed completely from C++14.
As gets() is a C style function, so if you need to include it in your c++ code then you need to include the header file called stdio.h and moreover you can only pass a c style string to gets() function not c++ string class.
So after slight modification in your code it becomes:
#include <iostream>
#include <string.h>
#include "stdio.h"
using namespace std;
int main()
{
// string str[]={"I am a boy"};
char str[20];`
gets(str);
printf("%s",str);
return 0;
}

How to use C++11 std::stoi with gcc? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert a number to string and vice versa in C++
I am using Qt Creator 2.5.0 and gcc 4.7 (Debian 4.7.2-4). I added "QMAKE_CXXFLAGS += -std=c++11" to .pro file. Everything seems to be OK, I used C++11 std::for_each and so on. But when I included "string" header and wanted to use stoi, i got the following error:
performer.cpp:336: error: 'std::string' has no member named 'stoi'
I found some questions related to MinGW and one more, to Eclipse CDT and they had their answers. But I use Linux, why it is NOT working here?
#include <iostream>
#include <string>
int main()
{
std::string test = "45";
int myint = stoi(test);
std::cout << myint << '\n';
}
or
#include <iostream>
#include <string>
using namespace std
int main()
{
string test = "45";
int myint = stoi(test);
cout << myint << '\n';
}
look at http://en.cppreference.com/w/cpp/string/basic_string/stol
std::stoi is a function at namespace scope, taking a string as its argument:
std::string s = "123";
int i = std::stoi(s);
From the error message, it looks like you expect it to be a member of string, invoked as s.stoi() (or perhaps std::string::stoi(s)); that is not the case. If that's not the problem, then please post the problematic code so we don't need to guess what's wrong with it.