0

C++ Ballpark order challenge

Hello im trying to solve the Ballpark Orders challenge in c++ and for some reason, only first item gets right amount when calculation total sum. Here is the code. #include <iostream> #include <sstream> using namespace std; int main() { string burger = "Cheeseburger"; string nachos = "Nachos"; string pizza = "Pizza"; string water = "Water"; string coke = "Coke"; int sum = 0; string order; cin >> order; string arr[4]; int i = 0; stringstream ssin(order); while (ssin.good() && i < 4){ ssin >> arr[i]; ++i; } for (int j = 0; j < 4; j++){ cout << sum; if (arr[j] == nachos || arr[j] == pizza) { sum += 6; } else if (arr[j] == burger ) { sum += 10; } else if (arr[j] == water ){ sum += 4; } else if (arr[j] == coke ){ sum += 5; } else { sum += 5; } } double tax = sum * 0.07; cout << sum + tax; }

6th Jan 2022, 11:38 PM
Marek Truska
6 Answers
+ 5
Replace cin >> order; by getline(cin, order); to fix the issue, as std::cin is designed to stop extracting characters when encountering any kind of whitespace, while getline() by default only stops reading at the newline character.
7th Jan 2022, 12:45 AM
Shadow
Shadow - avatar
+ 1
Thanks for the suggestion. It indeed work and replacing std cin with get line did the trick. Thanks.
8th Jan 2022, 6:33 AM
Marek Truska
+ 1
#include <iostream> using namespace std; int main() { string order[5]; cin >> order[0] >> order[1] >> order[2] >> order[3] ; string item[]={"Nachos", "Pizza", "Cheeseburger", "Water", "Coke"}; int sum = 0; for (int x=0; x<4; x++){ if (order[x] == item[0]){ sum += 6 ; }else if (order[x] == item[1]){ sum += 6 ; }else if (order[x] == item[2]){ sum += 10 ; }else if (order[x] == item[3]){ sum += 4 ; }else if (order[x] == item[4]){ sum += 5 ; }else sum += 5 ; } cout << sum + sum * .07 ; return 0; } https://code.sololearn.com/cf5K1k91UFf5/?ref=app
9th Nov 2022, 11:47 AM
Scott D
Scott D - avatar
+ 1
Hi I am solving this problem but ther is a bug in my code that I am not getting. So, please help me to find out for this. #include <stdio.h> #include <string.h> int main(){ char str[200] = {0}, s[50]={0}; float ttl=0; int mrp=0, cnt =0; scanf("%[^\n]%*c", str); for(int i=0, j=0; strlen(str)>=i; i++) { s[j++]=str[i]; if((str[i]==' ') || strlen(str)==i) { s[j-1] = '\0'; j = 0; cnt++; if(strcmp("Nacho", s) == 0 || strcmp("Pizza", s) == 0) mrp =6; else if(strcmp("Cheeseburger", s) == 0) mrp =10; else if(strcmp("Water", s) == 0) mrp =4; else if(strcmp("Coke", s) == 0) mrp =5; else mrp =5; ttl += (float) mrp; } } if(cnt<4) ttl += (4-cnt)*5.0; ttl += (0.07*ttl); printf("%.2f\n", ttl); return 0; }
11th Jun 2023, 5:04 PM
Amaresh Ray
Amaresh Ray - avatar
0
So lets say Water is first Word ina string. Sum variable gets the correct amount of 4 but then if second Word is again water, it Will get 5 now, according to else condition. And Like this, its with every item, first is always correct then others are not. I tried to look hard for solution but failed to do so for quite some time now. Thanks l.
6th Jan 2022, 11:41 PM
Marek Truska
0
The only way that I found to solve this challange was to create a variable named braker, containing a zero, and each time an order fit one of my if statements I turn the value of the variable braker to one and then to the end of my loop I have an if statement that require the braker to be at zero to work... And everytime the loop make a new turn the braker turn back to zero. #include <iostream> using namespace std; int main() { unsigned short bill; string words; for(int roll=0;roll<4;roll++){ cin >> words; int braker = 0; if(words == "Water"){ bill+=4; braker = 1; } if(words == "Nachos"||words == "Pizza"){ bill+=6; braker = 1; } if(words == "Cheeseburger"){ bill+=10; braker = 1; } if(braker == 0){ bill+=5; } } cout << bill+bill*0.07 << endl; return 0; } If anyone know a better way to solve this let me know...
22nd Sep 2022, 2:34 AM
Young Wizard Apprentice
Young Wizard Apprentice - avatar