C++ Expected unqualified-id when trying to write a function - c++

I wrote this code and was just trying to continue writing a function called register. I choose to set it to void because i am not certain if i am going to return anything at the moment. The Expected unqualified-id seems to show up when you put a semicolon in the wrong place, but i cant see that i make that error here..
I tried to comment out the menu.h include and all the three functions i get from the header file. No difference.
#include <iostream>
#include <string>
#include "menu.h"
//From the menu.h header file
int mainMenu();
int customerMenu();
int librarianMenu();
void register() { //Here the error appears.
}
int main () {
int runFlag = 1;
while(runFlag == 1) {
int mainMenuChoice = mainMenu();
if (mainMenuChoice == 1) {
customerMenu();
}
else if(mainMenuChoice == 2) {
librarianMenu();
}
}
}

Related

Trying to return a Bool Value from a class in C++

I'm very new to the world of coding and C++. I'm tasked with taking numbers from a .txt doc with different lines of values. I need to do error checks on each line to make sure the file is in the correct format. I was hoping to make a class that I can reuse in order to do these checks. i,e:
get data from first line
check to see if data matches criteria (this case it has to be 1 or 0 and I will have to apply different operations later on based on this value)
Trigger a true / false if statment in the main file based on results.
Main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <functional>
#include "ErrorSwitch.h"
using namespace std;
//Set pointer of 'file' to beginning line of 'num'
fstream& GotoLine(fstream& file, unsigned int num) {
file.seekg(ios::beg);
//Loop through file
for (int i = 0; i < num - 1; ++i) {
file.ignore(numeric_limits<streamsize>::max(), '\n');
}
return file;
}
int main() {
fstream file("file1.txt");
//Check Line 1
GotoLine(file, 1);
//Get Input from First Line & Convert to int
string line1;
file >> line1;
int inpu = stoi(line1);
//Error Check
ErrorSwitch checkOne;
checkOne.input = inpu;
checkOne.errorCheck();
if (checkOne == true) { //I can't seem to get this to work as a boolean check
//Do code here
}
else if (checkOne == false) {
//Do code here
}
}
ErrorSwitch.h
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <functional>
using namespace std;
class ErrorSwitch
{
public:
int input;
bool errorCheck() {
switch (input) {
case 0:
return true;
break;
case 1:
return true;
break;
default:
return false;
}
}
};
Can someone please have a look at this and help me understand how I can get a return value from this class function.
Thanks!
The bool value is returned from the function errorCheck, not the class itself.
You can store the return value to a variable like this:
bool checkRes = checkOne.errorCheck();
if (checkRes == true) {
//Do code here
}
else if (checkRes == false) {
//Do code here
}
or simply do:
if (checkOne.errorCheck()) {
//Do code here
}
else { // if bool is not true, it is false
//Do code here
}
Alternatively, you can add an operator to convert the class to bool like this
operator bool() {
return errorCheck();
}
to the class ErrorSwitch to have this code work:
if (checkOne == true) { //I can't seem to get this to work as a boolean check
//Do code here
}
else if (checkOne == false) {
//Do code here
}
Change this:
checkOne.errorCheck();
if (checkOne == true) {
//Do code here
}
else if (checkOne == false) {
//Do code here
}
To this:
if (checkOne.errorCheck()) {
//Do code here
}
else {
//Do code here
}

error: no matching function for call to "Queue::Queue()"

So I am trying to call a function in my main.cpp file but I get "error: no matching function for call to 'Queue::Queue()."
Queue.h
#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
class Queue
{
public:
Queue(int);
~Queue();
//circular queue methods
void enqueue(std::string);
std::string dequeue(); //should send through network, call transmit msg
void printQueue();
bool queueIsFull(); //send when full
bool queueIsEmpty(); //send when empty
protected:
private:
int queueSize;
int queueHead;
int queueTail;
int queueCount;
std::string *arrayQueue;
};
#endif // QUEUE_H
Queue.cpp
#include "Queue.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
Queue::Queue(int qs)
{
queueSize = qs;
arrayQueue = new string[queueSize];
queueHead = 0;
queueTail = 0;
}
Queue::~Queue()
{
delete[] arrayQueue;
}
void Queue::enqueue(string word)
{
for (int i=0;i<10;i++)
{
arrayQueue[i] = word;
}
}
void Queue::printQueue()
{
for(int j=0;j<10;j++)
{
cout<<arrayQueue[j]<<endl;
}
}
main.cpp
#include <iostream>
#include "Queue.h"
using namespace std;
int main()
{
int userChoice;
Queue q;
while(2==2)
{
cout<<"======Main Menu======"<<endl;
cout<<"1. Enqueue word"<<endl;
cout<<"2. Dequeue word"<<endl;
cout<<"3. Print queue"<<endl;
cout<<"4. Enqueue sentence"<<endl;
cout<<"5. Quit"<<endl;
cin>>userChoice;
if (userChoice == 1)
{
string enqueueWord;
cout<<"word: ";
cin>>enqueueWord;
enqueue(enqueueWord);
}
if (userChoice == 2)
{
}
if (userChoice == 3)
{
}
if (userChoice == 4)
{
}
if (userChoice == 5)
{
}
}
return 0;
}
So to call the function from the header file I did "Queue q;" at the beginning of the int main() and then when I needed to call the function I did "q.enqueue(enqueueWord)." I also tried just doing "Queue::enqueue(enqueueWord), but that also didn't work and I get a different error. I feel like this is an easy fix but I just can't figure it out. Thanks for the help and feel free to ask me to clarify anything.
Queue q;
attempts to call the default constructor Queue::Queue. However, this constructor has been removed automatically since you explicitly declare a constructor, namely Queue::Queue(int), on your own.
Pass an appropriate argument to q when initialized, like
Queue q1(42); // pre-C++11 syntax
Queue q{42}; // available since C++11
(Note: 42 is only an exemplary value here.)
You could also use default arguments to keep the definition as-is and initialize the object with a default value.
Notes:
Why while(2==2)? while (true) is the common way.

How to draw a rectangle using a char parameter in C++?

I want to draw and fill a rectangle using C++. The function parameter passed in must be a char, not an int. In my header file the drawing function is this:
void draw(char);
My rectangle.cpp file is this:
void rectangle::draw(char )
{
for(height=0;height<=height;height++)
{
for(width=1;width<=width;width++)
{
cout<<'*';
}
}
}
My main.cpp file is this:
rectangle d1;
d1.draw(char);
When I run the program it gives me the error:
Expected primary expression before 'char'.
I'm using Code::Blocks 13.12. Any ideas to solve this problem?
Your draw is missing a variable. Change draw(char); to draw(char c);
And both for loops need a maximum range, not the same value as the incrementing variable. height<=height; and width<=width;
#include <iostream>
using namespace std;
void draw(char c )
{
int rheight=10;
int rwidth=20;
for(int height=0;height<=rheight;height++)
{
for(int width=1;width<=rwidth;width++)
{
cout<<c;
}
cout<<"\n";
}
}
int main()
{
draw('*');
cout<<"\n";
return 0;
}

Why shows --"cannot pass objects of non-trivially-copyable type"?

You don't have to go through the complete code from the beginning. The problem is in the execl(..) statement inside main. Code is --
#include <cstdio>
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <sys/wait.h>
#include <vector>
#define li long int
using namespace std;
char TypedCommandInTerminal[1001];
vector <string> ValidCommands,TypedCommand;
void ShowTerminal()
{
cout<<"User:$ ";
gets(TypedCommandInTerminal);
}
void PushCommands()
{
ValidCommands.push_back("mkdir");
}
void GetCommandIntoVector()
{
TypedCommand.clear();
char *p = strtok(TypedCommandInTerminal," ");
while(p)
{
TypedCommand.push_back(p);
p = strtok(NULL," ");
}
}
bool MatchCommand(string Command)
{
li i;
for(i=0;i<ValidCommands.size();i++)
{
if(ValidCommands[i].compare(Command)==0)
{
return true;
}
}
return false;
}
int main()
{
int status;
string StoredCommand;
PushCommands();
while(true)
{
ShowTerminal();
if(fork()!=0)
{
waitpid(-1,&status,0);
}
else
{
GetCommandIntoVector();
if(MatchCommand(TypedCommand[0]))
{
StoredCommand = "mkdir";
if(StoredCommand.compare(TypedCommand[0])==0)
{
execl("/bin/mkdir","mkdir",TypedCommand[1],NULL);/*ERROR*/
}
}
else
{
cout<<"Command Not Available\n";
return -1;
}
}
}
return 0;
}
I am trying to design a simple terminal using c++ in linux. What I am trying to do here is -- taking this command in console as input - "mkdir ab" . Then I managed to tokenize this string and kept "mkdir" in TypedCommand[0] and "ab" in TypedCommand[1]. Problem is when I write "TypedCommand[1]" inside execl compiler gives an error--"cannot pass objects of non-trivially-copyable type....."
I removed TypedCommand[1] and manually wrote "ab" in place of it. The code ran and created a folder named "ab" in the execution directory. So looks like execl is working fine.
I need to pass the second string saved in TypedCommand[1] inside execl in some way...what is wrong here ?
You're passing a std::string object as a optional argument to a function (execl accepts a variable number of arguments). std::string has non-trivial constructors, destructor, etc. and cannot be used this way. In this case you want to pass a pointer to a string anyway so change
execl("/bin/mkdir","mkdir",TypedCommand[1],NULL);
to
execl("/bin/mkdir","mkdir",TypedCommand[1].c_str(),NULL);

need help on #include doesn't seem to be working

So I have a class called HPStack and I have to include it in my main class etc. However I get a "In File included from" error, what could be causing this?
Also my string objects also have errors I have have no idea why, the error is: "Unable to identifier string".
I'm new the C++ so any help would be appreciated, thanks in advance.
The error I am getting (I think) are these:
error: expected unqualified-id before "namespace"
error: expected `,' or `;' before "namespace"
error: expected namespace-name before ';' token
error: `<type error>' is not a namespace
Im not sure what I am missing but that isn't telling me much.
Here is my code: The class.h file.
#ifndef HPSTACK_H
#define HPSTACK_H
class HPStack {
public:
HPStack();
void push(double);
double pop();
double peek();
private:
double register_[4];
}
#endif
The class.cpp file.
#include "HPStack.h"
#include <cstdlib>
HPStack::HPStack() : register_{}{
}
double HPStack::push(double x) {
for (int i = 2; i >= 0; i--) {
if (isdigit(register_[i])) {
register_[i] = register_[i + 1];
}
register_[0] = x;
}
}
double HPStack::pop() {
return register_[0];
for (int i = 0; i < 3; i++) {
register_[i] = register_[i + 1];
}
}
double HPStack::peek() {
return register_[0];
}
And my main file:
#include "HPStack.h"
#include <cstdlib>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main() {
HPStack stack;
string line;
while (getline(cin, line)) {
stringstream expression(line);
string token;
while (expression >> token) {
if (isdigit(token[0])) {
stack.push(atof(token.data()));
} else if (token == "+") {
double x = stack.pop();
double y = stack.pop();
double z = (y + x);
stack.push(z);
}
}
cout << stack.peek();
}
The error is, I'm guessing, because of this line:
double register_[4] = {};
You can not initialize class members when declaring them.
If your compiler is new enough to support C++11 features, you can use an initializer list with the constructor:
HPStack::HPStack()
: register_{}
{
}
Otherwise you have to initialize the array manually in the constructor.
And as I noted in a comment, using register_ - 2 makes no sense as it returns a pointer so the index variable i will be way beyond the end of the array.
And using register_ - 1 as the condition in the pop loop makes even less sense, as it will always be non-zero and therefore always true and the loop will loop forever.
You're missing the ; at the end of the class definition:
class HPStack {
...
}; // <== This semicolon is required