Error] ISO C++ forbids comparison between pointer and integer [-fpermissive] - c++

I'm a beginner in c++ programming and I have this activity in school. I keep getting [Error] ISO C++ forbids comparison between pointer and integer [-fpermissive] in line 15. How do you solve this? Thanks!
#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
int pass[5];
int x;
main()
{
cout<<"\nEnter pin code: ";
for(x=0;x<=4;x++)
{
pass[x]=getch();
putch('#');
}
if(pass==86222)
cout<<"\nW E L C O M E!";
else
cout<<"\nIncorrect Pin Code";
getch();
}

You are doing things in a very strange way. If you wanna compare ints. Take and int, read it and compare, Why is the array needed?
The best and simple way to do this is to use only ints.
#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
main()
{
int pass;
cout<<"\nEnter pin code: ";
cin>>pass;
if(pass==86222)
cout<<"\nW E L C O M E!";
else
cout<<"\nIncorrect Pin Code";
getch();
}
If you want to do it the way you want, then use strcmp()
#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
char pass[5];
int x;
main()
{
cout<<"\nEnter pin code: ";
for(x=0;x<=4;x++)
{
pass[x]=getch();
putch('#');
}
if(!strcmp(pass, "86222"))
cout<<"\nW E L C O M E!";
else
cout<<"\nIncorrect Pin Code";
getch();
}

You are reading characters and comparing them as ints. That won't work....
The following first places the charactes into an array of characters, then converts that aray to an int and compares the int:
char passch[6];
int pass, x;
main()
{
cout<<"\nEnter pin code: ";
for(x=0;x<=4;x++)
{
passch[x]=getch();
putch('#');
}
passch[5]= '\0';
pass= atoi(passch);
if(pass==86222)
cout<<"\nW E L C O M E!";
else
cout<<"\nIncorrect Pin Code";
getch();
}

pass is an array (which is implemented in c++ as a pointer) and 86222 is an integer. you cannot compare those.
As #haris said in their comment, you really just want to store the input as an integer. you do this with std::cin >> pass. then you can compare pass with your stored value.

Related

how to use data stored in a map to sort an array using std::sort() function in c++?

i wanted to use a hash map to sort a string on the basis of the values the map has.but i just could not find a suitable way..please help me find a way.
so here is a c++ code that I wrote please help me how to write it better
i want to know how to use std::sort() by passing a data structure for sorting
#include<bits/stdc++.h>
using namespace std;
unordered_map<char,int>m;
bool h(char a,char b)
{
return m[a]<=m[b];
}
int main()
{
int t;
cin>>t;
while(t--)
{
//unordered_map<char,int>m;
for(int i=1;i<=26;i++)
{
char a;
cin>>a;
m[a]=i;
}
string s;
cin>>s;
sort(s.begin(),s.end(),h);
cout<<s<<endl;
//m.erase(m.begin(),m.end());
//cout<<endl<<m.size();
}
}
Your Compare function does not fulfill the strict weak ordering requirement.
return m[a] <= m[b]; should be return m[a] < m[b];
With that change, your program works correctly and sorts the std::string in the order your map holds. If you enter the characters zyxwvutsrqponmlkjihgfedcba your sort will sort the string in reverse alphabetical order.
Suggestions:
Read about why you shouldn't include <bits/stdc++.h>. Include the correct headers instead:
#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>
Try to avoid magic numbers like 26. You can make your loop like this for the same effect:
for(int i = 0; i <= 'Z'-'A'; i++)
1-26 and 0-25 (as the above loop produces) will have the same effect.
Avoid global variables such as m. You can make it local and refer to it in a functor, like a lambda.
Read Why is using namespace std; considered bad practice?
I'm going to assume that the commended out lines were what you were trying to get working.
#include <unordered_map>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
cout << "enter number of times" << endl;
int t;
cin>>t;
while(t--)
{
unordered_map<char,int>m;
cout << "enter 26 characters" << endl;
for(int i=1;i<=26;i++)
{
char a;
cin>>a;
m[a]=i;
}
cout << "enter a string" << endl;
string s;
cin>>s;
sort(s.begin(),s.end(), [&](char a, char b)
{
return m[a]<m[b];
});
cout<<s<<endl;
m.erase(m.begin(),m.end());
cout<<endl<<m.size();
}
}

How to convert string of characters to their respective int values?

I'm trying to convert a string of characters into their ASCII int values. However I cannot get this to work for one even one character in the string. I would expect a result of 72 when entering 'H', but it returns a 0 (the same for every character I've tried).
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
string a;
cin >> a;
const char * b = a.c_str();
int c = atoi(b);
cout << int(c) << endl;
}
Thanks in advance.
atoi parses the C-string interpreting its content as an integral number, i.e.
int i = atoi("123"); // i = 123
You don't want this: you want to know the ASCII value of every single character of the input string. To figure this out, you can use this code snippet:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a;
cin >> a;
for(int c: a)
cout << c << '\n';
}
I'm not quite used to the string library but simply type:
cout<<(int)a[pozition];
You can place that in a for like this.
for(int i=0;i<a.length();i++)
cout<<(int)a[i]<<endl;
You can just cast each character to an int
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a;
cin >> a;
for (int i = 0; i < a.length(); i++) {
cout << (int) a[i] << endl;
}
}

How to assign char to string object in c++?

i am trying to convert the string into capital letter string by assigning single char's to string like this:-
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;
int main()
{
string a;
getline(cin,a);
string b;
b.reserve(a.size()+1);
for(int i=(a.size()),i1=0;1;i1++)
{
if(b[i1]!='\0')
b[i1]=(char)toupper(a[i1]);
else
{
a[i1]='\0';
break;
}
}
cout << b <<endl;
}
every when run a.out by ./a.out ,Only endl gets prints
here is sample run:-
$ ./a.out
play clash royale
$
What is wrong in my program?? How can I assign single char to string??
There are some issues with your program. The main one is probably the diference between string reserve and string resize. What you want in your program is already had a string of a.size() length, so, use b.resize(a.size()).
A working version is bellow (there are better ways to write this, just being most consistent with OP proposal):
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;
int main()
{
string a;
getline(cin,a);
string b;
b.resize(a.size());
for(int i1=0; i1 < a.size();i1++)
{
if(a[i1]!='\0')
b[i1]=(char)toupper(a[i1]);
else
{
b[i1]='\0';
break;
}
}
cout << b <<endl;
}

Ask user to enter his/her name, and then reverse the given name and show it on screen. using character array

My code is this But i am getting garbage values. how do i make it general so that anyone can use it?
#include <iostream>
#include <string>
using namespace std;
void main()
{
char ch[31];
cin.get(ch,31);
for (int i = 30; i >= 0; i--)
{
cout << ch[i];
}
system("pause");
}
You are taking garbage char from you empty cells.
You must first know the number of char inserted by the user
use reverse function from algorithm header file and use string instead of char.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string s;
getline(cin,s);
reverse(s.begin(),s.end());
cout<<s<<endl;
return 0;
}

C++ segmentation fault on programming exercise

I am getting a segmentation fault error when trying to solve this programming marathon C++ exercise but I can't find the error anywhere:
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <stdio.h>
#include <string.h>
using namespace std;
int main(void)
{
int m,n,i,di,x,y;
char* let;
cin >> n >> m;
x=0;
y=0;
for (i = 0; i < n; i++)
{
cin >> let >>di;
if ((strcmp(let,"S"))||(strcmp(let,"O"))){
di=(-di);
}
if ((strcmp(let,"N"))||(strcmp(let,"S")))
{
x=+di;
}
if ((strcmp(let,"L"))||(strcmp(let,"O")))
{
y=+di;
}
if ((y*y)+(x*x)>(m*m))
{
cout << "1";
return 0;
}
}
cout << "0";
return 0;
}
This code:
char* let;
cin >> let
stores user input to the memory pointed to by let.
This is misuse of an uninitialized pointer. cin trusts that you have pointed it to valid memory, but you haven't assigned anything to it. Where it points to is unknown.
The easiest solution would be to change let to a proper C++ std::string.