Compilation error in CRC encoding program - c++

I wrote the following code for a program that performs CRC encoding. I modeled it on a C program that we were taught in class. It gives some compilation errors that I can't correct. I have mentioned them after the code.
I wrote the following code for a program that performs CRC encoding. I modeled it on a C program that we were taught in class. It gives some compilation errors that I can't correct. I have mentioned them after the code.
1 #include<iostream>
2 #include<string.h>
3
4 using namespace std;
5
6 class crc
7 {
8 char message[128],polynomial[18],checksum[256];
9 public:
10 void xor()
11 {
12 for(int i=1;i<strlen(polynomial);i++)
13 checksum[i]=((checksum[i]==polynomial[i])?'0':'1');
14 }
15 crc() //constructor
16 {
17 cout<<"Enter the message:"<<endl;
18 cin>>message;
19 cout<<"Enter the polynomial";
20 cin>polynomial;
21 }
22 void compute()
23 {
24 int e,i;
25 for(e=0;e<strlen(polynomial);e++)
26 checksum[e]=message[e];
27 do
28 {
29 if(checksum[0]=='0')
30 xor();
31 for(i=0;i<(strlen(polynomial)-1);i++)
32 checksum[i]=checksum[i+1];
33 checksum[i]=message[e++];
34 }while(e<=strlen(message)+strlen(checksum)-1);
35 }
36 void gen_proc() //general processing
37 {
38 int mesg_len=strlen(message);
39 for(int i=mesg_len;i<mesg_len+strlen(polynomial)-1;i++)
40 message[i]='0';
41 message[i]='\0'; //necessary?
42 cout<<"After appending zeroes message is:"<<message;
43 compute();
44 cout<<"Checksum is:"<<checksum;
45 for(int i=mesg_len;i<mesg_len+strlen(polynomial);i++)
46 message[i]=checksum[i-mesg_len];
47 cout<<"Final codeword is:"<<message;
48 }
49 };
50 int main()
51 {
52 crc c1;
53 c1.gen_proc();
54 return 0;
55 }
The compilation errors are:
crc.cpp:10: error: expected unqualified-id before ‘^’ token
crc.cpp: In member function ‘void crc::compute()’:
crc.cpp:30: error: expected primary-expression before ‘^’ token
crc.cpp:30: error: expected primary-expression before ‘)’ token
crc.cpp: In member function ‘void crc::gen_proc()’:
crc.cpp:41: warning: name lookup of ‘i’ changed for ISO ‘for’ scoping
crc.cpp:39: warning: using obsolete binding at ‘i’
I have been checking online for these errors and the only thing I have been seeing is errors caused by incorrect array handling. I have double checked my code but I don't seem to be performing any incorrect array access.

xor is a reserved keyword in C++. You should rename the function to something else.
The compiler isn't actually "seeing" an identifier, but a keyword. If, in your code snippet, you'd replace xor with ^ the obvious syntactic error becomes clear.

Related

Function not updating internal state of mt19937 [duplicate]

This question already has answers here:
C++ function not updating variables
(1 answer)
c++ trying to change local variable outside of function?
(1 answer)
Closed 7 months ago.
I have a function that generates and writes random integers:
void randint(int min, int max, int times,std::mt19937 rng){
std::uniform_int_distribution<int> dist(min, max);
for (int i=0;i<times;i++){
std::cout<<dist(rng)<<' ';
}
}
When this function is called, generates numbers from the mt19937 object. However, when returning from the function, the mt19937 object's internal state is not updated.
This can be seen by running this code below.
int main(){
std::mt19937 gen(2845);
std::uniform_int_distribution<int> spread(1,100);
randint(1,100,5,gen);
std::cout<<"| ";
for (int i=0;i<5;i++){
std::cout<<spread(gen)<<' ';
}
Actual Output:
88 93 79 43 92 | 88 93 79 43 92
Expected output:
88 93 79 43 92 | 97 71 34 32 70
Is there any way for the internal state to be continuously updated even when passed through the function?
You are passing the object by value (i.e. you are copying the object when you call the function). Use a reference
void randint(int min, int max, int times,std::mt19937& rng){

How to print table of 12 using recursion, I want it upto 10 but on running it turns out to be upto 12

I was trying to create a program to print table of 12 using recursion as I wrote a simple program for this I did get table, but table was instead upto 144 (12times12=144) instead of 120(12times10=120) I am sharing my code and output with you guys I was writing code in C++
//we will print table of 12 using concept of recursion
//a table of 12 is like this
//12 24 36 48 60 72 84 96 108 120
#include<iostream>
using namespace std;
void table(int n)
{
if(n==1)
{
cout<<12<<"\n";
return;
}
table(n-1);
cout<<n*12<<"\n";
}
int main(void)
{
table(12);
}
and now here is out put of this program
12
24
36
48
60
72
84
96
108
120
132
144
please help me what I'm missing here I am positive that adding some condition will help I tried one adding if(n==12) { return;} but it prevents does nothing as in the end it is return n*12

Error related to EOF command in fortran code

I am a beginner to Fortran and am trying to compile a Fixed-Term Fortran Code using gfortran. I got a bunch of errors, which I could fix them. However, there is an Error related to "EOF" which I could not solve it. Is there any way to fix this problem? (The two "EOF" lines are lines 40 and 121.)
37 OPEN(4,FILE="ABCE.Pn")
38
39 OPEN(5,FILE="../sta.txt")
40 DO WHILE (.not.EOF(5))
41 N=N+1
42 READ(5,*)STA(N)%COD,STA(N)%NAME,STA(N)%LAT,
43 $ STA(N)%LON,STA(N)%H
44 ENDDO
45 NSTA=N
46 CLOSE(5)`
......
121 DO WHILE (.not.EOF(1))
122 READ(1,'(A60)',ERR=999) TIT
123 C IF(IYEAR.GE.2008.OR.
(IYEAR.EQ.2007.AND.MONTH.GE.11))
124 C $ TIT=TIT(2:60)
125 IF(TIT(1:60).EQ.'')THEN ! NEW EARTHQUAKE`
The error:
DO WHILE (.not.EOF(5))
1
Error: Operand of .not. operator at (1) is REAL(4)
ReadP2Pn.for:121.21:
DO WHILE (.not.EOF(1))
1
Error: Operand of .not. operator at (1) is REAL(4)
EOF(5) is non-standard. You should check for EOF in the read statement (which sadly looks like a goto) :
40 DO WHILE (.true.)
41 N=N+1
42 READ(5,*,end=990)STA(N)%COD,STA(N)%NAME,STA(N)%LAT,
43 $ STA(N)%LON,STA(N)%H
44 ENDDO
45 990 NSTA=N

Does view::join require copyable inner range? Why?

Suppose that we have
cppcoro::generator<int> gen_impl(int in) {
const auto upper = in + 10;
for (; in < upper; ++in)
co_yield in;
}
cppcoro::generator<cppcoro::generator<int>> gen() {
for (int n = 1; n < 100; n += 10)
co_yield gen_impl(n);
}
So we can iterate inner range just fine
for (auto&& row : gen() ) {
for (auto n : row)
std::cout << n << ' ';
std::cout << '\n';
}
NOTE: range-for on ref is required because cppcoro::generator doesn't allow copying (deleted copy ctor)
Print
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
But when we try to "flattern" with view::join
auto rng = gen();
for (auto n : rng | ranges::view::join) {
std::cout << n << '\n';
};
It seems view::join require Copyable inner range?
In file included from <source>:3:
In file included from /opt/compiler-explorer/libs/rangesv3/trunk/include/range/v3/view.hpp:38:
In file included from /opt/compiler-explorer/libs/rangesv3/trunk/include/range/v3/view/for_each.hpp:23:
/opt/compiler-explorer/libs/rangesv3/trunk/include/range/v3/view/join.hpp:320:50: error: call to deleted constructor of 'cppcoro::generator<cppcoro::generator<int> >'
return join_view<all_t<Rng>>{all(static_cast<Rng&&>(rng))};
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/libs/rangesv3/trunk/include/range/v3/view/view.hpp:112:21: note: in instantiation of function template specialization 'ranges::v3::view::join_fn::operator()<cppcoro::generator<cppcoro::generator<int> > &, false, nullptr>' requested here
v.view_(static_cast<Rng&&>(rng))
^
/opt/compiler-explorer/libs/rangesv3/trunk/include/range/v3/utility/functional.hpp:731:42: note: in instantiation of function template specialization 'ranges::v3::view::view<ranges::v3::view::join_fn>::pipe<cppcoro::generator<cppcoro::generator<int> > &, ranges::v3::view::view<ranges::v3::view::join_fn> &, false, nullptr>' requested here
pipeable_access::impl<Pipe>::pipe(static_cast<Arg&&>(arg), pipe)
^
<source>:35:21: note: in instantiation of function template specialization 'ranges::v3::operator|<cppcoro::generator<cppcoro::generator<int> > &, ranges::v3::view::view<ranges::v3::view::join_fn>, false, nullptr>' requested here
for (auto n : rng | ranges::view::join) {
^
/opt/compiler-explorer/libs/cppcoro/include/cppcoro/generator.hpp:174:3: note: 'generator' has been explicitly marked deleted here
generator(const generator& other) = delete;
^
/opt/compiler-explorer/libs/rangesv3/trunk/include/range/v3/view/join.hpp:76:36: note: passing argument to parameter 'rng' here
explicit join_view(Rng rng)
^
What makes this not compiled?
Is there any bug in range-v3 or cppcoro?
Only incompatible design decisions?
godbolt (Full)
In range-v3, a move-only view is OK. That got implemented late and there may still be bugs, but that's not what is happening here.
The first problem is that you are trying to adapt an lvalue of type cppcoro::generator here:
auto rng = gen();
for (auto n : rng | ranges::view::join) {
Since a generator is a view, the join view will want to copy it. It can't because it is not copyable.
You can fix this problem by moving the generator in:
auto rng = gen();
for (auto n : std::move(rng) | ranges::view::join) {
Then you run into the next problem, which is that the reference type of generator<generator<int>> is const generator<int>&, and you have the same problem again: the join wants to hold a copy of the inner generator while it iterates over it, but it cannot make a copy.
The workaround is a bit ugly: change the generator to return a non-const lvalue reference:
cppcoro::generator<cppcoro::generator<int>&> gen() {
for (int n = 1; n < 100; n += 10) {
auto tmp = gen_impl(n);
co_yield tmp;
}
}
and then std::move each inner range with a move view:
auto rng = gen();
for (auto n : std::move(rng) | ranges::view::move | ranges::view::join) {
std::cout << n << '\n';
}
The result compiles. Whether it runs or not depends on how gracefully cppcoro handles the case where someone steals away the guts of the value that it safely tucked away in the coroutine's promise type.
https://godbolt.org/z/mszidX
A note about the future std::view::join:
The join view that will ship with C++20 is a little different. If the outer range's reference type is a real reference (as in this case), it will not try to make a copy of the view to which it refers. That means in C++20, you won't need the ugly view::move hack.
However, the C++20 View concept currently requires copyability so this solution still won't work. We have a TODO item to relax this before C++20 ships, but there's no telling how the Committee will like that idea.

position.hh:46: error: expected unqualified-id before ‘namespace’

Here's my code:
34
35 /**
36 ** \file position.hh
37 ** Define the example::position class.
38 */
39
40 #ifndef BISON_POSITION_HH
41 #define BISON_POSITION_HH
42
43 #include <iostream>
44 #include <string>
45
46 namespace example
47 {
48 /// Abstract a position.
49 class position
50 {
51 public:
52
53 /// Construct a position.
54 position ()
55 : filename (0), line (1), column (0)
56 {
Thanks, speeder, that's great. Necrolis, thank you as well. Both of you guys are onto the same track on the compilation units. Here's the full error report:
In file included from location.hh:45,
from parser.h:64,
from scanner.h:25,
from scanner.ll:8:
position.hh:46: error: expected unqualified-id before ‘namespace’
location.hh looks like this:
35 /**
36 ** \file location.hh
37 ** Define the example::location class.
38 */
39
40 #ifndef BISON_LOCATION_HH
41 # define BISON_LOCATION_HH
42
43 # include <iostream>
44 # include <string>
45 # include "position.hh"
46
47 namespace example
48 {
49
50 /// Abstract a location.
51 class location
52 {
53 public:
I should also add that these files are being generated by bison. it's when i try to compile the c++ scanner class generated by flex++ that I get to this stage. I get the .cc code by issuing flex --c++ -o scanner.cc scanner.ll.
this happen when a ; or some other closing thing is lacking before the namespace. Are you sure that the lines before 34 have no code? If they have code (even if that code is other #include) the error is there.
EDIT: Or in case all 34 lines have no code, the error is on the file that includes this header, most likely there are a code without a ending ; or } or ) or some other ending character, and right after it (ignoring comments, of course) there are the #include position.hh
Or if there are two includes in a row, one before position.hh, the last lines of the header included before position.hh are with the error, usually a structure without a ; after the closing }
The error might be occuring in a file other than the file its reported in(due to the compilation units), namely at or near the end of that 'other' file(such as a missing '}' or ';' or '#endif' etc)