include <iostream>
#include <string.h>
using namespace std;
#include "Stack.h"
int main()
{
Stack<float>b;
Stack <float> a(b);
char num[100];
while (true)
{
cout << "Enter: " << endl;
cin >> num;
if(strcmp(num,"Q") == 0 ||strcmp(num,"q")==0)
break;
if(strcmp(num,"*")==0)
{
float num1 = b.pop();//Error: Cannot initialize a variable of type 'float' with an rvalue of type 'void'
float num2 = b.pop();// Error: Cannot initialize a variable of type 'float' with an rvalue of type 'void'
if(num1 && num2){
a.push(num1*num2);
}
This I a part of my program. the problem is that I need to store the value I pop from my list and then multiply them and store them back. Over here I am trying to store the values in float number so that I can do an operation but it gives me an error as stated above any help will be appreciated.
If your "Stack.h" is similar to <stack>:
Referring to Stack.pop():
Return value
none
So you should use Stack.top():
Return value
A reference to the top element in the stack.
Sample Code
float num = b.top(); // get top value of stack
b.pop(); // pop it, not used anymore
// do something with num here
Related
I commented on the line in question. It makes little sense to me. Referring to "expression must have pointer to object type". I was trying to access my "items" array using random numbers for the index, but it errors the array.
#include "stdafx.h"
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <time.h>
using namespace std;
struct product {
double price;
} pasta, cookies, candy, chips, pop, bread, eggs;
//struct sales {
//double salepercent;
//} pastasale, cookiesale, candysale, chipsale, popsale, breadsale, eggsale;
double prices[7] = { pasta.price = 3.99, cookies.price = 4.99, candy.price =
2.99, chips.price = 1.99, bread.price = 3.85, eggs.price = 5.99 };
//double sales[7] = {pastasale.salepercent = 0.30, cookiesale.salepercent =
0.30, chipsale.salepercent = 0.30, popsale.salepercent = 0.30,
breadsale.salepercent = 0.30, eggsale.salepercent = 0.30}
double costumer_cart[7] = {};
double subtotal = 0;
double total = 0;
double fillcart(double cart[], double items)
{
srand(time(NULL));
for (int i = 0; i < 8; i++)
{
cart[i] = items[rand() % 8]; //EXPRESSION MUST HAVE POINTER-TO-
OBJECT TYPE on "items[rand() % 8]"
}
cout << "Cart: " << cart;
}
int main()
{
double fillcart(double costumer_cart, double prices);
system("PAUSE");
return 0;
}
In your fillcart() function
double fillcart(double cart[], double items)
items is of type double, but you use it in the loop as
cart[i] = items[rand() % 8]; //EXPRESSION MUST HAVE POINTER-TO-
An expression using array syntax [] does not work on a double. In it's basic form, such an expression requires items to be either a pointer or an array, and rand() % 8 to be of integral type. rand() %8 is of integral type (okay), but items is of type double, which is neither a pointer nor an array.
The usage of cart[i] is valid, since cart is a pointer (when array syntax [] is used to specify the type of a function argument, the argument is actually passed to the function as a pointer).
Note: array syntax can also be used in other circumstances in C++, for example with an instance of a class that supplies an operator[](), but that's also not the case here.
How can I access the value of a pointer to a vector in a struct? I've got the following code:
#include <iostream>
#include <vector>
using namespace std;
struct item {
int value;
vector<bool> pb;
vector<bool> *path = &pb;
};
int main(int argc, char* argv[]) {
vector<item> dp(10);
for (int n = 0; n < 10; n++)
dp[n].pb = vector<bool>(10);
if (dp[1].path[2] == true)
cout << "true";
else cout << "false";
}
Which results in the following compilation error:
Error C2678 binary '==': no operator found which takes a left-hand operand of type 'std::vector<bool,std::allocator<_Ty>>' (or there is no acceptable conversion)
How can I access the value stored at dp[1].path[2]?
path is a pointer to a vector. You have to do the following to access the value in the vector that it points to
if ((*(dp[1].path))[2] == true)
or
if (dp[1].path->operator[](2) == true)
In addition, you can use at function which is also
checks whether n is within the bounds of valid elements in the vector
code example
if (dp[1].path->at(2) == true)
I'm just starting to code, and am learning about arrays right now. I am trying to write a program that takes in a list of arrays, and tells me if the first or last number is a 2. To do this, I'm using a function.
My code looks like:
#include <iostream>
using namespace std;
const int size = 6;
bool firstlast(int array[size]);
int main()
{
int array[size];
for (int index = 0; index < size; index++)
{
cout << "Enter value for array[" << index << "]\n";
cin >> array[index];
}
bool check = firstlast(array[size]);
if (check)
cout << "The array either starts or ends in 2!\n";
else
cout << "The array does not start or end with 2.\n";
return 0;
}
bool firstlast(int array[size])
{
if (array[0] == 2)
return true;
if (array[size - 1] == 2)
return true;
return false;
}
What am I doing wrong?
The compiler gives me the error:
candidate function not viable: no known conversion from 'int' to 'int *' for 1st argument; take the address of the argument with and
The compiler is recognising your function fine.
The problem is in the manner your code calls the function
bool check = firstlast(array[size]);
which attempts to pass array[size] (a non-existent element of array) to a function expecting a pointer.
The call, presumably, should be
bool check = firstlast(array);
since arrays are implicitly converted to pointers when passed to functions.
This code
bool check = firstlast(array[size], size);
tries to pass the sizeth element of array not the array itself. In C++ arrays are passed by pointer, even if you write the function parameter with array syntax.
To avoid confusing yourself, change firstlast to
bool firstlast`(int* array, int size)`
and call it with
bool check = firstlast(array, size);
I have a file that has been previously filled with data. The file consists of an array of structures. Each structure represents a round and each array position represents up to 20 rounds for an individual. My .h file:
define READTWENTY_H
class readTwenty {
public:
readTwenty();
void nonZeroes(int, int &);
struct a_round {
int score;
double course_rating;
int slope;
char date[15];
char place[40];
char mark[1];
}; //end structure definition
struct a_round all_info[20];
FILE *fptr;
}; //end class
#endif
In the data file some "rounds" have actual data in them and some have previously been filled with zeroes. I want to count the zero rounds. I have a loop in which I can ask for another "person" value to look at. This value is sent to a function in which the number of zero rounds is determined and returned by reference to a variable named "howMany".
// readMember.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "readTwenty.h"
using namespace std;
int main()
{
int person = 0;
readTwenty personData;
int howMany = 0;
while (person != -999) {
cout << "Which member (keyfield) would you like to see? -999 to stop ";
cin >> person;
if (person == -999)
exit(0);
personData.nonZeroes(person-1, howMany);
cout << "The number of non-zero values for this member is " << howMany << endl;
}//end while
return 0;
}
Once sent to the nonzeroes function as "key" I create an offset into the file and read the 20 rounds for that individual and return by reference the value of count back to the calling routine into variable howMany.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include "readTwenty.h"
#include <errno.h>
#include <cstdio>
readTwenty::readTwenty() {
const char *configfile;
configfile = "scores.dat";
#ifdef WIN32
errno_t err;
if((err = fopen_s(&fptr,configfile, "rb")) != 0)
#else
if ((fp_config = fopen(configfile, "rb")) == NULL)
#endif
fprintf(stderr, "Cannot open cinfig file %s!\n", configfile);
}//end constructor
void readTwenty::nonZeroes(int key, int &count) {
int zeroes = 0;
int offset = key * ((sizeof(all_info[0]) * 20));
fseek(fptr, offset, SEEK_SET);
for (int i = 0; i < 20; i++){
fread(&all_info[i], sizeof(all_info[0]), 1, fptr);
if (all_info[i].score == 0)
zeroes++;
all_info[i].mark[0] = ' ';
}//end for loop
count = 20 - zeroes;
fclose(fptr);
}//end of function nonZeroes
The problem is that the first value that I give for person comes back with the correct number of non-zero rounds. However, each succeeding iteration of the while loop regardless of the second value I give for person comes back with the same result as the first person? Would greatly appreciate any ideas you may have.
I currently have no computer to verify, but one line jumps out to me as it is a common error (for me at least):
The first param to your fread is &all_info[i]; you probably want &(all_info[i]), but this is not how the compiler understands it - & is stronger than [i], so you get (&all_info)[i].
You could also use all_info+i for the same effect.
Null is not declared?
My code:
// Include necessary libraries
#include <cstdlib> // Exits
#include <iostream> // I/O
#include <cstring> // String functions
using namespace std;
int main(int argc, char *argv[])
{
//Declare local Constants and Variables
const char SOKINP[19] = "23456789TtJjQqKkAa"; // Valid Input Characters
char acCards [5]; // Array to hold up to five cards (user input)
bool bErr; // Loop on Error (Calculated)
int i, // Loop variable (Calculated)
iNbrCrd, // Number of Cards 2-5 (user input)
iNAces, // Number of Aces (Calculated)
iTS; // Total Score (Calculated)
...
for (i=0;i<iNbrCrd;i++){
do {
cout << "Enter Card #" << i << " (2-9,t,j,q,k or a) >";
cin >> acCards[i];
cout << endl;
bErr = (strchr(SOKINP, acCards[i]) == null) ? true : false; // *ERROR*
} while (bErr);
}
return EXIT_SUCCESS;
}
[Error] 'null' was not declared in this scope
How do I declare 'null'? I tried including several other libraries.
I'm using Dev C++ v5.4.2
Thanks, ~d
Its not null. It's NULL in all caps. If writing NULL does not work, you can define it yourself by using
#define NULL 0
Use NULL instead of Null.
If you are using it to initialize a pointer and you are using C++11, use nullptr.
Although NULL works for assigning the pointers(even though NULL is not a pointer type but is integer), you may face problems in the below case:
void func(int n);
void func(char *s);
func( NULL ); // guess which function gets called?
Refer to THIS for more details