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;
}
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 5 years ago.
Improve this question
I'm new to C++ and I don't understand why I'm getting a not declared error on this:
int main(){
string listOfColors[5] = {"red","blue","green","yellow","magenta"};
for(int i = 0;i < sizeof listofColors;i++){
cout << listofColors[i] << "\n";
}
return 0;
}
This is my first utilization of an array so far, so I may just not be declaring it correctly. I also had the array declaration before the main function beforehand.
You declared your variable as listOfColors (capital "O"), and then you use it as listofColors in your for loop. All you need to do is to capitalize the "O" when using your variable.
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".
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
stack<string> myStack;
string TemStore;
string return_value;
for(int i=0;i<input.length();i++)
if((input.at(i)!='32') && (input.at(i)!= '46'))
TemStore+=input.at(i);
if(input.at(i)=='32') //problem occured here << dont know why it says as the title.
myStack.push(TemStore);
myStack.push('32');
TemStore='\0';
}
i dont know why i getting error like this.. can anyone help me?
You need to use braces with your for-loop:
for(int i=0;i<input.length();i++) { // brace
if((input.at(i)!='32') && (input.at(i)!= '46'))
TemStore+=input.at(i);
if(input.at(i)=='32')
myStack.push(TemStore);
myStack.push('32');
TemStore='\0';
} // brace
Otherwise, the loop will only apply to this part:
if((input.at(i)!='32') && (input.at(i)!= '46'))
TemStore+=input.at(i);
And the i used here:
if(input.at(i)=='32') //problem occured here << dont know why it says as the title.
will be out of scope.
In addition, it looks like your second if-statement needs braces as well:
if(input.at(i)=='32') { // brace
myStack.push(TemStore);
myStack.push('32');
TemStore='\0';
} // brace
Basically, you need to use braces whenever you want a for-loop, if-statement, etc. to apply to two or more statements. Only when you have one statement can you omit them:
// Needs braces because there is more than one statement underneath.
if (condition) {
// statement 1
// statement 2
}
// Does not need braces because there is only one statement underneath.
if (condition)
// statement 1
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?.