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)
Related
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
I am working through Accelerated C++ by Andrew Koenig and Barbara Moo (2000) and am stuck on code from Chapter 4. I think this code reads an external data file containing exam, final and homework grades for multiple students and returns a course grade for each student.
I have downloaded solutions for the exercises from Andrew Koenig from GitHub:
https://github.com/bitsai/book-exercises/tree/master/Accelerated%20C%2B%2B
I can run the first example in Chapter 4 (main1.cc) which returns a course grade for a single student when the midterm, final and homework grades are entered from the keyboard. Here is an example:
data: harriet 85 95 88 87 93 45 76 99
student name = harriet
midterm grade = 85
final grade = 95
median of six homework grades (88, 87, 93, 45, 76, 99) = 87.5
final grade = 90
0.2 * 85 + 0.4 * 95 + 0.4 * 87.5 = 90
But I cannot run the second example (main2.cc) in which data are read for multiple students from an external file. No error messages appear when I create or run the main2.exe file. The cursor in the Windows 10 command window simply moves to the next line when I try to run main2.exe and nothing is displayed, not even the directory. Here are the lines returned when I make the executables:
c:\Users\mark_\myCppprograms\mychapter04>make
g++ main1.cc -o main1
g++ main2.cc -o main2
g++ -c -o main3.o main3.cc
g++ -c -o grade.o grade.cc
g++ -c -o median.o median.cc
g++ -c -o Student_info.o Student_info.cc
g++ main3.o grade.o median.o Student_info.o -o main3
The code in main2.cc is long. The third example in Chapter 4 (main3.cc) breaks this code into multiple C++ and header files but I cannot get the main3.exe file to return anything either.
Here is the code for main2.cc. I changed #include "../minmax.h" to #include "minmax.h" and put a file "minmax.h" in the same folder as main2.cc. The file "minmax.h" did not come from the aforementioned GitHub site and I paste its contents below.
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#ifdef _MSC_VER
//#include "../minmax.h"
#include "minmax.h"
#else
using std::max;
#endif
using std::cin;
using std::cout;
using std::domain_error;
using std::endl;
using std::istream;
using std::ostream;
using std::setprecision;
using std::setw;
using std::sort;
using std::streamsize;
using std::string;
using std::vector;
struct Student_info {
string name;
double midterm, final;
vector<double> homework;
}; // note the semicolon--it's required
// compute the median of a `vector<double>'
// note that calling this function copies the entire argument `vector'
double median(vector<double> vec) {
#ifdef _MSC_VER
typedef std::vector<double>::size_type vec_sz;
#else
typedef vector<double>::size_type vec_sz;
#endif
vec_sz size = vec.size();
if (size == 0)
throw domain_error("median of an empty vector");
sort(vec.begin(), vec.end());
vec_sz mid = size/2;
return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid];
}
// compute a student's overall grade from midterm and final exam grades and homework grade
double grade(double midterm, double final, double homework) {
return 0.2 * midterm + 0.4 * final + 0.4 * homework;
}
// compute a student's overall grade from midterm and final exam grades
// and vector of homework grades.
// this function does not copy its argument, because `median' does so for us.
double grade(double midterm, double final, const vector<double>& hw) {
if (hw.size() == 0)
throw domain_error("student has done no homework");
return grade(midterm, final, median(hw));
}
double grade(const Student_info& s) {
return grade(s.midterm, s.final, s.homework);
}
// read homework grades from an input stream into a `vector<double>'
istream& read_hw(istream& in, vector<double>& hw) {
if (in) {
// get rid of previous contents
hw.clear();
// read homework grades
double x;
while (in >> x)
hw.push_back(x);
// clear the stream so that input will work for the next student
in.clear();
}
return in;
}
istream& read(istream& is, Student_info& s) {
// read and store the student's name and midterm and final exam grades
is >> s.name >> s.midterm >> s.final;
read_hw(is, s.homework); // read and store all the student's homework grades
return is;
}
bool compare(const Student_info& x, const Student_info& y) {
return x.name < y.name;
}
int main() {
vector<Student_info> students;
Student_info record;
string::size_type maxlen = 0;
// read and store all the records, and find the length of the longest name
while (read(cin, record)) {
maxlen = max(maxlen, record.name.size());
students.push_back(record);
}
// alphabetize the records
sort(students.begin(), students.end(), compare);
#ifdef _MSC_VER
for (std::vector<Student_info>::size_type i = 0;
#else
for (vector<Student_info>::size_type i = 0;
#endif
i != students.size(); ++i) {
// write the name, padded on the right to `maxlen' `+' `1' characters
cout << students[i].name
<< string(maxlen + 1 - students[i].name.size(), ' ');
// compute and write the grade
try {
double final_grade = grade(students[i]);
streamsize prec = cout.precision();
cout << setprecision(3) << final_grade
<< setprecision(prec);
} catch (domain_error e) {
cout << e.what();
}
cout << endl;
}
return 0;
}
The code for main1.cc, main2.cc and main3.cc all compiles using a single makefile and make statement. Here are the contents of makefile. I do not believe main2.cc uses any of the header files mentioned in this makefile but I did not make any changes to the makefile. I have not pasted the contents of any of these header files here. If they are relevant to main2.cc I can provide them on request or they are all available at the GitHub site above.
CXX = g++
CC = g++
all: main1 main2 main3
Student_info.o: Student_info.cc Student_info.h
grade.o: grade.cc grade.h median.h Student_info.h
main3.o: main3.cc grade.h median.h Student_info.h
median.o: median.cc median.h
main3: main3.o grade.o median.o Student_info.o
test: all
./main1 <../data/single_grade
./main2 <../data/single_grade
./main2 <../data/grades
./main3 <../data/grades
clobber:
rm -f *.o *.exe core main1 main2 main3
I have created a subfolder called data and put two data files in it: grades and single_grade. Here are the contents of grades:
Moo 100 100 100 100 100 100 100 100
Moore 75 85 77 59 0 85 75 89
Norman 57 78 73 66 78 70 88 89
Olson 89 86 70 90 55 73 80 84
Peerson 47 70 82 73 50 87 73 71
Russel 72 87 88 54 55 82 69 87
Thomas 90 96 99 99 100 81 97 97
Vaughn 81 97 99 67 40 90 70 96
Westerly 43 98 96 79 100 82 97 96
Baker 67 72 73 40 0 78 55 70
Davis 77 70 82 65 70 77 83 81
Edwards 77 72 73 80 90 93 75 90
Franklin 47 70 82 73 50 87 73 71
Jones 77 82 83 50 10 88 65 80
Harris 97 90 92 95 100 87 93 91
Smith 87 92 93 60 0 98 75 90
Carpenter 47 90 92 73 100 87 93 91
Fail1 45 55 65 80 90 70 65 60
Fail2 55 55 65 50 55 60 65 60
Here are the contents of single_grade:
harriet 85 95 88 87 93 45 76 99
The only place I find these two data files mentioned in any of the above code is in the makefile which confuses me, but I guess the makefile associates main2.cc and the two data files.
Here are the contents of "minmax.h":
/**
* This file has no copyright assigned and is placed in the Public Domain.
* This file is part of the mingw-w64 runtime package.
* No warranty is given; refer to the file DISCLAIMER.PD within this package.
*/
#ifndef _INC_MINMAX
#define _INC_MINMAX
#ifndef __cplusplus
#ifndef NOMINMAX
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
#endif
#endif
#endif
Here is what is returned by c++ -v on my Windows 10 laptop:
c:\Users\mark_\myCppprograms>c++ -v
Using built-in specs.
COLLECT_GCC=c++
COLLECT_LTO_WRAPPER=c:/rtools/MINGW_64/bin/../libexec/gcc/x86_64-w64-mingw32/4.9.3/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-4.9.3/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/home/Jeroen/mingw-gcc-4.9.3/x86_64-493-posix-seh-rt_v3-s/mingw64 --with-gxx-include-dir=/mingw64/x86_64-w64-mingw32/include/c++ --enable-static --disable-shared --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-isl-version-check --disable-cloog-version-check --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-w64-mingw32-static --with-mpc=/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-w64-mingw32-static --with-isl=/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-w64-mingw32-static --with-cloog=/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-w64-mingw32-static --enable-cloog-backend=isl --with-pkgversion='x86_64-posix-seh, Built by MinGW-W64 project' --with-bugurl=http://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -I/home/Jeroen/mingw-gcc-4.9.3/x86_64-493-posix-seh-rt_v3-s/mingw64/opt/include -I/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-zlib-static/include -I/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -I/home/Jeroen/mingw-gcc-4.9.3/x86_64-493-posix-seh-rt_v3-s/mingw64/opt/include -I/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-zlib-static/include -I/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS= LDFLAGS='-pipe -L/home/Jeroen/mingw-gcc-4.9.3/x86_64-493-posix-seh-rt_v3-s/mingw64/opt/lib -L/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-zlib-static/lib -L/home/Jeroen/mingw-gcc-4.9.3/prerequisites/x86_64-w64-mingw32-static/lib '
Thread model: posix
gcc version 4.9.3 (x86_64-posix-seh, Built by MinGW-W64 project)
c:\Users\mark_\myCppprograms>
I am aware that other people have uploaded solutions to Accelerated C++ exercises. I have looked at several. But I have not been able to get any of them to run with main2.cc or main3.cc and return the expected results.
I'm not sure how you actually run it, but in my case it works perfectly fine. If you take a closer look at main in main2.cc, you'll notice that there's a blocking read call, which causes the program to wait for user input. Take a look at test target from makefile:
test: all
./main1 <../data/single_grade
./main2 <../data/single_grade
./main2 <../data/grades
./main3 <../data/grades
< means that we'll be redirecting what's in single_grade to the process being run. It's implicitly assumed that in folder above to the executable binary will be a data directory with single_grade data file. So, when you run $ make test, you'll notice that everything works as expected (assuming the directory structure is unchanged). If you simply run $ ./main2, nothing is gonna happen as the program awaits for input. Or, assuming your data file is next to main2 binary, you could run $ main2 < single_grade. Or, alternatively, you could omit the files at all:
$ ./main2
$ Moo 100 100 100 100 100 100 100 100
$ Moore 75 85 77 59 0 85 75 89
$ EOF (in bash shell it's CTRL + D, on windows cmd: CTRL + Z)
Which yields:
Moo 100
Moore 79.4
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
I was asked to calculate the average of marks for 10 students.
First, I was able to read and retrieve the data from data.txt file which looks like this:
No. Name Test1 Test2 Test3
1 Ahmad 58 97 83
2 Dollah 78 76 70
3 Ramesh 85 75 84
4 Maimunah 87 45 74
5 Robert 74 68 97
6 Kumar 77 73 45
7 Intan 56 23 27
8 Ping 74 58 18
9 Idayu 47 98 95
10 Roslan 79 98 78
Then I have to calculate the average for each student and determine the grades.
Here are what I've done so far.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
ifstream inFile1;
string temp;
int line=0;
inFile1.open("data.txt");
if(inFile1.fail())
{
cout << "File cannot be opened" << endl;
exit(1);
}
while(getline(inFile1, temp))
{
line++;
}
inFile1.close();
return 0;
}
This program should at least consists two prototype function: average() and grade().
This is where I got stuck.
You can check the answers here: find average salaries from file in c++.
Basically when you iterate through the file lines you should split the temp string into tokens you are interested in. How? An option would be to use getline with the delimeter ' ' or look into the std::noskipws stream manipulator or simply use operator>> to read from the file - depends on the details of your requirements.
If I correctly understand your case, I'd go with the operator>> to get the name of the student and then read using getline(inFile, gradesText) to read until end of line to get all grades for the current student.
Then I'd use a separate function to split the strings into a vector of grades. How to do the splitting you can check in Split a string in C++?. This way you could prepare a function like vector<int> split(const string& line, char delim = ' '). Within the implementation you should probably use std::stoi for the string-to-int conversion.
Afterwards, when you already have a proper collection you can calculate the mean from it with:
const double sum = std::accumulate(grades.begin(), grades.end(), 0.0);
const double gradesMean = sum / grades.size();
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.