ASCII values in table [closed] - c++

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) {
…
}

Related

Why is this Code giving an "stray '\' in program" error? [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 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'

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.

Deleting undesired characters at the end of a char [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
With this code below I dont know how to delete the undesired characters appearing at the end of the message array. It is compulsory for me to use char, can't use strings, because of the rest of my code.
recvbuf is also a char* recvbuf=new char
char* message=new char[140];
for (int i=1; i<141; i++){
message[i-1]=recvbuf[i];
}
printf("Message: %s\n", message);
delete[]recvbuf;
Though it is recommended you use strings to implement this code, the problem can be fixed by manually appending a null character \0 at the end of your char array.
You can introduce it as:
char* message=new char[141];
for (int i=1; i<141; i++){
message[i-1]=recvbuf[i];
}
message[140] = '\0'; //newly introduced line.
printf("Message: %s\n", message);
delete[]recvbuf;
NOTE 1: The size of the array was increased from 140 to 141 during initialization to make room for the \0 character at the end.
Cheers!

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.

Extracting integers from a ID string [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 7 years ago.
Improve this question
I'm trying to get the number 001 from the string UID001 (The user will always enter "UIDnnn"). I tried using cin.get and cin.ignore with delimiters but with no results. What is the most effective way to extract the integers from a string?
Well, the simplest c++ standard compliant solution seems to be as mentioned in my comment.
Read the ID value as a std:string:
std::string ID;
std::cin >> ID;
Get the substring with the number part:
std::string numpart = ID.substr(3);
Convert it to a numerical value:
int idnum = std::stoi(numpart);
This is one of those places that scanf and company really work well:
scanf("UID%d", &number);
With iostreams it's a bit uglier, but I'd consider something like:
for (int i=0; i<3; i++) {
char ch;
cin.get(ch);
}
cin >> number;