+ 2

Could I get some help checking the elements of the char array?

The question from the code coach I’m working on are in comments. I need to add the points per snack but int points stays at 0. Lettuce = 5, Carrot =4, Mango=9, Cheeseburger =0 https://code.sololearn.com/cBq64741Z1TX/?ref=app

12th Mar 2022, 7:22 PM
zack
11 ответов
+ 1
#include <iostream> #include <sstream> using namespace std; int main() { string input, output; stringstream ss; int points = 0; getline(cin,input); ss << input; while(ss >> output){ // Wright your string compare // and point additions in here. // e.g if(output == "Maple") // point += 9; // } // print your output here. return 0; } edit: although I was aware of stringstream, I've never used it or seen it being used on here, so...thank you for making me look into it.lol
13th Mar 2022, 1:08 AM
rodwynnejones
rodwynnejones - avatar
+ 2
To represent a string literal, you need to double quotes ex: "A". For character, literal, you need single quotes ex: 'A' snacks[i] returns a single charecter since it is character array.. Comparing with string is wrong way. Use a string instead of char array and compare without loop... what are trying actually by this code? according your description, char array won't work. edit : Just take snacks as string type. string snacks ; cin >> snacks; if ( snacks == "Lettuce" )
12th Mar 2022, 7:41 PM
Jayakrishna 🇮🇳
+ 2
Jayakrishna🇮🇳 its a code coach challenge. Here is the info Your pet Iguana has run away, and you found it up in a tree! It will come down right away if you brought the right snacks, but if you don't have enough, you will have to wait. You need 10 total snack points to bring it down. Lettuce is worth 5, Carrot is worth 4, Mango is worth 9, and Cheeseburger is worth 0. Task: Evaluate whether or not you have enough snack points to convince your iguana to come down. Input Format: Your input is a string that represents the snacks that you have. Snacks are separated by spaces, you can have any number of snacks, and they can repeat. Output Format: A string that says 'Come on Down!' if you have enough points, or 'Time to wait' if you do not.
12th Mar 2022, 8:20 PM
zack
+ 2
Jayakrishna🇮🇳 how would i go about comparing each word? If(snacks == “Lettuce”)?
12th Mar 2022, 8:21 PM
zack
+ 2
As you're using c-style string (char array) you'll need to use strtok from the cstring header file along with strcmp to compare the individual words. If you use C++ "string" you could use stringstream and do the comparisons using "==".
12th Mar 2022, 9:23 PM
rodwynnejones
rodwynnejones - avatar
+ 1
Just take snacks as string type. string snacks ; cin >> snacks; if ( snacks == "Lettuce" )
12th Mar 2022, 8:28 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 i have to use getline because its one string with multiple words seperated by spaces. I tried that if statment but it didnt work. Is it possible to access individual words in the string?
12th Mar 2022, 8:50 PM
zack
+ 1
zack What are the constraints to implement it? means along with getline method, what else you need to use? cin works by using a loop.. by getline , you need to split string into words.. there are many ways.. is this about any specific way of using inbuilt features ? or you can go with any method?
12th Mar 2022, 9:36 PM
Jayakrishna 🇮🇳
+ 1
rodwynnejones bro ive been reading on forums all day trying to figure this out. You taking the time to give me an example, in context, has made a lot of what i read make sense. Thank you very much
13th Mar 2022, 2:24 AM
zack
0
rodwynnejones Ive been reading about stringstream on different sites trying to figure out how to use it for this program, but so far i haven’t figured it out. Its really giving me trouble but ill just have to keep reading and try to figure it out.
13th Mar 2022, 12:37 AM
zack
0
//zack the other ways , you can use : #include <iostream> #include <string.h> using namespace std; int main() { int points =0; char snacks[100]; cin.getline(snacks,99); // using strtok way : char *ptr = strtok(snacks, " "); while(ptr != NULL){ if( strcmp(ptr ,"Lettuce") == 0) points += 5; if( strcmp(ptr , "Carrot") == 0 ) points = points + 4; if( strcmp(ptr, "Mango") == 0 ) points += 9; ptr = strtok(NULL, " "); } /* // simple way of using cin string snacks; while( cin >> snacks){ if( snacks == "Lettuce" ) points += 5; if( snacks == "Carrot" ) points = points + 4; if( snacks == "Mango" ) points += 9; } */ cout << points; return 0; } // Hope it helps..
13th Mar 2022, 10:20 AM
Jayakrishna 🇮🇳