C++ code not doing what I want [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 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
When I call this function it doesn't display any *. But when I change == to < it works. Why?
void starBox(int size){
for(int i = 0; i == size; i++){
for(int j = 0; j == size; j++){
cout << '*';
}
cout << endl;
}
}

I think your problem is due to misunderstanding of what the condition in the middle of for loop means. This is a loop continuation condition, meaning that the loop needs it to be true to proceed. Loop's post-condition is the inverse of its continuation condition.
When I call this function it doesn't display any *.
This is because the loop continuation condition is false unless size is zero, so loop body is skipped.
But when I change == to < it works. Why?
Because the loop continuation condition becomes true, and stays true until the loop is over. That is why < works; != would work as well.

Related

How to fix input and output problem with files in C++? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 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
I can't find a problem with my code. It's loading in DEV C++ but after that the window with "Program has stopped working" pop up.
fstream file;
file.open("dane1.txt");
string linia;
string tab[5];
int i = 0;
do
{
getline(file,linia);
cout<<linia<<endl;
tab[i]=linia;
i++;
}
while(!file.eof());
file.close();
ofstream file2("wynik.txt");
if (file2)
{
for(int i=5;i>0;i--)
{
file2<< tab[i];
file2<< endl;
}
}
else
{
cout<<"You have problem with file!"<<endl;
}
pliki.close();
I want to get lines from 1st file (dane1.txt) and then put it in diffrent order in fail "wyniki.txt"
string tab[5];
// ...
for(int i=5;i>0;i--)
{
file2<< tab[i];
file2<< endl;
}
The first iteration of this for loop attempts to access tab[5] which, of course, doesn't exist since the five-element tab array contains only tab[0] through tab[4]. Undefined behavior, and the near-certain reason for your program crashing.

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.

Unterminating while loop in c++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 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
I'm trying to store values in double array in a while loop with a terminating value of -1. It seems the values are being stored fine but when -1 is entered the loop does not terminate. Any ideas? Thank you so much!
screen shot of the problem area
int main(void)
{
double a[7],dev[6],mean, std;
int i,n;
int entered;
char letg[6];
cout<<"Please enter the test grades one at a time (max 6)\n";
cout<<"enter a -1 when you are done entering scores\n";
//based off class notes
i=0;
cin>>entered;
while (entered>0)
{ a[i]=entered;
i++;
cin>>entered;
}
Change
{while (entered>0)
to
while (entered>0) {
Your process is executing the statement a[i]=entered; indefinitely. You should put the braces after the while loop:
while (entered>0)
{
a[i]=entered;
i++;
cin>>entered;
}

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.