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 6 years ago.
Improve this question
I've 10 numbers to be read. My task is to generate exceptions with the numbers are either a negative number or a even number. Below is the code I wrote, but it isn't working.
#include <iostream>
using namespace std;
int main()
{
int a[10];
for(int i=0;i<10;i++)
{
cin>>a[i];
}
for(int i=0;i<10;i++)
{
try{
if(a[i]<0 && a%2==0)
throw a[i];
}
catch(int a)
{
cout<<"You ve entered a -ve number or a even number";
}
}
return 0;
}
This was the error shown:
In function 'int main()': 16:24: error: invalid operands of types 'int [10]' and 'int' to binary 'operator%'
Thanks for the help!
Two things:
You are modulo-ing an array a with an integer 2. You should dereference it to get the value: a[i] % 2 == 0.
You do a boolean AND, where you want a boolean OR (according to your question): || instead of &&.
Typo here. You can not do modulo operation on an array. a%2==0 should be a[i]%2==0. Also change && to || as you are trying to find "either a negative number or a even number".
Related
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 1 year ago.
Improve this question
I need to compare a character in a string that is in an array with another string. This is a functioning but simple version of my problem:
#include <iostream>
using namespace std;
int main() {
string a_ray[1] = {"asd"};
if (a_ray[0][0] == "a") {
bool a;
}
return 0;
}
Error message: ISO C++ forbids comparison between pointer and integer [-fpermissive]|
What causes this? And how can I do what I want to do in the correct way?
Thank you in advance!
Since you are comparing against a character, your code should be
if (a_ray[0][0] == 'a')
You are trying to compare a character with a character array, hence the error message.
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 2 years ago.
Improve this question
I'm just making a program and this happens
I don't understand where is converts like that.
This program is supposed to see how many of each character is in a input string. It is work in progress but I already made a for loop to process the input.
Error:
11:30: error: conversion from 'int' to non-scalar type 'std::vector<int>' requested
Code:
// Example program
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string input;
vector<int> letters = (26,0);
vector<char> alpha = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
cin >> input;
for(int i = 0; i < input.size();i++){
for(int j = 0; j < alpha.size(); j++){
if(input[i]==alpha[j]){
letters[j] ++;
}
}
}
}
Your mistake is that you try to call constructor with 2 parameters, using assign without explicitly showing what are that 2 ints mean. You can choose 2 ways of this implementation:
vector<int> letters = vector<int>(26, 0);//here you call copy constructor
vector<int> letters(26,0); // here you call constructor with 2 parameters
Compiler just don't know what do you mean writing "vector letters = (26,0);" it's could be everything - from just 1 int number to implicit convertion to your classes; So you should to show it explicitly.
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 3 years ago.
Improve this question
In this problem,which is an easy and a simple one, i do the sum and the product. I wanted to do with a "for" instruction to understand how this thing is working.
#include < iostream >
using namespace std;
int main()
{
int n,i,s=0,p=1;
cin>>n;
for (i=1;i<=n;i++)
s=s+i;
p=p*i;
cout<<s<<" "<<p;
}
I can't explain why the result is "6" for sum and "4" for product...
Can someone explain me why the code is showing this?
If i put the instructions from the "for" structure between of braces,it is showing "6" for sum and "6" for product.
First of all, it's not < iostream >, it's <iostream>. White space is not allowed there. Second of all, despite the indentation, p=p*i; is outside the for loop. Turn on compiler warnings:
prog.cc:7:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
7 | for (i=1;i<=n;i++)
| ^~~
prog.cc:9:10: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
9 | p=p*i;
| ^
Use {} to fix this:
for (i=1;i<=n;i++)
{
s=s+i;
p=p*i;
}
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
this is the code to check whether the string is palindrome or not.
#include<iostream>
using namespace std;
int main()
{
char a[20]="",b[20]="";
int len,c;
cout<<"Enter the string";
cin>>a;
len=strlen(a);
cout<<len<<endl;
for(int i=len-1,j=0;i>=0,j<=len;i--,j++)
{
b[i]=a[j];
}
cout<<a<<endl;
cout<<b<<endl;
cout<<strlen(b);
c=strcmp(a,b);
cout<<c<<endl;
if(a==b)
{
cout<<"palindrome";
}
}
in the above code if the input is sas the output should be palindrome. But it is not working pls say what is the error.
The strcmp function returns an integer greater than, equal to, or less than zero,
accordingly as the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2.
So equal strings will return 0.
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 1 year ago.
Improve this question
This is a function to check a number for a power of two. Although the compiler does not print anything, it's also not showing any errors. Please let me know if the logic is correct or not.
Here is my code:
#include <bits/stdc++.h>
using namespace std;
bool isPowerofTwo(long long n)
{
// Your code here
for (int i = 1; i <=n; i<<1)
{
if(i==n){
return true;
}
}
return false;
}
int main()
{
cout << isPowerofTwo(2);
return 0;
}
The expression i<<1 in the third statement (the "iteration expression") of your for loop doesn't actually do anything (that is, it doesn't modify the value of i). You need, instead, i<<=1 (or, in 'long form', i = i << 1).
Also, please read: Why should I not #include <bits/stdc++.h>?. In your code, the only standard header you need is #include <iostream>. Another good post to read is: Why is "using namespace std;" considered bad practice?.