Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
i'm trying to make a very simple program with stacks but i seem to be getting an error when i try to run it. The error says "ISO C++ forbids comparison between pointer and integer.". Any help is greatly appreciated.
My Code:
#include <iostream>
#include <string>
using namespace std;
const int maxstack = 5;
struct stacktype{
string name[maxstack];
int top;
};
void createstack(stacktype &stack);
void destroystack(stacktype &stack);
bool fullstack(stacktype stack);
void push(stacktype &stack, string &newelement);
bool emptystack(stacktype stack);
void pop(stacktype &stack, string &poppedelement);
int main(){
stacktype stack;
string newelement, poppedelement;
char quest;
createstack(stack);
cout<<"Do you want to enter data? (y/n)";
cin>>quest;
while((quest == "y" || quest == "Y") && !(fullstack(stack))){ //I get the error on this line
cout<<"Please enter name";
cin>>newelement;
push(stack, newelement);
cout<<"Do you want to enter data? (y/n)";
cin>>quest;
}
cout<<endl<<endl;
while(!emptystack(stack)){
pop(stack, poppedelement);
cout<<poppedelement<<endl;
}
destroystack(stack);
system("PAUSE");
return 0;
}
void createstack(stacktype &stack){
stack.top = -1;
}
void destroystack(stacktype &stack){
stack.top = -1;;
}
bool fullstack(stacktype stack){
if(stack.top == maxstack - 1){
return true;
}else{
return false;
}
}
void push(stacktype &stack, string &newelement){
stack.top++;
stack.name[stack.top] = newelement;
}
bool emptystack(stacktype stack){
if(stack.top == -1){
return true;
}else{
return false;
}
}
void pop(stacktype &stack, string &poppedelement){
poppedelement = stack.name[stack.top];
stack.top--;
}
quest is a char, yet "y" is a string literal with type const char[2]. When you try to compare these with quest == "y", the string literal is converted to a pointer to its first element, and so you are attempting to compare a char with a pointer. That's what the error is telling you.
Instead of a string literal, you want a character literal like 'y', which has type char.
quest is of char type, so it is single character, and you try to compare it with string literals like "y". Use single quotes for char literals: 'y'.
Since you are writing C++ code, you should use the string class instead of "y".
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 days ago.
Improve this question
So I have this strcpy with tables but I need to change it so that there's no tables and only pointers. When I try to do it, there's an error (I put $$ in front)
So the original:
#include <iostream>
using namespace std;
int main() {
char *mon_strcpy(char destination[], char source[]) {
int index = 0;
while (source[index] != '\0') {
destination[index] = source[index];
index++;
}
destination[index] = '\0';
return destination;
}
return 0;
}
And this is the one I'm trying to make it work:
#include <iostream>
using namespace std;
int main() {
char *mon_strcpy(char *destination, char *source) $${
int index = 0;
while (*source != '\0')
{
*destination = *source;
index++;
}
*destination = '\0';
return destination;
}
return 0;
}
I can't wrap my head around to find the problem.. TIA
In C & C++, you have to declare-define a function outside the other function (here main()). Something like:
char *mon_strcpy(char *destination, char *source) { ... }
int main () {
mon_strcpy(dst, src);
}
Also $$ sign is not allowed to be used inside C++ code except comments.
This just addresses the compiler error you have.
If you have a problem with the function logic, why is it not working? You may want to debug followed by a new question.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I'm writing a code to evaluate a prefix expression. The values of the expression are separated by spaces. So if the input is "+ * 87 89 666", I should get 8409 as the answer. The concept of my code is to store the values to an array and then evaluate them value by value. Right now I'm stuck at the switch part because the compiler says invalid conversion from char to const char*
#include <iostream>
#include <bits/stdc++.h>
#include <algorithm>
#include <stack>
#include <string>
#include <sstream>
using namespace std;
char n[99999][6]={};
int evaluatePrefix(int l)
{
stack<int> Stack;
for (int j = l; j >= 0; j--) {
string x=n[j];
if (n[j][0]!='+' || n[j][0]!='-' || n[j][0]!='*' || n[j][0]!='/'){
stringstream ss;
int a;
ss<<x;
ss>>a;
Stack.push(a);
}
else {
int o1 = Stack.top();
Stack.pop();
int o2 = Stack.top();
Stack.pop();
if (strcmp(n[j], '+')==0){
Stack.push(o1 + o2);
}
else if (strcmp(x, '-')==0){
Stack.push(o1 - o2);
}
else if (strcmp(x, '*')==0){
Stack.push(o1 * o2);
}
else if (strcmp(x, '/')==0){
Stack.push(o1 / o2);
}
}
}
return Stack.top();
}
int main()
{
char e[99999], w[99999];
int i=0;
scanf("%[^\n]%*c",e);
char *token = strtok(e, " ");
while (token != NULL)
{
strcpy(n[i], token);
token = strtok(NULL, " ");
}
return 0;
}
You wrote:
if (strcmp(n[j], '+')==0)
n[j] decays into a char*, but '+' is a single char, not a char*. strcmp needs two char pointers.
https://en.cppreference.com/w/c/string/byte/strcmp
So, you should use:
if (strcmp(n[j], "+")==0)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Am trying to find If a string has all Unique characters and below is my code, But I get the error "invalid types 'char[int]' for array subscript" in the if statement of the function Unique char, can anyone tell me how to correct this
#include <iostream>
#include<cstring>
using namespace std;
bool unique_char(char);
int main()
{
char s;
bool check;
cout << "Enter any string" << endl;
cin>>s;
check = unique_char(s);
if(check)
cout<<"there are no duplicates";
else
cout<<"the string has duplicates";
return 0;
}
// The if statement in this section has the error
bool unique_char(char s)
{
bool check[256] = {false};
int i=0;
while (s != '\0')
{
if (check **[(int) s[i]]**)
return false;
else
{
check[(int) s[i]] = true;
i++;
}
}
}
You need to pass char array rather than a single char.
int main()
{
char s[1000]; // max input size or switch to std::string
bool check;
cout << "Enter any string" << endl;
cin>>s;
check = unique_char(s);
if(check)
cout<<"there are no duplicates";
else
cout<<"the string has duplicates";
return 0;
}
bool unique_char(char* s)
{
bool check[256] = {false};
int i=0;
while (s[i] != '\0')
{
if (check[(int) s[i]])
return false;
else
{
check[(int) s[i]] = true;
i++;
}
}
return true;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm quite a newbie regarding C++ and I'm struggling with one (maybe simple) thing. I want to write a code that checks the first word of the string line and copy the second word of the string line to the string x_start, x_end or num_steps if the first word of the string is equal to "x_start", "x_end" or "num_steps".
The problem I've to solve is the following. In the if statement the value of the second word (label) is copied to the string x_start, but when I go further to below in my debugger, the content of the string x_start disappears after the if statement end bracket. The first sentence of the output (cout) is now equal to: x_start =, where it should be: x_start = 0.
Does anyone know why this happens and how it can be solved? Thanks in advance!
#include <iostream>
#include <fstream>
#include <cassert>
#include <sstream>
#include <string>
int main(int argc, char** argv) {
std::string x_end;
std::string num_steps;
std::string x_start;
std::string line = "x_start 0";
std::istringstream linestream(line);
while (!linestream.eof()) {
std::string label;
std::string value;
linestream >> label >> value;
if(label.compare("x_start") == 0){
std::string x_start = value;
}
else if(label.compare("x_end") == 0){
std::string x_end = value;
}
else if(label.compare("num_steps") == 0){
std::string num_steps = value;
}
else{
std::cout<<"The format of the text in the file 'params.in' ";
std::cout<<"is not correct"<<std::endl";
break;
}
std::cout<<"x_start = "<<x_start<<std::endl;
std::cout<<"x_end = "<<x_end<<std::endl;
std::cout<<"num_steps = "<<num_steps<<std::endl;
}
return 0;
}
Instead of this
std::string x_start = value;
Write this
x_start = value;
Your code introduces new variable x_start that "hides" the existing variable with the same name. This new variable is set, and immediately afterwards destroyed. So, just don't introduce new variable, and all will be fine.
Repeat this also for x_end and num_steps.
When you do this
if(label.compare("x_start") == 0){
std::string x_start = value;
} //^^^^^^^^^^^
you are telling the compiler the following: "I need a new string variable called x_start. This variable will replace x_start from the outside until the end of the if statement".
This is not what you want, though: you want to assign the existing variable! Removing the declaration will fix this problem:
if(label.compare("x_start") == 0){
x_start = value;
}
Now you are assigning an existing variable - the one that would remain there after the if statement. Of course you need to do the same with other declarations that shadow variables outside of their statements.
Note: the technical term for which variable is visible at what part of your program is scope. Look it up to see what it means and how to work with it.
if(label.compare("x_start") == 0){
std::string x_start = value;
}
Should be
if(label.compare("x_start") == 0){
x_start = value;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I tried to write lastindexOf function for my C++ class. After I was trying for 2 weeks, I still can't get it working.
At first , I was trying to follow the logic from this post: CString find the last entry , but since they use CString class instead of char class,I have no success to duplicate the code for char class. I also try the strstr, but I have no luck with that neither. I would appreciate any helps.
here is the code I have came up with so far :
#include
using namespace std;
int lastIndexOf(char *s, char target);
int main()
{
char input[50];
cin.getline(input, 50);
char h = h;
lastIndexOf(input, h);
return 0;
}
int lastIndexOf( char *s, char target)
{
int result = -1;
while (*s != '\0')
{
if (*s == target ){
return *s;
}}
return result;
}
Try this:
int lastIndexOf(const char * s, char target)
{
int ret = -1;
int curIdx = 0;
while(s[curIdx] != '\0')
{
if (s[curIdx] == target) ret = curIdx;
curIdx++;
}
return ret;
}