Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
I have this simple program to formulate a table of any given number however at the end i want the user to be prompted to either end the program or formulate another table.however the loop does not occur.(i'm only a newbie)
int table(){
int tablenumber;
int tablecount;
cout<<"which number's table would you like to print?"<<endl;
cin>>tablenumber;
cout<<"till which number would you like to multiply it?"<<endl;
cin>>tablecount;
for(int i=0; i<=tablecount; i++){
cout<<tablenumber<<" X "<<i<<"="<<tablenumber*i<<endl;
}
}
int main(){
bool yes=true;
bool no=false;
char answer= yes;
while(answer==true){
table();
cout<<"would you like to formulate another table?(yes/no)"<<endl;
cin>>answer;
}
return 0;
}
The problem is that answer is a char and you are trying to compare it with a bool. true and false are always zero (false) and any non zero number (true), so once you read info into answer the ascii value of the inputted char will not be equal to 0 (The int value for false).
Instead read input and loop while answer equals yes (Or y/Y) since answer is a char. Or make answer a string:
string answer = "yes";
while (answer == "yes" || answer == "Yes") {
//code
}
As a followup on #GBlodgett's answer, I'd like to point out a possible solution:
do{
table();
cout<<"would you like to formulate another table?(yes/no)"<<endl;
cin >> answer;
}while(answer == "yes");
As you can see, you don't even need those two bools. However, since you want the answer to be "yes", you need to make answer a string. Good luck!
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 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]);
}
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've just started coding in c++ and now I have an exercise that I can't do because the code seems to not work.
I've to find the max and the min with a sequence of n numbers (in this case i already know that they are 4). I've to use while.
I've just started so I don't know how return properly works...
there aren't syntactical errors but when I run it ask me the number but then it says that the algorithm ends with 0 value.
Here's the code, if you can help me thank you!
#include <iostream>
using namespace std;
main ()
{ float mag,min,i,a;
mag=0;
min=0;
i=0;
while (1)
{
if (i<5)
{ cout<<"insert a number"<<endl;
cin>>a;
if (i = 0)
{ mag=a;
min=a;
}
else
{ if (a<min)
{ min=a;
}
else
{ if (a>mag)
{ mag=a;
}
}
}
i=i+1;
}
else
{ cout<<"maggiore= "<<mag<<endl<<"min= "<<min<<endl;
}
return 0;
}
system ("pause");
}
I see at minimum one problem:
if (i = 0)
This is assignment of i to 0 and compare the result of assignment, which is always false if you assign a 0.
I believe you want only compare and not assign, so you have to use:
if ( i == 0 )
The next problem is
return 0;
This will always end the current function, if the function is main(), it will terminate your program. In your case, you can simply remove the return statement, as in main it will return 0 by default if the function ends.
But if you use
while (1)
without any return, your program runs endless. I don't know what is the expected behavior.
Rest looks fine.
Hint: Your indentation is a bit special. :-)
1st it should be i==0 not i=0 in the if
2nd you should place that return 0 after that cout maggiore or it will close after the first loop
3rd you don't need that system pause there. It does literally nothing. You should either remove it or place it exactly before the return.
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.
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
The program will ask how many items to store, and then asks for the name of the item and the price.
Finally, all the items must be displayed in a table. Duplicate items must be displayed as one item. Use only small letters.
Display should look like this:
My problem #1 is that if an item with its price is equal to another input, the program should count them as 1 and the quantity of an item will depend on how many same input(the item and price).
Problem #2 is that the 1st for loop in the void function does not work properly, it always ask an item and price and doesn't stop, (#3) and the item number too is not working properly. Here's the picture:
Here is my code:
#include<iostream>
using namespace std;
int num;
void numItems(int num){
string items[num];
double price[num];
int quantity[num];
for(int x=0;x<num;x++){
cout<<"Item #"<<x+1<<": ";
cin.get();//cin.ignore();
/*cin>>items[x];*/getline(cin,items[x]);
cout<<"Price #"<<x+1<<": ";
cin>>price[x];
if(x=1){ //(x==1) fixed
if(items[x]==items[x-1] && price[x]==price[x-1]){
quantity[x]++;
}
}
cout<<"\n\n";
}
cout<<"============RECEIPT=============\n\n";
cout<<"Items\t\t\t\tPrice\t\t\t\tQuantity"<<endl;
for(int x=0;x<num;x++){
if(x=1){
if(items[x]==items[x-1]){
cout<<items[x]<<"\t\t\t\t"<<price[x]<<"\t\t\t\t"<<quantity<<endl;
}else continue;
}
}
}
int main(){
cout<<"===ITEM AUDIT==="<<endl;
cout<<"Enter number of items to store: ";
cin>>num;
cout<<"\n\n";
numItems(num);
}
The if statement has a hard-to-spot issue:
if(x=1){
should be
if(x == 1) {
Don't forget to fix both typos!
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;
}