C++ comparing string elements to an int [closed] - c++

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 4 years ago.
Improve this question
I have number lying in a string variable.
I wanna check if every one of its elements is equal to some value, so I use the for loop to loop over every element and use if:
int zera = 0, jedynki = 0;
for (int i = 0; i < liczba.length(); i++) {
if (liczba[i] == 0) zera ++;
else if (liczba[i] == 1) jedynki ++;
}
liczba is a string.
I know now that I can't do that. I tried to convert this int into char but still, nothing happened.
What's wrong here? What should I do?

you're comparing int with char
should be:
if (liczba[i] == '0') {}
else if (liczba[i] == '1') {}

should be used:
if (liczba[i] == '0')
or using atoi:
if (atoi(liczba[i]) == 0)

Related

For loop only executing once? [closed]

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 don't know if I'm just being stupid, but Visual Studio says that my for loop is only executing once, and it does seem to be the case. However I can't figure out why. I'd really appreciate it if anyone could tell me what I'm doing wrong.
for (int i = (nbToVerify - 1); i == 1; i--)
{
if (nbToVerify % i == 0)
{
nbIsPrime = false;
break;
}
else
{
nbIsPrime = true;
}
}
Thanks!
i == 1 should be i != 1, that's it.

How do i get rid of this error right operand of comma operator has no effect ( wunsued value)? [closed]

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
i am having an issue with this code so all the tarif = x; is underlined when i run the program, but i don't see how i could solve this and exactly what is the error ? thank you for your help !
double saaq::Camion::tarificationAnnuelle() const
{
double tarif;
if(m_nbEssieux == 2 && m_poids >= 3001 && m_poids <= 4000)
{
tarif = 570,28;
}
if(m_nbEssieux == 2 && m_poids >= 4001)
{
tarif = 905,28;
}
if(m_nbEssieux == 4)
{
tarif = 2206,19;
}
if(m_nbEssieux == 5)
{
tarif = 2821,76;
}
if(m_nbEssieux >= 6)
{
tarif = 3729,76;
}
return tarif;
}
It's an obvious typo when you write double constant:
tarif = 570,28;
should be
tarif = 570.28;
that applies to all other double assignments..
Also you should initialise your tarif variable,
double tarif = 0;

Why do I keep getting an "expected expression error" in my While Loop? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
So I am trying to put a code together for my homework assignment and for some reason I keep getting an expected expression error in the condition of my while loop where I am putting the <= end_money part. The error shows up on the <= . This is the only place in my code where I am getting this error. If someone could pleaseeee help me, I would greatly appreciate it. I've been stuck for so long. Here is the snippet:
Edit: Also there is an ending brace for the while loop, i just forgot to paste it here.
int player_total = 0;
int dealer_total = 0;
int player_bet;
int card_value = 0;
const int end_money = 1000;
const int starting_money = 100;
string card;
string response;
while (player_bet >= 0 && <= end_money)
{
cout << "You have $100. Enter bet: ";
cin >> player_bet;
if (player_bet <= starting_money) {
return true;
}
else if (player_bet > starting_money) {
cout << "You only have $100 to bet. Enter bet: ";
}
Because that is not a valid expression.
Change this:
while (player_bet >= 0 && <= end_money)
To:
while (player_bet >= 0 && player_bet <= end_money)
Translation:
while player-bet is 0 or bigger, and also while player-bet is end-money or smaller.
Your original expression is roughly:
while player_bet is zero or bigger and also while (something unknown and not specified) is less than or equal to end-money.

Project Euler #5 - Why won't this while loop finish? [closed]

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
If I change the code line "num++" to "num+=2520", the code runs fine and returns the correct answer, but I'd like to know why it doesn't run as is, primarily because I didn't think of the fact that the number must be a multiple at 2520 before looking the answer up, and I don't see why my own code isn't correctly giving the answer without that change. To me, it seems correct. Unfortunately, the while loop never ends.
My guess is it has something to do with how long the correct number is (232792560), because if I lower the requirements even a little bit (from 9 to 8, per se), the while loop manages to finish.
long long int num = 1;
int div_counter = 1;
bool check = false;
while(!check)
{
for(int i = 2; i < 21; i++)
{
if(num % i == 0)
{
div_counter++;
}
}
if(div_counter == 20)
{
check = true;
}
else
{
num++;
div_counter = 0;
}
}
return num;
You have to reset div_counter to 1 instead of 0.
Your for loop only runs from 2 to 20 inclusive, so if div_counter starts at 0 the max value it can reach is 19.

how to get correct answer merge 2 sorted arrays?! C++ [closed]

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 wrote a little algorithm for marge to sorted array. but i have problem with that.
#include <iostream>
using namespace std;
int main() {
// main function started form here:
int firstArray[10] = {1,3,5,7,9,11,13,15,17,19};
int secondtArray[10] = {2,4,6,8,10,12,14,16,18,20};
int mergedArray[20];
int firstCounter=0 , secondtCounter=0 , mergedCounter=0;
while(firstCounter < 10 && secondtCounter < 10){
if(firstArray[firstCounter] < secondtArray[secondtCounter]){
mergedArray[mergedCounter] = firstArray[firstCounter];
firstCounter++;
} else {
mergedArray[mergedCounter] = secondtArray[secondtCounter];
secondtCounter++;
}
mergedCounter++;
}
while(firstCounter < 10) {
mergedArray[mergedCounter] = firstArray[firstCounter];
firstCounter++;
mergedCounter++;
}
while(secondtCounter < 10) {
mergedArray[mergedCounter];
secondtCounter++;
mergedCounter++;
}
for(int j=0; j<20; j++){
//cout << mergedArray[j] << endl;
}
cout << mergedArray[19];
return 0;
}
in outpout for array mergedArray[19] i get something like this: 2686916!!!
i don't know why i get this value. how can i fix that. and why i get this value.
Typo in last while. You may increase your warning level to let your compiler show you your typo (warning: statement has no effect [-Wunused-value]).
while(secondtCounter < 10) {
mergedArray[mergedCounter];
secondtCounter++;
mergedCounter++;
}
should be
while(secondtCounter < 10) {
mergedArray[mergedCounter] = secondtArray[secondtCounter];
secondtCounter++;
mergedCounter++;
}
As pointed out by WhozCraig's comment, you're not assigning any value to mergedArray[19] because you left out the assignment part of the statement.
Since you haven't assigned a value, it's printing out whatever value happens to be at that memory address from previous usage. If you run your program (as it's currently written) several times, you'll see that the number there might change. Also, if you'd printed out the values in mergedArray before assigning anything, you'd see more such meaningless (to you in the current application) numbers.