Why is this Code giving an "stray '\' in program" error? [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I came across this question while practicing Pointer questions and according to my understanding I thought option C would be correct but Option D was the correct answer, so I ran the code on VS Code and it did give a Compilation error.
Strings are terminated by NULL character then why an error is occurring if the for loop is checking for the occurrence of the NULL character.
Please can somebody explain what is the problem with this code? Here is the actual code:
#include<iostream>
using namespace std;
int main() {
char st[] = "ABCD";
for(int i = 0; st[i] != ‘\0’; i++) {
cout << st[i] << *(st)+i << *(i+st) << i[st];
}
return 0;
}

‘ (U+2018 LEFT SINGLE QUOTATION MARK) and ’ (U+2019 RIGHT SINGLE QUOTATION MARK) are Unicode characters, and your source file is saved in UTF-8.
You likely copied this code from some website which used these characters.
You need to use ASCII ' (U+0027 APOSTROPHE) instead:
st[i] != '\0'

Related

What is the problem with the following c++ code? [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 2 years ago.
Improve this question
The problem is 12289 - One-Two-Three from Online Judge at https://onlinejudge.org/external/122/12289.pdf
I have to compare a given string s with the following: "one","two","three", and return a number that indicates which of those has the most correct characters in a correct position with the string.
The following is my attempt at getting an accepted answer.
#include <cstdio>
using namespace std;
int main(){
char c;
int t,len,c1,c2;
scanf("%d\n",&t);
while(t--){
len = 0;
c1 = 0;
c2 = 0;
while(true){
scanf("%c",&c);
if(c=='\n') break;
if("one"[len] == c) c1++;
if("two"[len] == c) c2++;
len++;
}
if(len>3) printf("%d\n",3);
else if (c1>c2) printf("%d\n",1);
else printf("%d\n",2);
}
printf("\n");
}
I am getting a "Wrong answer" in this question, that usually does not involve formatting problems. I am new to C++ so it would help me a lot to know in what can I improve.
As Thomas says, you should check if len is > 3. Strings are basically character arrays terminated by the null byte or '\0'. In memory this is represented as ['o', 'n', 'e', '\0', ?, ...] wherein the ? is garbage value or, as C/C++ calls it, illegal memory access. So, there is a chance that "one"[4] == c or "two"[4] == c to be true since we do not know the value stored there. If that happens then the line
else if (c1>c2) printf("%d\n",1);
would have a problem.
Thank you for your support. I discovered that the real problem was the formatting of the input. There were blank lines between two consecutive inputs, so the part of if(c=='\n') break; was causing some trouble.
Anyways, I will try to remake the solution following C++ guidelines. I just did not know how to process a string, so I thought of doing it character by character. I'm closing the thread.

Remove whitespace in C++ string doesn't work [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have read these two questions already:
Remove spaces from std::string in C++
remove whitespace in std::string
For some reason, I can never get the solutions to work correctly. In my program, I collect input from the user and pass it to an std::string. From there, I want to remove all of the spaces in it. For example, if the user inputs "3 + 2", I would like it to change to "3+2".
What happens is, whatever is before the first string is kept. Here is my program:
#include <iostream>
std::string GetUserInput() {
std::cout << "Please enter what you would like to calculate: ";
std::string UserInput;
std::cin >> UserInput;
return UserInput;
}
int PerformCalculation(std::string Input) {
Input.erase(std::remove_if(Input.begin(), Input.end(), ::isspace), Input.end());
std::cout << Input;
return 0;
}
int main() {
std::string CalculationToBePerformed = GetUserInput();
int Solution = PerformCalculation(CalculationToBePerformed);
return 0;
}
So when I run this program and type in "3 + 2", the output is "3".
Here is my console:
Please enter what you would like to calculate: 3 + 2
3
Process finished with exit code 0
I cannot figure out how to resolve this. I even tried using a solution that involved using a regex to remove all the \s characters, and that gave me the same issue.
To read the complete line (up to terminating \n), you need to use e.g. std::getline(std::cin, UserInput);. Otherwise, you're currently reading text up to first whitespace character.

My codes work well In Dev c++ IDE, but in linux terminal, it doesn't. (especially in the part of 'while' loop.) [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 6 years ago.
Improve this question
while(true) {
getline(myfile, a[i]);
if (a[i]=="")//or if(a[i].empty())
break;
i++;
n = i;
}
In this while loop, when getline function gets a blank line from myfile object (there is a blank line between a series of binary numbers).
Example:
101010
000
11
1
00
<- when getline meets this line, by "if" break; has to work.
0011
10
00111
1101
But, it doesn't realize that blank line.
What is wrong?
What should I code to break when getline() meets the blank line?
I do this through PuTTY.
You are most likely running into the NL/CR issue.
Instead of
if (a[i]=="")
Use something like:
if (isEmptyLine(a[i]))
where
bool isEmptyLine(std::string const& s)
{
for ( auto c : s )
{
if ( !std::isspace(c) )
return false;
}
return true;
}
You can also convert the file into a file with UNIX style line endings by using a utility called dos2unix. That should also fix the problem.

ASCII values in table [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 8 years ago.
Improve this question
I want to initialise the table with all the ascii characters i.e. at 65-A, 66-B ....
Table abc;
for(int ascii=0;ascii<256;ascii++)
{
string a;
a=ascii;
abc.insertvalue(a,ascii);
//I have a class named table which has insertvalue function
}
The code shows error after inserting the 127th ascii character.
How can I modify it.
While debugging, it only printed till 127th position of array.
ASCII is a 7bit encoding. You should change the loop to while (ascii < 128).
And use a for loop:
for (int ascii = 0; ascii < 128; ++ascii) {
…
}

Strcmp() function not working properly [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
ifstream myfile;//file reading mode
myfile.open("file2.txt");//file opened
if(!myfile)
{
cout<<"your file cannot be opened";
}
for(;!myfile.eof();)
{
myfile>>name>>salary>>concerned_department;
cout<<name<<"\t"<<salary<<"\t"<<concerned_department<<"\n";
}
do
{
cout<<"To search an employee please enter the name of the employee<<"\n";
cin>>empName;//will take the string from the user.
cout<<empName<<"\n";
ifstream myfile;//file reading mode
myfile.open("file2.txt");//file opened successfully
if(strcmp(name,"empName")==0)//here the main problem lies
{
myfile>>name>>salary>>concerned_department;
cout<<name<<"\t"<<salary<<"\t"<<concerned_department<<"\n";
}
else
{//
cout<<"ERROR "could not be compared"<<"\n";
}
cout<<"Do you want to continue (y/n)";
cin>>con;
}
while(con=='y');
The strcmp() function is not comparing the strings though given. string1 is given in the file whereas the string2 is being taken from the user.
Use the following code:
if(strcmp(name,empName)==0)
{
...
}
Note that there must be no quotation marks around empName to compare its contents.