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!
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 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 =.
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 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!
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;
}
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.