First input number turns 0 after the executing the codes [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 10 months ago.
Improve this question
I'm writing a program to generate a line equation using given slope and y-intercept, below are the codes i extracted from my program:
int m,c;
cout<<"m >>";
cin>>m;
cout<<endl;
cout<<"c >>";
cin>>c;
cout<<endl;
if (m=0){
cout<<"y= "<<c;
}
else if(m>0){
if (c>0){
cout<<"y="<<m<<"x+"<<c;
}
else if (c<0){
cout<<"y="<<m<<"x"<<c;
}
else {
cout<<"y="<<m;
}
}
else {
if (c>0){
cout<<"y="<<m<<"x+"<<c;
}
else if (c<0){
cout<<"y="<<m<<"x"<<c;
}
else {
cout<<"y="<<m;
}
}
When I input 8 8, the output is as follow:
y=0x+8
When I input 8 -8, the output is as follow:
y=0x-8
When I input 0 5, the output is as follow:
y=0x+5
Even when I try adjust the front part of my codes to
cin>>m>>c;
It doesnt seem helping, which part of my codes has given such error ?

You are setting m to 0 in the first if check. Should be using == not =.

Related

Memory limit exceeded bad_alloc [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 2 years ago.
Improve this question
I was solving a problem on codeforces and it gave me memory exceeded and even on my local compiler it says 'std::bad_alloc'
can someone explain why and how to solve this !!!
int tst;
cin >> tst;
while(tst--){
string s;
cin >> s;
ll n=s.length();
string ans;
if(n==2){
cout<<s<<endl;
}
else{
if(n%2!=0){
for(int i=0;i<n;i+2){
ans.push_back(s[i]);
}
ans.push_back(s[n-1]);
}
else{
for(int i=0;i<n;i+2){
ans.push_back(s[i]);
}
}
cout<<ans<<endl;
}
}
So, firstly, specify your problem statement.
Secondly, please, pay attention to this cycle:
for(int i=0;i<n;i+2){
ans.push_back(s[i]);
}
The cycle which is written in this way would be infinite, because You are not incrementing i at all, and i will be equal to zero all way cycle goes(infinite times).
So, additionally, You are trying to enlarge Your container by n element in an infinite cycle. You may see, memory is not an infinite resource.
To fix this issue, just rewrite cycle like that:
for(int i=0;i<n;i+=2){
ans.push_back(s[i]);
}

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.

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;
}

Why do i get this wrong result? [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 8 years ago.
Improve this question
I'M trying to make a code that is able to increase int 'd' by one each time the a+b reach 20.
and if there still any number less than 20 then this will be the int 'c'.
but instead of getting the right result in my next program which is
49-0
i get this wrong answer
47-40
what should i do ?
#include <iostream>
using namespace std;
int main(){
int a=50;
int b=18;
int c=a+b;
int d=0;
int i;
for(i=0;i<c;i++)
{
while(c>20)
{
d+=1;
c=c-20;
break;
}}
cout<<d<<"-"<<c;
return 0;
}
The problem is in your while loop:
while(c > 20)
{
d+=1;
c=c-20;
break;
}
The loop will only execute once because of your break statement.