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.
Related
I want to use strings to input the path of files:
char** argv;
char* mytarget[2]={ (char*)"D:\\testlas\\BigOne.pcd",(char*)"D:\\testlas\\SmallOne.pcd" };
argv = mytarget;
for(int i=0;i<2;i++)
{
std::cout << "m.name: " << argv[i] <<std::endl;
}
However, cout outputs:
m.name: ?D:\\testlas\\BigOne.pcd
m.name: ?D:\\testlas\\SmallOne.pcd
Why is there a ? before the strings?
I use VS2017 C++11.
I created a new program and used the code:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
std::string test = "abc789";
cout << test << endl;
return 0;
}
It also outputs "?abc789". Why?
std::string test = "abc789";
There is a hidden LEFT-TO-RIGHT EMBEDDING character between the opening quote " and the first letter a (Unicode character U+202A, or UTF-8 E2 80 AA). Remove it, for example by deleting and retyping the line, then the ? will go away.
When i try to run a code with the function "strtok" in it i get the error code 0x80070002. I included cstring, cctype, string.h and i also tried using /DEBUG FULL in Properties - Linker - Debugging, like a few other posts said but it still doesn't work. Any clues why VS doesn't work with strtok? I also tried reinstalling VS and running a simple code like this:
#include <iostream>
#include <cstring>
#include <cctype>
#include <string.h>
using namespace std;
int main()
{
char s[100], * p;
cin.getline(s, 100);
p = strtok(s, " ");
cout << p;
return 0;
}
The desired behaviour would be to show me the first word of s. Even when i try to run the code at https://en.cppreference.com/w/cpp/string/byte/strtok i get the same error.
There are two methods to meet your needs;
Add _CRT_SECURE_NO_WARNINGS in Properties->C/C++->Preprocessor->Preprocessor Definitions.
Use strtok_s instead of strtok:
int main()
{
char *buf;
char s[100], *p;
cin.getline(s, 100);
p = strtok_s(s, " ", &buf);
cout << p;
return 0;
}
Hi so this code worked 5 minutes ago to do exactly what I needed it to do.
The data is:
Set Field [G],Sheet Resistivity (Gavg) [ohm/sqr]
0.0000E+0,
0.0000E+0,7.270620E+2
1.0000E-2,
1.0000E-2,7.271280E+2
-1.0000E-2,
-1.0000E-2,
-1.0000E-2,7.271290E+2
And my code for it is:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ifstream ip("/Users/10Exahertz/Documents/Hall Data/Test/data.txt");
if(!ip.is_open()) std::cout << "ERROR: File Open" << '\n';
string x;
string y;
while(getline(ip,x,',')){
getline(ip,y,'\n');
if(y!="")
std::cout <<x<<","<< y << '\n';
}
ip.close();
}
Like I said 5 mintues ago this worked, it got rid of the rows with an empty y string and all was good. But then I went back to the original data file and it didnt work there. I was confused so I put the original data into data.txt and now that one is not working either. Im honestly confused, but what would be the best condition in that if loop to make it so this works.
It sounds like you may have some whitespace that's crept in. I'd use the solution from this answer to trim the whitespace from your y string just to be sure:
#include <iostream>
#include <algorithm>
#include <cctype>
#include <locale>
#include <fstream>
using namespace std;
// trim from start (in place)
static inline void ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
return !std::isspace(ch);
}));
}
int main() {
[...]
while(getline(ip,x,',')){
getline(ip,y,'\n');
ltrim(y);
if(y!="")
std::cout <<x<<","<< y << '\n';
}
}
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);
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?