I'm pretty new at c++ so talk caveman to me. I'm trying to get the code to run in a loop and and when the loop is done calculate the total and everything ordered. I'm running into this error and I'm not sure why.
[Error] expected unqualified-id before '.' token
#include <iostream>
#include<string>
#include <vector>
using namespace std;
string again;
char pType, pSize, topping, temp;
const int SMALL = 1;
int type = 0, size = 0;
const int MEDIUM = 2;
const int LARGE = 3;
const int DEEPDISH = 1;
const int HANDTOSSED = 2;
const int PAN = 3;
double total = 0;
class Pizza
{
private:
int type;
int size;
bool cheese;
bool pepperoni;
public:
Pizza();
int getType();
int getSize();
bool getCheese();
bool getPepperoni();
void setType(int t);
void setSize(int s);
void setCheese(bool choice);
void setPepperoni(bool choice);
void outputDescription();
double computePrice();
};
class Order
{
private:
vector<Pizza> c;
public:
Order();
void customerOrder();
void customerTotal();
void customerinput();
};
Pizza::Pizza()
{
type = DEEPDISH;
size = SMALL;
cheese = pepperoni = false;
}
int Pizza::getType()
{
return type;
}
int Pizza::getSize()
{
return size;
}
bool Pizza::getCheese()
{
return cheese;
}
bool Pizza::getPepperoni()
{
return pepperoni;
}
void Pizza::setType(int t)
{
type = t;
}
void Pizza::setSize(int s)
{
size = s;
}
void Pizza::setCheese(bool choice)
{
cheese = choice;
}
void Pizza::setPepperoni(bool choice)
{
pepperoni= choice;
}
void Pizza::outputDescription()
{
switch (size)
{
case SMALL:
cout << "Small "; break;
case MEDIUM:
cout << "Medium "; break;
case LARGE:
cout << "Large "; break;
default:
cout << "Unknown" ;
}
switch (type)
{
case DEEPDISH:
cout << "deepdish "; break;
case HANDTOSSED:
cout << "hand tossed "; break;
case PAN:
cout << "pan "; break;
default:
cout << "Unknown";
}
cout << "pizza";
}
double Pizza::computePrice()
{
double cost = 0.0;
switch (size)
{
case SMALL:
cost += 10; break;
case MEDIUM:
cost += 14; break;
case LARGE:
cost += 17; break;
}
if (cheese)
cost += 2.0;
if (pepperoni)
cost += 2.0;
return cost;
}
Order custmizedTotal;
Pizza myPizza;
bool done=false;
void Order::customerinput(){
while ( again == "y"){
cout << "What sized pizza, please enter S, M OR L: ";
cin >> pSize;
cin.clear();
switch(pSize)
{
case 'S': case 's':
size = SMALL; break;
case 'M': case 'm':
size = MEDIUM; break;
case 'L': case 'l':
size = LARGE; break;
}
cout << "What type of pizza, enter D for Deepdish, H for Hand tossed, and P for Pan: ";
cin >> pType;
cin.clear();
switch(pType)
{
case 'D': case 'd':
type = DEEPDISH; break;
case 'H': case 'h':
type = HANDTOSSED; break;
case 'P': case 'p':
type = PAN; break;
}
myPizza.setSize(size);
myPizza.setType(type);
cout << "Would you like cheese (y/n)? ";
cin >> topping;
cin.clear();
if (topping == 'Y' || topping == 'y')
myPizza.setCheese(true);
cout << "Would you like pepperoni (y/n)? ";
cin >> topping;
cin.clear();
if (topping == 'Y' || topping == 'y')
myPizza.setPepperoni(true);
cout << endl
<< "Your order: ";
myPizza.outputDescription();
cout << endl;
cout << "Price: $" << myPizza.computePrice() << endl;
cout << "Again? (y/n)";
cin >> again;
}
}
void Order::customerTotal(){
cout << "Your Total order is: " << endl;
for(int i=0; i<c.size(); i++)
{
c[i].outputDescription();
cout << endl;
cout << c[i].computePrice();
cout << endl;
total=total+c[i].computePrice();
}
cout << "Totat Cost: $" << total;
cout << endl;
c.push_back(myPizza);
}
int main()
{
custmizedTotal.customerinput();
//Order.customerinput();
if(again != "y"){
custmizedTotal.customerTotal();
}
return 0;
}
Replace
int main(){
Order.customerinput(); //error is here
if(again != "y"){
custmizedTotal.customerTotal();
}
return 0;
}
By:
int main(){
custmizedTotal.customerinput(); // Change this line
if(again != "y"){
custmizedTotal.customerTotal();
}
return 0;
}
The second error that you have as caused because you forgot to define Order constructor.
Add this to your code (above main() method):
Order::Order(){
// Set the initial values for order
}
You also forgot to add customerOrder method (but this does not cause error since you are not using this method):
void Order::customerOrder() {
}
Related
Can you please help me find the error in printing the reverse of sequence using stacks implemented by vector?
I am getting a Segmenattion fault
#include <iostream>
#include<vector>
using namespace std;
class stack{
public :
int top;
vector<int> data;
bool isempty(){return top == -1;}
void push(int x){data[++top] = x;}
void pop(){--top;}
int topper(){return data[top];}
};
int main()
{
stack s;
int n;
s.top = -1;
cout << "enter the number of integers" << endl;
cin >> n;
for(int i =0; i < n; i ++){
s.push(i);
}
while(!s.isempty()){
cout << s.topper();
s.pop();
}
return 0;
}
This problem occurs, because a vector has size = 0 by default.
You can either resize the vector, before you add values into it like so:
#include <iostream>
#include<vector>
using namespace std;
class stack {
public:
int top;
vector<int> data;
bool isempty() { return top == -1; }
void push(int x) { data.resize(++top+1); data[top] = x; }
void pop() { --top; }
int topper() { return data[top]; }
};
int main()
{
stack s;
int n;
s.top = -1;
cout << "enter the number of integers" << endl;
cin >> n;
for (int i = 0; i < n; i++) {
s.push(i);
}
while (!s.isempty()) {
cout << s.topper();
s.pop();
}
return 0;
}
Or you can use the built-in functionality for vectors like that, which I think is the far better solution:
#include <iostream>
#include<vector>
using namespace std;
class stack {
public:
vector<int> data;
bool isempty() { return data.size() == 0; }
void push(int x) { data.push_back(x); }
void pop() { data.pop_back(); }
int topper() { return data.back(); }
};
int main()
{
stack s = stack();
int n;
cout << "enter the number of integers" << endl;
cin >> n;
for (int i = 0; i < n; i++) {
s.push(i);
}
while (!s.isempty()) {
cout << s.topper();
s.pop();
}
return 0;
}
I'm using method QUdpSocket::writeDatagram and sending a lot of data. When the socket is full, the method return -1. Is it possible to do somethind like select() with a QUdpSocket?
while(!counterList.empty())
{
CounterValuePtr value = counterList.dequeue();
if (mUdpSocket->bytesToWrite() <= 1<<13)
{
const QByteArray datagram = toDatagram(*value);
qint64 sentBytes = 0;
try
{
sentBytes = mUdpSocket->writeDatagram(datagram, QHostAddress(mSettings->getConnectionIp()), mSettings->getConnectionPort());
}
catch(...)
{
emit dataLost();
qWarning() << "Exception";
throw;
}
qDebug() << datagram;
if(sentBytes == -1)
{
QString errorMsg = getErrorMsg(WSAGetLastError());
qWarning() << "SocketError (" << mUdpSocket->error() << ") : " << mUdpSocket->errorString() << ". " << errorMsg;
emit dataLost();
}
else if(sentBytes < datagram.size())
{
qWarning() << "Not all data sent";
emit dataLost();
}
else
{
qDebug() << "Counter "<< value->getName() << " sent";
datasent = true;
emit dataSent();
}
}
else
{
qWarning() << "Socket write buffer full. Dropping data.";
emit dataLost();
}
}
Some times, writeDatagram returns -1. When I print the last error with WSAGetLastError it prints the buffer is full.
I am unable to understand what is the role of "usage:\n\tll '(a+a)'" in the code. What is its function?? I am using g++ compiler to compile the code. If more than 2 arguments are passed in command prompt then problem occurs.
#include <iostream>
#include <map>
#include <stack>
enum Symbols {
TS_L_PARENS,
TS_R_PARENS,
TS_A,
TS_PLUS,
TS_EOS,
TS_INVALID,
NTS_S,
NTS_F
};
enum Symbols lexer(char c)
{
switch(c)
{
case '(': return TS_L_PARENS;
case ')': return TS_R_PARENS;
case 'a': return TS_A;
case '+': return TS_PLUS;
case '\0': return TS_EOS;
default: return TS_INVALID;
}
}
int main(int argc, char **argv)
{
using namespace std;
if (argc < 2)
{
cout << **"usage:\n\tll '(a+a)'"** << endl;
return 0;
}
map< enum Symbols, map<enum Symbols, int> > table;
stack<enum Symbols> ss; // symbol stack
char *p; // input buffer
ss.push(TS_EOS); // terminal, $
ss.push(NTS_S); // non-terminal, S
p = &argv[1][0];
table[NTS_S][TS_L_PARENS] = 2;
table[NTS_S][TS_A] = 1;
table[NTS_F][TS_A] = 3;
while(ss.size() > 0)
{
if(lexer(*p) == ss.top())
{
cout << "Matched symbols: " << lexer(*p) << endl;
p++;
ss.pop();
}
else
{
cout << "Rule " << table[ss.top()][lexer(*p)] << endl;
switch(table[ss.top()][lexer(*p)])
{
case 1: // 1. S → F
ss.pop();
ss.push(NTS_F); // F
break;
case 2: // 2. S → ( S + F )
ss.pop();
ss.push(TS_R_PARENS); // )
ss.push(NTS_F); // F
ss.push(TS_PLUS); // +
ss.push(NTS_S); // S
ss.push(TS_L_PARENS); // (
break;
case 3: // 3. F → a
ss.pop();
ss.push(TS_A); // a
break;
default:
cout << "parsing table defaulted" << endl;
return 0;
break;
}
}
}
cout << "finished parsing" << endl;
return 0;
}
cout is the function, "usage:\n\tll '(a+a)'" is a string literal passed to the function. This bit in your question prints:
usage:
ll '(a+a)'
I need to perform some regexp operations on binary data. I wrote a function to convert QByteArray data in a hexa string representation. Each byte is prepended by 'x' for parsing purpose.
How could this code be optimized?
QByteArray data;
QByteArray newData;
for (int i = 0; i < data.size(); i++) {
QString hex;
hex.setNum(data[i], 16);
if (data[i] < 10) {
hex.prepend("x0");
} else {
hex.prepend("x");
}
newData.append(hex.toLatin1());
}
The code you posted has two bugs in it that I corrected.
1) Assuming you always want two hex digits you want to check if the value is less than 16, not 10.
2) QString::setNum has no overload for char, so the value is promoted to a larger type. For a value like 128, which is negative in a signed char, you would get x0ffffffffffffff80 due to sign extension.
The function foo1 is your original code with the bugs fixed, and foo2 is a more optimal version that avoids creating a temporary QString since the conversion to unicode and back isn't free, and prepending values to a string requires additional copying.
I used QElapsedTimer because on Windows where I am testing it uses the high resolution PerformanceCounter clock. If you are on another platform it might be less accurate. You can see the different types of clocks it may use in the documentation.
Set display_converted_string to true if you want the converted string printed to verify they are identical.
#include <QString>
#include <QByteArray>
#include <QElapsedTimer>
#include <iostream>
QByteArray foo1(QByteArray data)
{
QByteArray newData;
for (int i = 0; i < data.size(); i++) {
unsigned char c = data[i];
QString hex;
hex.setNum(c, 16);
if (c < 16) {
hex.prepend("x0");
} else {
hex.prepend("x");
}
newData.append(hex.toLatin1());
}
return newData;
}
QByteArray foo2(QByteArray data)
{
static const char digits[] = {'0','1','2','3','4','5','6','7',
'8','9','a','b','c','d','e','f'};
QByteArray newData;
newData.reserve(data.size() * 3);
for (int i = 0; i < data.size(); i++)
{
unsigned char c = data[i];
newData.append('x');
newData.append(digits[(c >> 4) & 0x0f]);
newData.append(digits[c & 0x0f]);
}
return newData;
}
int main()
{
const int iterations = 10000;
const bool display_converted_string = false;
QElapsedTimer t;
std::cout << "Using clock type " << t.clockType() << ".\n";
QByteArray data(256, 0);
QByteArray newData;
qint64 elapsed1 = 0, elapsed2 = 0;
//Set the values in data to 0-255 to make sure all values are converted properly.
for(int i = 0; i < data.size(); ++i)
{
data[i] = i;
}
t.start();
for(int i = 0; i < iterations; ++i)
{
newData = foo1(data);
}
elapsed1 = t.nsecsElapsed();
std::cout << "foo1 elapsed time = " << elapsed1 << "\n";
if(display_converted_string)
{
std::cout << "newData = " << newData.data() << "\n";
}
t.restart();
for(int i = 0; i < iterations; ++i)
{
newData = foo2(data);
}
elapsed2 = t.nsecsElapsed();
std::cout << "foo2 elapsed time = " << elapsed2 << "\n";
if(display_converted_string)
{
std::cout << "newData = " << newData.data() << "\n";
}
return 0;
}
I'm trying to write a set (in mathematical sens) in C++ but I have problem. When i launch my program i have "vector subcript out of range" error. I'm using Visual Studio and its not show any error. Sorry for my bad language.
#include "Set.h"
template <class T>
void Set<T>::print() {
cout << endl << "{";
for(int i = 0; i < set.size() - 1; i++) {
cout << set[i] << ", ";
}
//cout << set[set.size()] << "}" << endl;
cout << endl;
}
template <class T>
bool Set<T>::contains(T value) {
for(unsigned int i = 0; i < set.size(); i++) {
if(set[i] == value) return true;
}
return false;
}
template <class T>
void Set<T>::operator +(const T &obj) {
if(!contains(obj)) set.push_back(obj);
}
template <class T>
void Set<T>::operator -(const T &obj) {
if(contains(obj)) {
unsigned int i = 0;
// NIE DZIAŁA
while(i < set.size()) {
if(set[i] == obj) break;
i++;
}
while(i < (set.size() - 1)) {
set[i] = set[i + 1];
i++;
}
//set[i] = set[set.size()];
}
}
template <class T>
Set<T> Set<T>::operator +(const Set<T> &obj) {
Set<T> result;
for(unsigned int i = 0; i < set.size(); i++) {
result + set[i];
}
for(unsigned int i = 0; i < obj.set.size(); i++) {
if(!result.contains(obj.set[i])) {
result + obj.set[i];
}
}
return result;
}
template <class T>
Set<T> Set<T>::operator -(const Set<T> &obj) {
Set<T> result;
for(unsigned int i = 0; i < set.size() - 1; i++) {
if(!contains(obj.set[i])) {
result + set[i];
}
}
return result;
}
template <class T>
Set<T> Set<T>::operator *(const Set<T> &obj) {
Set<T> result;
for(unsigned int i = 0; i < set.size() - 1; i++) {
if(contains(obj.set[i])) {
result + set[i];
}
}
return result;
}
Can someone help me?
I fix my problem. It did in print method. It's correct version of this method:
template <class T>
void Set<T>::print() {
if(!set.empty()) {
cout << endl << "{";
for(unsigned int i = 0; i < set.size() - 1; i++) {
cout << set[i] << ", ";
}
cout << set[set.size() - 1] << "}" << endl;
cout << endl; }
}
Thanks for help!