Confusing error requesting semicolon (error C2143) - c++

I am using VS Professional 2013 and I am building a console application. One of my methods is determining string length from user input. I keep receiving a confusing error on line 5 of this method: that there is a missing semicolon (error C2143). No functions within the line require an extra semicolon to my knowledge. Also, I am intentionally not calling namespaces. The #include functions are replicated below and are stored in the header file.
#include <stdio.h>
#include <tchar.h> //Part of VS' implementation for applications. Can effectively be ignored.
#include <iostream>
#include <string>
int main() {
std::string s;
std::cout << "Enter your string: " << std::flush;
std::string.getline(std::cin, s);
const int size = s.length();
std::cout << "The total number of characters entered is: " << size << std::endl;
}

std::string has no member getline, so std::string.getline(std::cin, s); is illegal.
You want
std::getline(std::cin, s);

Related

Cout and Cin throwing errors despite using headings #include<iostream> [duplicate]

Why I cannot cout string like this:
string text ;
text = WordList[i].substr(0,20) ;
cout << "String is : " << text << endl ;
When I do this, I get the following error:
Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\users\mollasadra\documents\visual studio 2008\projects\barnamec\barnamec\barnamec.cpp 67 barnamec**
It is amazing, that even this is not working:
string text ;
text = "hello" ;
cout << "String is : " << text << endl ;
You need to include
#include <string>
#include <iostream>
You need to reference the cout's namespace std somehow. For instance, insert
using std::cout;
using std::endl;
on top of your function definition, or the file.
There are several problems with your code:
WordList is not defined anywhere. You should define it before you use it.
You can't just write code outside a function like this. You need to put it in a function.
You need to #include <string> before you can use the string class and iostream before you use cout or endl.
string, cout and endl live in the std namespace, so you can not access them without prefixing them with std:: unless you use the using directive to bring them into scope first.
Above answers are good but If you do not want to add string include, you can use the following
ostream& operator<<(ostream& os, string& msg)
{
os<<msg.c_str();
return os;
}
You do not have to reference std::cout or std::endl explicitly.
They are both included in the namespace std. using namespace std instead of using scope resolution operator :: every time makes is easier and cleaner.
#include<iostream>
#include<string>
using namespace std;
Use c_str() to convert the std::string to const char *.
cout << "String is : " << text.c_str() << endl ;
If you are using linux system then you need to add
using namespace std;
Below headers
If windows then make sure you put headers correctly
#include<iostream.h>
#include<string.h>
Refer this it work perfectly.
#include <iostream>
#include <string>
int main ()
{
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (3,5); // "think"
std::size_t pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos);
// get from "live" to the end
std::cout << str2 << ' ' << str3 << '\n';
return 0;
}

Assertion failure in std::ispunct in a simple program

I'm using the book C++primer by Stanley B.Lippman and this error is caused by the solution of Excersise 3.2.3 test 3.10.It requires that write a program that reads a string of characters including punctuation and writes what was read but with the punctuation removed.
here's the code:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
string s;
cout << "Please input a string of characters including punctuation:" << endl;
getline(cin, s);
for (auto c : s) {
if (!ispunct(c))
cout << c;
}
cout << endl;
return 0;
}
when I run this code in Visual studio 2017 it shows this:
Debug Assertion failed.
Expression:c>=-1&&c<=255
For information on how your program can cause an assertion failure,see the Visual C++ documentation on asserts.
why it shows like this? I can't understand.
Although the assertion failure you get is due to a bad call to std::ispunct() (you should iterate over the string with an unsigned char), the proper solution would be to use std::iswpunct:
#include <iostream>
#include <string>
#include <locale>
#include <cwctype> // std::iswpunct
int main()
{
std::wstring s;
do {
std::wcout << "Please input a string of characters including punctuation:\n";
} while (!std::getline(std::wcin, s));
for (auto c : s) {
if (!std::iswpunct(c))
std::wcout << c;
}
std::wcout << std::endl;
}
On a Windows platform, the conjunction of std::wstring1 and std::iswpunct will let you handle Chinese characters right. Note that I assumed your system locale is "zh_CH.UTF-8". If it is not, you'll need to imbue your streams.
1) see this excellent answer about the difference between string and wstring.

This pointer to print strings C++ [duplicate]

Why I cannot cout string like this:
string text ;
text = WordList[i].substr(0,20) ;
cout << "String is : " << text << endl ;
When I do this, I get the following error:
Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\users\mollasadra\documents\visual studio 2008\projects\barnamec\barnamec\barnamec.cpp 67 barnamec**
It is amazing, that even this is not working:
string text ;
text = "hello" ;
cout << "String is : " << text << endl ;
You need to include
#include <string>
#include <iostream>
You need to reference the cout's namespace std somehow. For instance, insert
using std::cout;
using std::endl;
on top of your function definition, or the file.
There are several problems with your code:
WordList is not defined anywhere. You should define it before you use it.
You can't just write code outside a function like this. You need to put it in a function.
You need to #include <string> before you can use the string class and iostream before you use cout or endl.
string, cout and endl live in the std namespace, so you can not access them without prefixing them with std:: unless you use the using directive to bring them into scope first.
Above answers are good but If you do not want to add string include, you can use the following
ostream& operator<<(ostream& os, string& msg)
{
os<<msg.c_str();
return os;
}
You do not have to reference std::cout or std::endl explicitly.
They are both included in the namespace std. using namespace std instead of using scope resolution operator :: every time makes is easier and cleaner.
#include<iostream>
#include<string>
using namespace std;
Use c_str() to convert the std::string to const char *.
cout << "String is : " << text.c_str() << endl ;
If you are using linux system then you need to add
using namespace std;
Below headers
If windows then make sure you put headers correctly
#include<iostream.h>
#include<string.h>
Refer this it work perfectly.
#include <iostream>
#include <string>
int main ()
{
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (3,5); // "think"
std::size_t pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos);
// get from "live" to the end
std::cout << str2 << ' ' << str3 << '\n';
return 0;
}

Unable to use cout with a C++ string unless I run it through data() or c_str()

On the following program, I'm getting this when I attempt to use cout to output a C++ string to stdout - the other instructions produce the expected output. I'm using MS Visual Studio 2010 on a Windows 7 system.
First-chance exception at 0x00dd4e89 in Lab1.exe: 0xC00000FD: Stack
overflow. Unhandled exception at 0x00dd4e89 in Lab1.exe: 0xC00000FD:
Stack overflow. The program '[3740] Lab1.exe: Native' has exited with
code -1073741571 (0xc00000fd).
#include "StdAfx.h"
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <ostream>
#include <string>
#include <ctime>
//more code here
int main() {
int number = 1;
string myStr = "Hello, string!";
cout << "number: " << number << endl;
cout << "Hello, World!" << endl;
cout << myStr << endl; //failing instruction
cout << "\nHit any key to continue...." << endl;
cin.get();
return 0;
}
My instructor suggested changing the failing instruction to use data() or c_str() like so:
cout << myStr.data() << endl;
I did this, and this resolved the problem. He didn't know why, just said it worked so not to worry about it.
It seems to me that a C++ ostream object like cout should be able to handle a C++ string. Am I missing something, or do I really need to use data() or c_str() with cout?
I also tried using std::cout, std::string, and std::endl - it didn't help.
Thanks in advance for your advice; I'm really wanting to understand what's going on here.
Helen
You should include string instead of string.h:
#include <string>
I doubt that cout << myStr << endl; was the troublesome line.
This code works fine:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string s("Hello World!");
cout << s << endl;
return 0;
}
The error message indicates that you have a stack overflow: it seems some function is being called recursively. You didn't define your own output function for string by any chance? What is in "more code here" which may be related to output operators?

Why I cannot cout a string?

Why I cannot cout string like this:
string text ;
text = WordList[i].substr(0,20) ;
cout << "String is : " << text << endl ;
When I do this, I get the following error:
Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\users\mollasadra\documents\visual studio 2008\projects\barnamec\barnamec\barnamec.cpp 67 barnamec**
It is amazing, that even this is not working:
string text ;
text = "hello" ;
cout << "String is : " << text << endl ;
You need to include
#include <string>
#include <iostream>
You need to reference the cout's namespace std somehow. For instance, insert
using std::cout;
using std::endl;
on top of your function definition, or the file.
There are several problems with your code:
WordList is not defined anywhere. You should define it before you use it.
You can't just write code outside a function like this. You need to put it in a function.
You need to #include <string> before you can use the string class and iostream before you use cout or endl.
string, cout and endl live in the std namespace, so you can not access them without prefixing them with std:: unless you use the using directive to bring them into scope first.
Above answers are good but If you do not want to add string include, you can use the following
ostream& operator<<(ostream& os, string& msg)
{
os<<msg.c_str();
return os;
}
You do not have to reference std::cout or std::endl explicitly.
They are both included in the namespace std. using namespace std instead of using scope resolution operator :: every time makes is easier and cleaner.
#include<iostream>
#include<string>
using namespace std;
Use c_str() to convert the std::string to const char *.
cout << "String is : " << text.c_str() << endl ;
If you are using linux system then you need to add
using namespace std;
Below headers
If windows then make sure you put headers correctly
#include<iostream.h>
#include<string.h>
Refer this it work perfectly.
#include <iostream>
#include <string>
int main ()
{
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (3,5); // "think"
std::size_t pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos);
// get from "live" to the end
std::cout << str2 << ' ' << str3 << '\n';
return 0;
}